Word操作 三步曲(一)

来源:互联网 发布:java 分布式日志框架 编辑:程序博客网 时间:2024/06/08 06:09

首先添加引用,解决方案资源管理器-》引用-》添加-》Com-》浏览-》C:/Program Files/Microsoft Office/OFFICE11/MSWORD.OLB    我使用的是office 2003其他版本我不太清楚,.net会自动把OLB控件转换成DLL文件

使用方法:

  object oMissing = System.Reflection.Missing.Value;   Word.Application oWord =new Word.Application();  oWord.Visible = false;//设置Word应用程序为不可见//新建一个Word文档   Word.Document oDoc=oWord.Documents.Add(ref oMissing,ref oMissing ,ref oMissing,ref oMissing);   //文档内容的复制与粘贴    oDoc.Content.Copy();    oDoc.Content.Paste()//文档的另存为oDoc.SaveAs(ref fileName,ref saveFormat,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing);//其他设置   oDoc.PageSetup.PaperSize=Word.WdPaperSize.wdPaperA3;//页面设置   oDoc.PageSetup.Orientation=Word.WdOrientation.wdOrientLandscape;//横板还是竖板   oDoc.PageSetup.TextColumns.SetCount(2);//分栏//关闭Word   oWord.Application.Quit(ref b,ref oMissing,ref oMissing);   System.Runtime.InteropServices.Marshal.ReleaseComObject(oWord);


通过oDoc对象对Word文档进行操作(word能做的它都能做)进行操作里面有很多函数,有兴趣的自己研究


转载地址:http://blog.csdn.net/chenhuizhouhb/article/details/6228822