利用html模板生成Word文件(服务器端不需要安装Word)

  由于管理的原因,不能在服务器上安装Office相关组件,所以只能采用客户端读取Html模板,后台对模板中标记的字段数据替换并返回给客户端的方法来实现,经过测试这种方法也是一种不错的选择!

首先自己写一个html网页模板,代码如下:

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title>投注站申请表</title>
  5. <style type="text/css">
  6. table {
  7. border-collapse: collapse;
  8. }
  9. table tr td {
  10. border: 1px solid black;
  11. font-size:17px;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <table cellpadding="" cellspacing="" style="margin:10px auto;">
  17. <tr>
  18. <td colspan="" style="font-weight:bold;text-align:center;">
  19. 投注站申请表
  20. </td>
  21. </tr>
  22. <tr>
  23. <td style="width:80px;">
  24. 申请人
  25. </td>
  26. <td style="width:220px;">
  27. {ProposerName}
  28. </td>
  29. <td style="width:150px;">
  30. 电话号码
  31. </td>
  32. <td style="width:130px;">
  33. {PhoneNo}
  34. </td>
  35. </tr>
  36. <tr>
  37. <td style="width:80px;">
  38. 申请地址
  39. </td>
  40. <td style="width:220px;">
  41. {ProposerAddress}
  42. </td>
  43. <td style="width:150px;">
  44. 申请房屋面积
  45. </td>
  46. <td style="width:130px;">
  47. {HouseArea}
  48. </td>
  49. </tr>
  50. <tr>
  51. <td style="width:80px;">
  52. 房屋类型
  53. </td>
  54. <td style="width:220px;">
  55. {HouseType}
  56. </td>
  57. <td style="width:150px;">
  58. 房屋性质
  59. </td>
  60. <td style="width:130px;">
  61. {HouseNature}
  62. </td>
  63. </tr>
  64. <tr>
  65. <td style="width:80px;">
  66. 申请日期
  67. </td>
  68. <td colspan="">
  69. {ApplyDate}
  70. </td>
  71. </tr>
  72. </table>
  73. </body>
  74. </html>

html模板代码

  

后台读取该模板并替换返回给客户端即可,代码如下:

  1. #region 根据申请单ID号和模板生成word下载文件
  2. public void DownLoadWord(string id)
  3. {
  4. if (string.IsNullOrEmpty(id))
  5. {
  6. id = "";
  7. }
  8. string sql = "SELECT ID,ProposerName,PhoneNo,ProposerAddress,HouseArea,HouseType,HouseNature,ApplyDate" +
  9. " from BettingStationApply where ID=@id ";
  10. SqlParameter[] parm = new SqlParameter[] { new SqlParameter("@id", int.Parse(id)) };
  11. //根据ID号取得当前申请单的详细信息
  12. DataTable dt = DBHelper.GetDataSet(sql, parm);
  13. if (dt.Rows.Count > )
  14. {
  15. DataRow dr = dt.Rows[];
  16. try
  17. {
  18. //模板路径
  19. string tempName = Server.MapPath(@"/BettingStation/ApplyTemplete.html");
  20. string fileContent=File.ReadAllText(tempName, Encoding.UTF8);
  21. //替换html模板中相关内容
  22. if (dr["ProposerName"] != DBNull.Value)
  23. {
  24. fileContent=fileContent.Replace("{ProposerName}", dr["ProposerName"].ToString());
  25. }
  26. else
  27. {
  28. fileContent = fileContent.Replace("{ProposerName}", "");
  29. }
  30. if (dr["PhoneNo"] != DBNull.Value)
  31. {
  32. fileContent = fileContent.Replace("{PhoneNo}", dr["PhoneNo"].ToString());
  33. }
  34. else
  35. {
  36. fileContent = fileContent.Replace("{PhoneNo}", "");
  37. }
  38. if (dr["ProposerAddress"] != DBNull.Value)
  39. {
  40. fileContent = fileContent.Replace("{ProposerAddress}", dr["ProposerAddress"].ToString());
  41. }
  42. else
  43. {
  44. fileContent = fileContent.Replace("{ProposerAddress}", "");
  45. }
  46. if (dr["HouseArea"] != DBNull.Value)
  47. {
  48. fileContent = fileContent.Replace("{HouseArea}", dr["HouseArea"].ToString());
  49. }
  50. else
  51. {
  52. fileContent = fileContent.Replace("{HouseArea}", "");
  53. }
  54. if (dr["HouseType"] != DBNull.Value)
  55. {
  56. fileContent = fileContent.Replace("{HouseType}", dr["HouseType"].ToString());
  57. }
  58. else
  59. {
  60. fileContent = fileContent.Replace("{HouseType}", "");
  61. }
  62. if (dr["HouseNature"] != DBNull.Value)
  63. {
  64. fileContent = fileContent.Replace("{HouseNature}", dr["HouseNature"].ToString());
  65. }
  66. else
  67. {
  68. fileContent = fileContent.Replace("{HouseNature}", "");
  69. }
  70. if (dr["ApplyDate"] != DBNull.Value)
  71. {
  72. fileContent = fileContent.Replace("{ApplyDate}", Convert.ToDateTime(dr["ApplyDate"].ToString()).ToString("yyyy-MM-dd HH:mm:ss"));
  73. }
  74. else
  75. {
  76. fileContent = fileContent.Replace("{ApplyDate}", "");
  77. }
  78. //替换掉换行
  79. fileContent = fileContent.Replace("\r", "").Replace("\n","").Replace("^p","") ;
  80. //文件名字
  81. string fileName = dr["ProposerName"].ToString() + "_投注站申请表.doc";
  82. HttpContext.Current.Response.Clear();
  83. HttpContext.Current.Response.Charset = "GB2312";
  84. HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
  85. // 添加头信息,为"文件下载/另存为"对话框指定默认文件名
  86. HttpContext.Current.Response.AddHeader("Content-Disposition",
  87. "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
  88. // 指定返回的是一个不能被客户端读取的流,必须被下载
  89. HttpContext.Current.Response.ContentType = "application/ms-word";
  90. // 把文件流发送到客户端
  91. HttpContext.Current.Response.Write(fileContent);
  92. // 停止页面的执行
  93. HttpContext.Current.Response.End();
  94. }
  95. catch (Exception ex)
  96. {
  97. //writeLog.WriteErrorLog("根据模板生成Word文件出错!错误信息:" + ex.Message);
  98. //Message.show("根据模板生成Word文件出错!错误信息:" + ex.Message);
  99. }
  100. }
  101. else
  102. {
  103. //writeLog.WriteErrorLog("id=" + id + "没有查找到任何数据!");
  104. //Message.show("id=" + id + "没有查找到任何数据!");
  105. }
  106.  
  107. }
  108. #endregion

  

到此,根据模板导出Word功能就完成了,该方法不需要服务器端安装Office组件,即可实现Word或者Excel的相关导出功能。

利用html模板生成Word文件(服务器端不需要安装Word)的更多相关文章

  1. 使用word模板生成pdf文件

    使用word模板生成pdf文件 源码:UserWord

  2. 根据PDF模板生成PDF文件(基于iTextSharp)

    根据PDF模板生成PDF文件,这里主要借助iTextSharp工具来完成.场景是这样的,假如要做一个电子协议,用过通过在线填写表单数据,然后系统根据用户填写的数据,生成电子档的协议.原理很简单,但是每 ...

  3. 利用T4模板生成ASP.NET Core控制器的构造函数和参数

    前言 在ASP.NET Core中引入了DI,并且通过构造函数注入参数,控制器中会大量使用DI注入各种的配置参数,如果配置注入的参数比较多,而且各个控制器需要的配置参数都基本一样的话,那么不断重复的复 ...

  4. 利用Python 脚本生成 .h5 文件 代码

    利用Python 脚本生成 .h5 文件 import os, json, argparse from threading import Thread from Queue import Queue ...

  5. 【Linux开发】【DSP开发】利用CCS6.1生成out文件的同时生成bin文件

    [Linux开发][DSP开发]利用CCS6.1生成out文件的同时生成bin文件 标签:[DSP开发] [Linux开发] 尝试在windows上安装的CCS6.1开发AM4378-Linux下的应 ...

  6. django生成文件txt、pdf(在生成 PDF 文件之前,需要安装 ReportLab 库)

    from django.http import HttpResponse def download_file(request): # Text file #response = HttpRespons ...

  7. itextsharp利用模板生成pdf文件笔记

    iTextSharp是一款开源的PDF操作类库,使用它可以快速的创建PDF文件. 中文参考网站:http://hardrock.cnblogs.com/ http://pdfhome.hope.com ...

  8. 【VS外接程序】利用T4模板生成模块代码

    引言 记得第一次做asp.net mvc项目时,可以用model直接生成Html的增删改查页面, 没什么特殊要求都可以不用修改直接用了, 觉得很神奇,效率太高了.后来在做客户端开发时,发现很多模块都是 ...

  9. java通过FreeMarker模板生成Excel文件之.ftl模板制作

    关于怎么通过freemarker模板生成excel的文章很多,关键点在于怎么制作模板文件.ftl 网上的办法是: (1)把Excel模板的格式调好,另存为xml文件 (2)新建一个.ftl文件,把xm ...

随机推荐

  1. [转载] Zookeeper中的 ACL(Access Control List)访问控制列表

    zk做为分布式架构中的重要中间件,通常会在上面以节点的方式存储一些关键信息,默认情况下,所有应用都可以读写任何节点,在复杂的应用中,这不太安全,ZK通过ACL机制来解决访问权限问题,详见官网文档:ht ...

  2. RESTLET开发实例

    1 前提 由于近期工作的需要,要把RESTLET应用到项目中,于是在网上参考了一些资料的基础上,实践了一个关于RESTLET接口的小例子. Restlet的思想是:HTTP客户端与HTTP服务器之间的 ...

  3. jira破解

    JIRA是一个优秀的问题(or bugs,task,improvement,new feature )跟踪及管理软件.    它由Atlassian开发,采用J2EE技术.它正被广泛的开源软件组织,以 ...

  4. ORA-02041: client database did not begin a transaction

    .NET中访问Oracle数据库链接:ORA-02041: client database did not begin a transaction 问题的处理. .NET中访问Oracle中带有DB_ ...

  5. DTD - Elements

    In a DTD, elements are declared with an ELEMENT declaration. Declaring Elements In a DTD, XML elemen ...

  6. 转载有个小孩跟我说LINQ(重点讲述Linq中GroupBy的原理及用法)

    转载原出处: http://www.cnblogs.com/AaronYang/archive/2013/04/02/2994635.html 小孩LINQ系列导航:(一)(二)(三)(四)(五)(六 ...

  7. UI基础 获取当前屏幕显示的viewcontroller

    #pragma mark - 获取当前屏幕显示的viewcontroller - (UIViewController *)getCurrentVC { UIViewController *result ...

  8. .net MVC全局定时器执行作业

    首先的一个需求是在OA系统中定时跑一些定时作业,例如发放年假等事务,之前的做法是在服务器上加入一个服务,用系统定时作业去跑服务,这样有个问题就是当系统在发布的过程中,有可能忘记启动服务而导致无法定时执 ...

  9. POJ 2003 Hire and Fire (Tree)

    题目:Hire and Fire 题目翻译成数据结构就是:建树,加结点,删除结点,打印结点.只有删除结点稍微复杂点,因为删除设计掉树的调整. 首先要考虑树怎么存储才能使解题更顺手. 1.我们要存储每个 ...

  10. 中国大概能用的NTPserver地址

    133.100.11.8 prefer210.72.145.44203.117.180.36131.107.1.10time.asia.apple.com64.236.96.53130.149.17. ...