C#创建Word文档实例解析

C#创建Word文档是我们经常在实际开发应用中碰到的实用性开发实例,那么具体的实现过程是什么呢?我们下面通过一个C#创建Word文档实例向你介绍具体的步骤。

C#创建Word文档实例:

 
 
 
  1. using MSExcel = Microsoft.Office.Interop.Excel;  
  2.  
  3. using System.IO;  
  4.  
  5. using System.Refletion;  
  6. //C#创建Word文档  
  7. Class Program  
  8.  
  9. {  
  10.  
  11.   static void Main(string[] args)  
  12.  
  13.   {  
  14.  
  15. string path;  //C#创建Word文档之文件路径  
  16.  
  17. string strContent;//文本内容  
  18.  
  19. MSWord.Application wordApp;提要    
  20. //C#创建Word文档之word应用程序  
  21.  
  22. MSWord.document wordDoc;//word文档  
  23.  
  24. path = @"c:\test.docx";  
  25.  
  26. wordApp = new MSWord.applicationClass();  
  27.  
  28. if(File.Exists(path))  
  29.  
  30. {  
  31.  
  32.   File.Delete(path);  
  33.  
  34. }  
  35.  
  36. Object nothing = Missing.Value;  
  37.  
  38. wordDoc = wordApp.Documents.Add(  
  39. ref nothing,ref nothing,ref nothing,ref nothing);  
  40.  
  41. strContent = "你好!\n";  
  42.  
  43. wordDoc.Paragraphs.Last.Rang.Text = strContent;  
  44.  
  45. object format = MSWord.WdSaveFormat.wdFormatDocumentDefault;  
  46.  
  47. wordDoc.SaveAs(ref path,ref format,  
  48. ref nothing,ref nothing,ref nothing,  
  49. ref nothing,ref nothing,  
  50.  
  51.  ref nothing,ref nothing,ref nothing,  
  52. ref nothing,ref nothing,ref nothing,  
  53.  
  54. ref nothing,ref nothing,ref nothing);  
  55.  
  56. wordDoc.Close(ref nothing,ref nothing,ref nothing);  
  57.  
  58. wordApp.Quit(ref nothing,ref nothing,ref nothing);  
  59.  
  60.   }  
  61. //C#创建Word文档  
  62. }   

C#创建Word文档的基本内容和实现实例就向你介绍到这里,希望对你了解和学习C#创建Word文档有所帮助。

【编辑推荐】

  1. 详解C#读取word内容操作
  2. C#读取Word文件实例详解
  3. C#读取Word学习经验总结
  4. 浅析C#打开Word文档实例
  5. 浅析C#Word文档替换操作

网页名称:C#创建Word文档实例解析
本文链接:http://www.hantingmc.com/qtweb/news41/442591.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联