本文介绍.NET操作Word的实现方法。通过Word的对象库文件“MSWORD.OLB”(word 2000为MSWORD9.OLB),就可以实现这个功能。

.NET操作Word可以用using Word来完成。基本上,vs.net将会主动将 库文件转化为DLL组件,这样咱们只要在源码中创立该组件目标即可到达操作Word的意图。

.NET操作Word的完成:using Word(.net 生成word)  .NET操作Word 第1张

要完成,咱们就需要Word的目标库文件“MSWORD.OLB”(word 2000为MSWORD9.OLB),一般装置了Office Word后,你就可以在office装置目录的Office10文件夹下面找到这个文件,当咱们将这个文件引进到项目后,咱们就可以在源码中运用各种操作函数来操作Word。具体做法是翻开菜单栏中的项目>增加引证>阅读,在翻开的“挑选组件”对话框中找到MSWORD.OLB后按确认即可引进此目标库文件。

在CS代码文件中对命名空间的运用,如:using Word;.NET操作Word典范如下:

  1. usingSystem;
  2. usingSystem.Drawing;
  3. usingSystem.Collections;
  4. usingSystem.ComponentModel;
  5. usingSystem.Windows.Forms;
  6. usingWord;
  7. namespaceExamSecure
  8. {
  9. ///
  10. ///ItemToDoc的摘要阐明。
  11. ///
  12. publicclassItemToDoc:System.Windows.Forms.Form
  13. {
  14. objectstrFileName;
  15. ObjectNothing;
  16. Word.ApplicationClassmyWordApp=newWord.ApplicationClass();
  17. Word.DocumentmyWordDoc;
  18. stringstrContent="";
  19. privateSystem.ComponentModel.Containercomponents=null;
  20. publicItemToDoc()
  21. {
  22. //
  23. //Windows窗体规划器支撑所必需的
  24. //
  25. InitializeComponent();
  26. //
  27. //TODO:在InitializeComponent调用后增加任何结构函数代码
  28. //
  29. }
  30. [STAThread]
  31. staticvoidMain()
  32. {
  33. System.Windows.Forms.Application.Run(newItemToDoc());
  34. }
  35. ///
  36. ///整理一切正在运用的资源。
  37. ///
  38. protectedoverridevoidDispose(booldisposing)
  39. {
  40. if(disposing)
  41. {
  42. if(components!=null)
  43. {
  44. components.Dispose();
  45. }
  46. }
  47. base.Dispose(disposing);
  48. }
  49. #regionWindowsFormDesignergeneratedcode
  50. ///
  51. ///规划器支撑所需的办法-不要运用代码修改器修正
  52. ///此办法的内容。
  53. ///
  54. privatevoidInitializeComponent()
  55. {
  56. //
  57. //ItemToDoc
  58. //
  59. this.AutoScaleBaseSize=newSystem.Drawing.Size(6,14);
  60. this.ClientSize=newSystem.Drawing.Size(292,273);
  61. this.Name="ItemToDoc";
  62. this.Text="ItemToDoc";
  63. this.Load+=newSystem.EventHandler(this.ItemToDoc_Load);
  64. }
  65. #endregion
  66. privatevoidItemToDoc_Load(objectsender,System.EventArgse)
  67. {
  68. WriteFile();
  69. }
  70. privatevoidWriteFile()
  71. {
  72. strFileName=System.Windows.Forms.Application.StartupPath+"\\试题库【"+GetRandomString()+"】.doc";
  73. ObjectNothing=System.Reflection.Missing.Value;
  74. myWordDoc=myWordApp.Documents.Add(refNothing,refNothing,refNothing,refNothing);
  75. #region将数据库中读取得数据写入到word文件中
  76. strContent="试题库\n\n\r";
  77. WriteFile(strContent);
  78. strContent="试题库";
  79. WriteFile(strContent);
  80. #endregion
  81. //将WordDoc文档目标的内容保存为DOC文档
  82. myWordDoc.SaveAs(refstrFileName,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing);
  83. //封闭WordDoc文档目标
  84. myWordDoc.Close(refNothing,refNothing,refNothing);
  85. //封闭WordApp组件目标
  86. myWordApp.Quit(refNothing,refNothing,refNothing);
  87. }
  88. ///
  89. ///获取一个随即字符串
  90. ///
  91. ///
  92. privatestringGetRandomString()
  93. {
  94. DateTimeiNow=DateTime.Now;
  95. stringstrDate=iNow.ToString("yyyyMMddHHmmffff");
  96. Randomran=newRandom();
  97. intiRan=Convert.ToInt32(10000*ran.NextDouble());
  98. stringstrRan=iRan.ToString();
  99. //位数缺乏则补0
  100. intiRanlen=strRan.Length;
  101. for(inti=0;i<4-iRanlen;i++)
  102. {
  103. strRan="0"+strRan;
  104. }
  105. returnstrDate+strRan;
  106. }
  107. ///
  108. ///将字符串写入到Word文件中
  109. ///
  110. ///要写入的字符串
  111. privatevoidWriteFile(stringstr)
  112. {
  113. myWordDoc.Paragraphs.Last.Range.Text=str;
  114. }
  115. }
  116. }

以上便是.NET操作Word的完成代码。

【修改引荐】

  1. ASP.NET新手问题总结
  2. 深入研究Repeater控件:最大的灵活性
  3. DataList控件入门介绍
  4. DataGrid Web控件运作机制探秘
  5. 小议ASP.NET数据Web控件之间的相似性
转载请说明出处
知优网 » .NET操作Word的完成:using Word(.net 生成word)

发表评论

您需要后才能发表评论