参考: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. }

最新文章

  1. nginx 各类网站设置 (laravel , thinkphp , nodejs , https)
  2. HDU 1004 Let the Balloon Rise map
  3. Codeforces 338D GCD Table 中国剩余定理
  4. CocoaAsyncSocket + Protobuf 处理粘包和拆包问题
  5. linux的crash之hardlock排查记录
  6. 前端导出功能get和post两种方式
  7. TCP中往返时间的估计与超时
  8. js_html_input中autocomplete=&quot;off&quot;在chrom中失效的解决办法
  9. scikit-learn入门导航
  10. PHP查看编译参数
  11. 5、二、App Components(应用程序组件):0、概述
  12. _itemmod_rate_stone
  13. vim自定义插件放入pathogen管理
  14. 核弹级的git指令 git filter-branch
  15. docker中制作自己的JDK+tomcat镜像
  16. I/O的方法、输入流和输出流
  17. css3中有关transform的问题
  18. python清华大学镜像
  19. 配置linux服务器和pycharm的连接
  20. INSPIRED启示录 读书笔记 - 第27章 合理运用瀑布式开发方法

热门文章

  1. Base64中文不能加密问题
  2. 光流和KLT
  3. Cordova 使用经验
  4. malloc,free简单的实现
  5. ServiceStack.Redis——Redis于.net向上client解
  6. js中frame的操作问题
  7. ext panel 它们的定义图像刷新
  8. 微信公众平台企业号验证接口、回调 PHP版
  9. Sqlserver2012 评估期已过解决问题
  10. c++使用stmp协议发送电子邮件(163邮箱,TTL非SSL)