最近要做一个处理Excel和Word文档的小程序,因为是新接触此类东东,因此在这里将这次开发的经历记录下来。刚刚在网上查了很此类资料,大多要在代码中引入一个命名空间"Microsoft.Office"之类,可是这之前需要导入组件"Excel.dll和Word.dll"(与引类似).这样才可以引入命名空间“Excel;”,接下来就是写代码了......(注:1.要保证机器本身要安装OFFICE.2.把[C:Program FilesMicrosoft OfficeOffice:默认安装路径]下的EXCEL9.OLB文件拷贝到c盘根目录下(如果没有OLB文件,可以用Excel.exe)3.打开Visual Studio .Net2003命令提示,运行TlbImp Excel9.olb Excel.dll ,就会在c盘根目录下下生成Excel.dll、Office.dll、VBIDE.dll组件。4.在项目中添加Excel.dll引用就OK了)(需要注意的地方:1、程序会把EXCLE表中的第一行记录作为列名;2、在使用EXCLE表的时候,要在表名后面加上符号$)

Excel.Application excel = new Excel.Application();//创建excel对象excel.Workbooks.Add(true);//创建excel工作薄

Office开发 (二)简单的打开保存Excel代码如下:private void btnExcel_Click(object sender, EventArgs e){Excel.Workbooks wb = null;Excel.Application excelKccx=null;try{excelKccx = new Excel.Application();object rmiss = System.Reflection.Missing.Value;wb=excelKccx.Workbooks;wb.Open(@"C:Documents and Settingsywg桌面短信报表 est.xls", rmiss, rmiss, rmiss, rmiss, rmiss, rmiss, rmiss, rmiss, rmiss, rmiss, rmiss, rmiss, rmiss, rmiss); excelKccx.Visible = true;//使excel可见*/excelKccx.Save(@"c:1.xls"); }catch (Exception err){ }finally{excelKccx.Quit();wb.Close();}}简单的打开保存Word代码如下:private void btnWord_Click(object sender, EventArgs e){Word.Application word = new Word.Application();object obj=System.Reflection.Missing.Value;string filename="d:test.doc";object file = (object)filename;Word.Document docu = word.Documents.Open(ref file, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj);word.Visible = true;foreach(Word.Bookmark bm in docu.Bookmarks){switch(bm.Name){case "book1": bm.Range.Text = "Book1"; break;case "book2": bm.Range.Text = "Book2"; break;case "book3": bm.Range.Text = "Book3"; break;default: bm.Range.Text = "其它"; break;}}file = (object)"c:test111.doc";docu.SaveAs(ref file, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj,ref obj,ref obj);}Office开发 (三)Excel2003连接字符串:@"Provider = Microsoft.Jet.OLEDB.4.0; Data Source =Excel文件路径;Extended Properties=Excel 8.0;"