本方法是针对word导出操作,需要制作好的模板文件 模板.doc

引入应用Microsoft.Office.Interop.Word 11.0  (office2003)

导出文件注意:有时候迅雷会在浏览器中安装插件,下载时会默认使用迅雷下载,导致下载的文档格式丢失,这时为了避免错误,可以将下载方法DownFile()写在另一个页面中,通过session将参数传递过去就可以

  1. /// <summary>
  2. /// 导出按钮
  3. /// </summary>
  4. /// <param name="sender"></param>
  5. /// <param name="e"></param>
  6. protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
  7. {
  8.  
  9. string id;
  10. id = Session["ObjChangeID"].ToString();
  11. try
  12. {
  13.  
  14. if (id!=null)
  15. {
  16.  
  17. string _filePath = PrintDoc(id);
  18. if (_filePath != "")
  19. {
  20. DownloadFile(_filePath, Path.GetFileName(_filePath));//下载文件
  21. }
  22. }
  23. }
  24. catch (Exception ex)
  25. {
  26. ZWL.Common.PublicMethod.errorLog("ibtnExport_Click", ex);
  27. }
  28. }
  29. /// <summary>
  30. /// 打印操作,传入车辆Carcod或者变更ID
  31. /// </summary>
  32. /// <param name="id"></param>
  33. protected string PrintDoc(string id)
  34. {
  35. Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
  36. Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
  37. try
  38. {
  39.  
  40. if (id.Length != )
  41. {
  42. objCarInfor.OpenCar(id);
  43. string templeteName = "模版.doc", downName = "";///模板文件名称
  44. downName = objCarInfor.ORG_NAME + "新文件.doc";//导出文件名
  45.  
  46. string templeteFile = System.Web.HttpContext.Current.Server.MapPath("~/") + "DocTempalete\\" + templeteName;//模板文件全路径
  47. string downFile = System.Web.HttpContext.Current.Server.MapPath("~/") + "ReportFile\\gonghan\\" + downName;///导出文件全路径
  48. try
  49. {
  50. File.Delete(downFile);//删除原有的同名文件
  51. }
  52. catch
  53. {
  54. }
  55. File.Copy(templeteFile, downFile);//复制模板文件到导出文件对应的文件夹下存档
  56. object Obj_FileName = downFile;
  57. object Visible = false;
  58. object ReadOnly = false;
  59. object missing = System.Reflection.Missing.Value;
  60. //打开文件
  61. doc = app.Documents.Open(ref Obj_FileName, ref missing, ref ReadOnly, ref missing,
  62. ref missing, ref missing, ref missing, ref missing,
  63. ref missing, ref missing, ref missing, ref Visible,
  64. ref missing, ref missing, ref missing,
  65. ref missing);
  66. doc.Activate();
  67. #region 给模板填入类容
  68. //光标转到书签
  69. object BookMarkName = "函号";
  70. object what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark;
  71.  
  72. if (app.ActiveDocument.Bookmarks.Exists(BookMarkName.ToString()) == true)
  73. {
  74. doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref BookMarkName);
  75. doc.ActiveWindow.Selection.TypeText(System.DateTime.Now.ToString("yyMMdd"));
  76. }
  77. BookMarkName = "年";
  78. what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark;
  79.  
  80. if (app.ActiveDocument.Bookmarks.Exists(BookMarkName.ToString()) == true)
  81. {
  82. doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref BookMarkName);
  83. doc.ActiveWindow.Selection.TypeText(DateTime.Now.ToString("yy"));
  84. }
  85. BookMarkName = "月";
  86. what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark;
  87.  
  88. if (app.ActiveDocument.Bookmarks.Exists(BookMarkName.ToString()) == true)
  89. {
  90. doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref BookMarkName);
  91. doc.ActiveWindow.Selection.TypeText(DateTime.Now.ToString("MM"));
  92. }
  93. BookMarkName = "日";
  94. what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark;
  95.  
  96. if (app.ActiveDocument.Bookmarks.Exists(BookMarkName.ToString()) == true)
  97. {
  98. doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref BookMarkName);
  99. doc.ActiveWindow.Selection.TypeText(DateTime.Now.ToString("dd"));
  100. }
  101. ///注意:书签必须不一样才能真确绑定,如果说模板中有需要出现两次的的内容,必须设置成两个标签:
    /// 如:姓名显示两次,则必须给两个位置都加上标签,以示区别
    259 #endregion
  102. object IsSave = true;
  103. doc.Close(ref IsSave, ref missing, ref missing);///关闭doc文档对象
  104. System.Runtime.InteropServices.Marshal.ReleaseComObject(doc);
  105.  
  106. doc = null;
  107.  
  108. object IsSave1 = false;
  109. app.Quit(ref IsSave1, ref missing, ref missing);
  110. System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
  111. app = null;
  112. GC.Collect();
  113. return downFile;
  114. }
  115. else
  116. {
  117. return "";
  118. }
  119.  
  120. }
  121. catch (Exception ex)
  122. {
  123. app = null;
  124. GC.Collect();
  125. return "";
  126. }
  127.  
  128. }
  129. ///
  130. /// <summary>
  131. /// 下载文件
  132. /// </summary>
  133. /// <param name="filename">文件名(全路径)</param>
  134. /// <param name="downname">文件下载名</param>
  135. protected void DownloadFile(string filename, string downname)
  136. {
  137. FileStream f;
  138. byte[] buffer = new byte[];
  139. try
  140. {
  141. f = new FileStream(filename, FileMode.Open);
  142. buffer = new byte[f.Length];
  143. f.Read(buffer, , buffer.Length);
  144. f.Close();
  145. }
  146. catch
  147. {
  148. ZWL.Common.MessageBox.Show(this, "文件不存在!");
  149. return;
  150. }
  151.  
  152. filename = filename.Replace(@"/", @"\");
  153. //20121023wangyj
  154.  
  155. string saveFileName = "";
  156. int intStart = filename.LastIndexOf("\\") + ;
  157. saveFileName = filename.Substring(intStart, filename.Length - intStart);
  158.  
  159. Response.Clear();
  160. Response.Buffer = true;
  161. Response.Charset = "GB2312";
  162.  
  163. string fileType = Path.GetExtension(filename).ToLower();
  164.  
  165. switch (fileType)
  166. {
  167. case ".asf":
  168. System.Web.HttpContext.Current.Response.ContentType = "video/x-ms-asf";
  169. break;
  170. case ".jpg":
  171. case ".jpeg":
  172. System.Web.HttpContext.Current.Response.ContentType = "image/jpeg";
  173. break;
  174. case ".gif":
  175. System.Web.HttpContext.Current.Response.ContentType = "image/gif";
  176. break;
  177. case ".pdf":
  178. System.Web.HttpContext.Current.Response.ContentType = "application/pdf";
  179. break;
  180. case ".avi":
  181. System.Web.HttpContext.Current.Response.ContentType = "video/avi";
  182. break;
  183. case ".doc":
  184. System.Web.HttpContext.Current.Response.ContentType = "application/msword";
  185. break;
  186. case ".zip":
  187. System.Web.HttpContext.Current.Response.ContentType = "application/zip";
  188. break;
  189. case ".rar":
  190. System.Web.HttpContext.Current.Response.ContentType = "application/rar";
  191. break;
  192. case ".xls":
  193. System.Web.HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
  194. break;
  195. case ".wav":
  196. System.Web.HttpContext.Current.Response.ContentType = "audio/wav";
  197. break;
  198. case ".mp3":
  199. System.Web.HttpContext.Current.Response.ContentType = "audio/mpeg3";
  200. break;
  201. case ".mpg":
  202. System.Web.HttpContext.Current.Response.ContentType = "audio/mpeg";
  203. break;
  204. case ".rtf":
  205. System.Web.HttpContext.Current.Response.ContentType = "application/rtf";
  206. break;
  207. case ".htm":
  208. case ".html":
  209. System.Web.HttpContext.Current.Response.ContentType = "text/html";
  210. break;
  211. case ".asp":
  212. System.Web.HttpContext.Current.Response.ContentType = "text/asp";
  213. break;
  214. default:
  215. System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
  216. break;
  217. }
  218.  
  219. Response.HeaderEncoding = System.Text.Encoding.GetEncoding("GB2312");
  220. Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(downname));
  221. Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
  222.  
  223. //Response.WriteFile(System.Configuration.ConfigurationSettings.AppSettings["TemplatePhysicalPath"].ToString() + pt.Path);
  224. Response.BinaryWrite(buffer);
  225. Response.GetHashCode();
  226. Response.End();
  227. }

C#实现按Word模板导出Word(加书签bookMark)的更多相关文章

  1. 利用模板导出文件(二)之jacob利用word模板导出word文件(Java2word)

    https://blog.csdn.net/Fishroad/article/details/47951061?locationNum=2&fps=1 先下载jacob.jar包.解压后将ja ...

  2. .net core 使用NPOI填充Word模板导出Word

    最近工作用到在Word模板插入数据库数据,导出一个带数据的Word文件,想起来之前操作Word都是用微软提供的Microsoft.Office.Interop.Word,而在最新的..NET CORE ...

  3. java根据word模板导出word文件

    1.word模板文件处理,如下图所示在word 文档中填值的地方写入占位变量 2.将word文档另存为xml文件.编辑如下图,找到填写的占位,修改为${bcrxm}格式 3.将文件后缀名改为.ftl文 ...

  4. SpringBoot集成文件 - 如何基于POI-tl和word模板导出庞大的Word文件?

    前文我们介绍了通过Apache POI通过来导出word的例子:那如果是word模板方式,有没有开源库通过模板方式导出word呢?poi-tl是一个基于Apache POI的Word模板引擎,也是一个 ...

  5. C#通过模板导出Word(文字,表格,图片)

    C#通过模板导出Word(文字,表格,图片)   C#导出Word,Excel的方法有很多,这次因为公司的业务需求,需要导出内容丰富(文字,表格,图片)的报告,以前的方法不好使,所以寻找新的导出方法, ...

  6. Net Core DocXCore 实现word模板导出

    实际工作中,往往有这样的需求,需要导出word,还有各种各样的样式,于是有了word模板导出. 实现以下几个需求: 1.表单导出 2.表格导出 3.表单表格混合导出 4.实际用例测试 解决方案: 实现 ...

  7. OpenXml Sdk 根据Word模板导出到word

    一:OpenXml Sdk 简介 Open XML标准的简单介绍:Ecma Office Open XML(“Open XML”)是针对字处理文档.演示文稿和电子表格的国际化开放标准,可免费供多个应用 ...

  8. JSP利用freemarker生成基于word模板的word文档

    利用freemarker生成基于word模板的word文档 freemarker简介 FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出.FreeMarker与Web容器 ...

  9. 使用Spire.Doc组件利用模板导出Word文档

    以前一直是用Office的组件实现Word文档导出,但是让客户在服务器安装Office,涉及到版权:而且Office安装,包括权限配置也是比较麻烦. 现在流行使用第三方组件来实现对Office的操作, ...

随机推荐

  1. DirectUI实现原理

    一,概念 传统的Windows窗口程序对每一个控件都会创建一个句柄,而DUI技术奖所有控件都绘制在一个窗体上,这些控件的逻辑和绘图方式必须自己进行编写和封装,所以这些控件都是无句柄的. DUI技术的实 ...

  2. IRC配置for open source community

    IRC是开源社区进行交流使用的一个很重要的工具. 但是很多公司有限制,不让在办公电脑上, 安装IRC的客户端. ZNC + weechat 是一个可以替代的 选择. irccloude 也是一个可替代 ...

  3. hdu1728逃离迷宫 (利用最短路径思想+优先队列(BFS))

    Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有 ...

  4. mysql中多个字段共同确定唯一性

    create table tbl_table ( id integer not null auto_increment, fname varchar(255), lname varchar(255), ...

  5. poj - 4045 - Power Station

    题意:一棵有n个结点的树,要取其中的一个结点,使得该结点到其他所有结点的距离和dis最小,即损耗I * I * R * dis最小,输出最小损耗和该结点(有多个的话按结点编号从小到大输出)(3 < ...

  6. [置顶] 有关ListIterator接口的add与remove方法探究

    ListIterator接口继承自Iterator接口,新增了add()等方法. 关于ListIterator的add()方法的作用(接口是没有方法实现的,但其实现类对于add()方法的实现机制大致相 ...

  7. uva 10051 Tower of Cubes(DAG最长路)

    题目连接:10051 - Tower of Cubes 题目大意:有n个正方体,从序号1~n, 对应的每个立方体的6个面分别有它的颜色(用数字给出),现在想要将立方体堆成塔,并且上面的立方体的序号要小 ...

  8. java 解析json的问题

    本文转载自http://chriszz.sinaapp.com/?p=392 Json就是Javascript notation,可以替代XML,用做数据交互. Json的两种基本表示形式,可以用自动 ...

  9. IsPostBack是什么意思,如何运用?

    IsPostBack是Page类的一个属性,返回值为一个布尔值.一般放在Page_Load事件中.当页面是第一次打开时其值为False,若当前页面为一个提交后的页面其值为True. if (!IsPo ...

  10. 在PreparedStatement中设置空值

    在PreparedStatement中设置空值 分类: Work&amp Study java2009-09-10 09:56 922人阅读 评论(0) 收藏 举报 nulltypes数据库 ...