打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
c#写word文档基础操作

c#写word文档基础操作    
     
  新建一个项目,在项目的引用当中添加Com组件:Microsoft   Word   11.0   Object   Library  
  之后使用名称空间Word即:using   Word;  
  下面一个函数,建立一个Word   文档,添加页眉、页脚,在内容中两个不同字体的Hello!!!  
  public   void   myFunction()  
  {  
    Word.ApplicationClass   oWordApp     =   new   Word.ApplicationClass();//建立Word   对象,启动word程序  
    object   missing   =   System.Reflection.Missing.Value;  
    object   oTemplate   =   System.Windows.Forms.Application.StartupPath+"\\mytemplate.dot";  
    Word.Document   oWordDoc   =   oWordApp.Documents.Add(   ref   oTemplate,ref   missing,ref   missing,   ref   missing);//新建word文档  
    oWordApp.Visible   =   true;//设置Word程序可见,如果为false   那么word   不可见  
    //页面设置  
    oWordDoc.PageSetup.TopMargin   =   oWordApp.CentimetersToPoints(2.5f);       //上  
    oWordDoc.PageSetup.BottomMargin   =   oWordApp.CentimetersToPoints(2f);//下  
    oWordDoc.PageSetup.LeftMargin=oWordApp.CentimetersToPoints(2.2f);//左  
    oWordDoc.PageSetup.RightMargin=oWordApp.CentimetersToPoints(2.2f);//右  
    //添加页眉  
    oWordDoc.ActiveWindow.ActivePane.View.SeekView   =   WdSeekView.wdSeekCurrentPageHeader;   //激活页眉的编辑  
    oWordApp.Selection.ParagraphFormat.Alignment   =   WdParagraphAlignment.wdAlignParagraphCenter;   //设置对齐方式  
    string   headtext1   ="Head   Text";      
    oWordApp.Selection.Font.Name   ="华文新魏";   //设置字体  
    oWordApp.Selection.Font.Size   =10.5f;  
    oWordApp.Selection.Font.UnderlineColor   =   Word.WdColor.wdColorAutomatic;  
    oWordApp.Selection.Font.Underline   =   Word.WdUnderline.wdUnderlineSingle;   //添加下划线  
    oWordApp.Selection.TypeText(headtext1);  
    oWordApp.Selection.Font.Underline   =   Word.WdUnderline.wdUnderlineNone;  
    //添加页脚  
    string   foottext1   ="Foot   Text";  
    oWordDoc.ActiveWindow.ActivePane.View.SeekView   =Word.WdSeekView.wdSeekCurrentPageFooter;   //激活页脚的编辑  
    oWordApp.Selection.ParagraphFormat.Alignment   =   WdParagraphAlignment.wdAlignParagraphCenter;  
    oWordApp.Selection.Font.Name   ="仿宋_GB2312";  
    oWordApp.Selection.Font.Size   =8;  
    oWordApp.Selection.TypeText(foottext1);  
    //添加正文  
    oWordDoc.ActiveWindow.ActivePane.View.SeekView   =Word.WdSeekView.wdSeekMainDocument;//激活页面内容的编辑  
    oWordApp.Selection.Font.Name   ="宋体";  
    oWordApp.Selection.Font.Size   =10.5f;  
    oWordApp.Selection.Font.Scaling   =   200;  
    oWordApp.Selection.TypeText("Hello!!!");  
    oWordApp.Selection.TypeParagraph();//另起一段  
   
  oWordApp.Selection.Font.Name   ="黑体";  
    oWordApp.Selection.Font.Size   =10.5f;  
    oWordApp.Selection.Font.Scaling   =   100;  
    oWordApp.Selection.TypeText("Hello!!!");  
    oWordApp.Selection.TypeParagraph();//另起一段  
     
    string   strfilename   =   System.Windows.Forms.Application.StartupPath+"\\myfirst.doc";  
    object   filename   =   strfilename     ;  
    //保存文档为word2000格式  
    oWordDoc.SaveAs2000(ref   filename,ref   missing,ref   missing,ref   missing,ref   missing,ref   missing,ref   missing,ref   missing,ref   missing,ref   missing,ref   missing);  
    //保存文档为word2003格式  
    //oWordDoc.SaveAs(ref   filename,   ref   missing,   ref   missing,   ref   missing,   ref   missing,  
    //       ref   missing,   ref   missing,   ref   missing,   ref   missing,   ref   missing,  
    //       ref   missing,   ref   missing,   ref   missing,   ref   missing,   ref   missing,  
    //       ref   missing)   ;  
    //以下关闭Word程序  
    object   nochanges   =   Word.WdSaveOptions.wdDoNotSaveChanges;  
    if(oWordApp.Documents!=   null)  
    {  
      IEnumerator   ie   =     oWordApp.Documents.GetEnumerator();  
      while(   ie.MoveNext())  
      {  
        Word.Document   closedoc   =   (Word.Document)ie.Current;  
        closedoc.Close(ref   nochanges,ref   missing,ref   missing);  
      }  
    }    
    oWordApp.Quit(ref   nochanges,   ref   missing,   ref   missing);  
  }  
     
  使用C#进行Office套件编程基础教程(概念篇)  
   
  公司员工根据实际项目操作写了一篇“C#写word文档基础操作”的交流文档,讲述了一下使用C#进行Word文档操作的基本方法。在实际应用过程中,我们不仅可以使用Word程序,而且可以使用所有基于COM组件技术的OFFICE套件,往往可使问题解决获得最佳途径。下面阐述一下在C#中使用这些Office套件的一些基本概念。  
  一、托管代码与非托管代码  
          众所周知C#使用的.Net   FrameWork   SDK中的所有类库都是托管代码(Managed   Code),而COM组件使用的是非托管代码(Unmanaged   Code)的文件,这些非托管代码的COM组件要想被C#文件使用必须经过转换或者"如果可用,应始终使用要集成到托管代码中的   COM   组件的作者所发布的主   Interop   程序集。主   Interop   程序集中的类型已为您导入,可以从托管代码中激活和调用。"(详见微软相关文档http://msdn2.microsoft.com/zh-cn/library/tt0cf3sx.aspx)在上述“C#写word文档基础操作”一文中已经说明了通过Interop程序集使用Office套件的方法,不过需要说明的是在程序里面引用了COM组件:Microsoft   Word   11.0   Object   Library之后,在引用中的Word的名称是"Microsoft.Office.Interop.Word",他的类型是"ActiveX",在程序中要使用Word时需要添加的命名空间应为"Microsoft.Office.Interop.Word",而不能只引用Word。  
  二、目标库  
    每一个Microsoft   Office应用程序都在一个dll文件中提供了多种类型库资源,这种dll文件叫做目标库(*.olb).  
            一个类型库是一个提供COM对象功能信息的文件或文件的一部分,而且类型库包含了有关类的信息。此类型库并不存储实际的对象,而只是存储有关这些对象的信息。类型库详细说明了一个自动化客户机为对象需要调用的方法和属性的信息,比如说它详细的描述了接受或返回的值。    
  三、非托管代码文件到托管代码文件的转换  
            非托管代码文件   COM类型库中的类型定义转换为托管代码公共语言运行库程序集中的等效定义可以通过微软.Net   FrameWork   SDK中提供的工具"Tlbimp.exe"进行转换。Tlbimp.exe   的输出为二进制文件(程序集),该文件中包含在原始类型库中定义的类型的运行库元数据。可以使用诸如   Ildasm.exe   这样的工具检查此文件。具体的使用命令为:tlbimp.exe   XXXXX.olb。在转换后就可以得到我们编程所需的类库文件。例如使用tlbimp.exe   Excel9.olb   可以得到excel.dll,此excel.dll可以在C#中直接使用。  
            在引用了转换过的类库文件后在引用中的Word的名称是"Word",他的类型是"程序集",这样命名空间就可以直接使用Word了。  
  四、命名空间的指定  
            在多数程序中我们运行程序的语句为Application.Run(new   Form1());  
  但在我们引用了Office相关套件以后(不论引用的COM还是托管类库)在相关命名空间中都会多出一个Application的对象,这样必须指定运行程序的语句为System.Windows.Forms.Application.Run(new   Form1());才能使程序如常运行。  
   
   
  这两篇文章是在公司时候别人写的,关于那个文中提到的引用的问题,应该和Office的版本有关,最近没什么时间看office编程了,都再吃老底子,出错难免.不要见笑哦. 

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jy_0219/archive/2010/01/23/5223622.aspx

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
高级 COM 互操作性
C# 互操作性入门系列(四):在C# 中调用COM组件
VSTO学习笔记(二)Excel对象模型
C#Using用法
使用.Net访问Office编程接口(转)
解决方法:未能加载文件或程序集“Microsoft.Office.Interop.Excel。。
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服