现在的位置: 首页 > 综合 > 正文

使用C#创建Word文档

2018年01月27日 ⁄ 综合 ⁄ 共 938字 ⁄ 字号 评论关闭

using MSExcel = Microsoft.Office.Interop.Excel;

using System.IO;

using System.Refletion;

Class Program

{

  static void Main(string[] args)

  {

    string path;      //文件路径

    string strContent;    //文本内容

    MSWord.Application wordApp;提要   //word应用程序

    MSWord.document wordDoc;    //word文档

    path = @"c:/test.docx";

    wordApp = new MSWord.applicationClass();

    if(File.Exists(path))

    {

      File.Delete(path);

    }

    Object nothing = Missing.Value;

    wordDoc = wordApp.Documents.Add(ref nothing,ref nothing,ref nothing,ref nothing);

    strContent = "你好!/n";

    wordDoc.Paragraphs.Last.Rang.Text = strContent;

    object format = MSWord.WdSaveFormat.wdFormatDocumentDefault;

    wordDoc.SaveAs(ref path,ref format,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,

              ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,

             ref nothing,ref nothing,ref nothing);

    wordDoc.Close(ref nothing,ref nothing,ref nothing);

    wordApp.Quit(ref nothing,ref nothing,ref nothing);

  }

抱歉!评论已关闭.