c#生成word文档

时间:2022-09-07 09:09:49

参考:http://blog.163.com/zhouchunping_99/blog/static/7837998820085114394716/

  1. 生成word文档

生成word文档

view plaincopy to clipboardprint?

  1. public class BiultReportForm
  2. {
  3. /// <SUMMARY></SUMMARY>
  4. /// word 应用对象
  5. ///
  6. private Microsoft.Office.Interop.Word.Application _wordApplication;
  7. /// <SUMMARY></SUMMARY>
  8. /// word 文件对象
  9. ///
  10. private Microsoft.Office.Interop.Word.Document _wordDocument;
  11. /// <SUMMARY></SUMMARY>
  12. /// 创建文档
  13. ///
  14. public void CreateAWord()
  15. {
  16. //实例化word应用对象
  17. this._wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
  18. Object myNothing = System.Reflection.Missing.Value;
  19. this._wordDocument = this._wordApplication.Documents.Add(ref myNothing, ref myNothing, ref myNothing, ref myNothing);
  20. }
  21. /// <SUMMARY></SUMMARY>
  22. /// 添加页眉
  23. ///
  24. /// <PARAM name="pPageHeader" />
  25. public void SetPageHeader(string pPageHeader)
  26. {
  27. //添加页眉
  28. this._wordApplication.ActiveWindow.View.Type =Microsoft .Office .Interop .Word.WdViewType.wdOutlineView;
  29. this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader;
  30. this._wordApplication.ActiveWindow.ActivePane.Selection.InsertAfter(pPageHeader);
  31. //设置中间对齐
  32. this._wordApplication.Selection.ParagraphFormat.Alignment =Microsoft .Office .Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
  33. //跳出页眉设置
  34. this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;
  35. }
  36. /// <SUMMARY></SUMMARY>
  37. /// 插入文字
  38. ///
  39. /// <PARAM name="pText" />文本信息
  40. /// <PARAM name="pFontSize" />字体打小
  41. /// <PARAM name="pFontColor" />字体颜色
  42. /// <PARAM name="pFontBold" />字体粗体
  43. /// <PARAM name="ptextAlignment" />方向
  44. public void InsertText(string pText, int pFontSize, Microsoft.Office.Interop.Word.WdColor pFontColor, int pFontBold, Microsoft.Office.Interop.Word.WdParagraphAlignment ptextAlignment)
  45. {
  46. //设置字体样式以及方向
  47. this._wordApplication.Application.Selection.Font.Size = pFontSize;
  48. this._wordApplication.Application.Selection.Font.Bold = pFontBold;
  49. this._wordApplication.Application.Selection.Font.Color= pFontColor;
  50. this._wordApplication.Application.Selection.ParagraphFormat.Alignment = ptextAlignment;
  51. this._wordApplication.Application.Selection.TypeText(pText);
  52. }
  53. /// <SUMMARY></SUMMARY>
  54. /// 换行
  55. ///
  56. public void NewLine()
  57. {
  58. //换行
  59. this._wordApplication.Application.Selection.TypeParagraph();
  60. }
  61. /// <SUMMARY></SUMMARY>
  62. /// 插入一个图片
  63. ///
  64. /// <PARAM name="pPictureFileName" />
  65. public void InsertPicture(string pPictureFileName)
  66. {
  67. object myNothing = System.Reflection.Missing.Value;
  68. //图片居中显示
  69. this._wordApplication.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
  70. this._wordApplication.Application.Selection.InlineShapes.AddPicture(pPictureFileName, ref myNothing, ref myNothing, ref myNothing);
  71. }
  72. /// <SUMMARY></SUMMARY>
  73. /// 保存文件
  74. ///
  75. /// <PARAM name="pFileName" />保存的文件名
  76. public void SaveWord(string pFileName)
  77. {
  78. object myNothing = System.Reflection.Missing.Value;
  79. object myFileName = pFileName;
  80. object myWordFormatDocument =Microsoft .Office .Interop .Word.WdSaveFormat.wdFormatDocument;
  81. object myLockd = false;
  82. object myPassword = "";
  83. object myAddto = true;
  84. try
  85. {
  86. this._wordDocument.SaveAs(ref myFileName, ref myWordFormatDocument, ref myLockd, ref myPassword, ref myAddto, ref myPassword,
  87. ref myLockd, ref myLockd, ref myLockd, ref myLockd, ref myNothing, ref myNothing, ref myNothing,
  88. ref myNothing, ref myNothing, ref myNothing);
  89. }
  90. catch
  91. {
  92. throw new Exception("导出word文档失败!");
  93. }
  94. }
  95. }