利用html模板生成Word文件(服务器端不需要安装Word)
利用html模板生成Word文件(服务器端不需要安装Word)
由于管理的原因,不能在服务器上安装Office相关组件,所以只能采用客户端读取Html模板,后台对模板中标记的字段数据替换并返回给客户端的方法来实现,经过测试这种方法也是一种不错的选择!
首先自己写一个html网页模板,代码如下:
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>投注站申请表</title>
- <style type="text/css">
- table {
- border-collapse: collapse;
- }
- table tr td {
- border: 1px solid black;
- font-size:17px;
- }
- </style>
- </head>
- <body>
- <table cellpadding="" cellspacing="" style="margin:10px auto;">
- <tr>
- <td colspan="" style="font-weight:bold;text-align:center;">
- 投注站申请表
- </td>
- </tr>
- <tr>
- <td style="width:80px;">
- 申请人
- </td>
- <td style="width:220px;">
- {ProposerName}
- </td>
- <td style="width:150px;">
- 电话号码
- </td>
- <td style="width:130px;">
- {PhoneNo}
- </td>
- </tr>
- <tr>
- <td style="width:80px;">
- 申请地址
- </td>
- <td style="width:220px;">
- {ProposerAddress}
- </td>
- <td style="width:150px;">
- 申请房屋面积
- </td>
- <td style="width:130px;">
- {HouseArea}
- </td>
- </tr>
- <tr>
- <td style="width:80px;">
- 房屋类型
- </td>
- <td style="width:220px;">
- {HouseType}
- </td>
- <td style="width:150px;">
- 房屋性质
- </td>
- <td style="width:130px;">
- {HouseNature}
- </td>
- </tr>
- <tr>
- <td style="width:80px;">
- 申请日期
- </td>
- <td colspan="">
- {ApplyDate}
- </td>
- </tr>
- </table>
- </body>
- </html>
html模板代码
后台读取该模板并替换返回给客户端即可,代码如下:
- #region 根据申请单ID号和模板生成word下载文件
- public void DownLoadWord(string id)
- {
- if (string.IsNullOrEmpty(id))
- {
- id = "";
- }
- string sql = "SELECT ID,ProposerName,PhoneNo,ProposerAddress,HouseArea,HouseType,HouseNature,ApplyDate" +
- " from BettingStationApply where ID=@id ";
- SqlParameter[] parm = new SqlParameter[] { new SqlParameter("@id", int.Parse(id)) };
- //根据ID号取得当前申请单的详细信息
- DataTable dt = DBHelper.GetDataSet(sql, parm);
- if (dt.Rows.Count > )
- {
- DataRow dr = dt.Rows[];
- try
- {
- //模板路径
- string tempName = Server.MapPath(@"/BettingStation/ApplyTemplete.html");
- string fileContent=File.ReadAllText(tempName, Encoding.UTF8);
- //替换html模板中相关内容
- if (dr["ProposerName"] != DBNull.Value)
- {
- fileContent=fileContent.Replace("{ProposerName}", dr["ProposerName"].ToString());
- }
- else
- {
- fileContent = fileContent.Replace("{ProposerName}", "");
- }
- if (dr["PhoneNo"] != DBNull.Value)
- {
- fileContent = fileContent.Replace("{PhoneNo}", dr["PhoneNo"].ToString());
- }
- else
- {
- fileContent = fileContent.Replace("{PhoneNo}", "");
- }
- if (dr["ProposerAddress"] != DBNull.Value)
- {
- fileContent = fileContent.Replace("{ProposerAddress}", dr["ProposerAddress"].ToString());
- }
- else
- {
- fileContent = fileContent.Replace("{ProposerAddress}", "");
- }
- if (dr["HouseArea"] != DBNull.Value)
- {
- fileContent = fileContent.Replace("{HouseArea}", dr["HouseArea"].ToString());
- }
- else
- {
- fileContent = fileContent.Replace("{HouseArea}", "");
- }
- if (dr["HouseType"] != DBNull.Value)
- {
- fileContent = fileContent.Replace("{HouseType}", dr["HouseType"].ToString());
- }
- else
- {
- fileContent = fileContent.Replace("{HouseType}", "");
- }
- if (dr["HouseNature"] != DBNull.Value)
- {
- fileContent = fileContent.Replace("{HouseNature}", dr["HouseNature"].ToString());
- }
- else
- {
- fileContent = fileContent.Replace("{HouseNature}", "");
- }
- if (dr["ApplyDate"] != DBNull.Value)
- {
- fileContent = fileContent.Replace("{ApplyDate}", Convert.ToDateTime(dr["ApplyDate"].ToString()).ToString("yyyy-MM-dd HH:mm:ss"));
- }
- else
- {
- fileContent = fileContent.Replace("{ApplyDate}", "");
- }
- //替换掉换行
- fileContent = fileContent.Replace("\r", "").Replace("\n","").Replace("^p","") ;
- //文件名字
- string fileName = dr["ProposerName"].ToString() + "_投注站申请表.doc";
- HttpContext.Current.Response.Clear();
- HttpContext.Current.Response.Charset = "GB2312";
- HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
- // 添加头信息,为"文件下载/另存为"对话框指定默认文件名
- HttpContext.Current.Response.AddHeader("Content-Disposition",
- "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
- // 指定返回的是一个不能被客户端读取的流,必须被下载
- HttpContext.Current.Response.ContentType = "application/ms-word";
- // 把文件流发送到客户端
- HttpContext.Current.Response.Write(fileContent);
- // 停止页面的执行
- HttpContext.Current.Response.End();
- }
- catch (Exception ex)
- {
- //writeLog.WriteErrorLog("根据模板生成Word文件出错!错误信息:" + ex.Message);
- //Message.show("根据模板生成Word文件出错!错误信息:" + ex.Message);
- }
- }
- else
- {
- //writeLog.WriteErrorLog("id=" + id + "没有查找到任何数据!");
- //Message.show("id=" + id + "没有查找到任何数据!");
- }
- }
- #endregion
到此,根据模板导出Word功能就完成了,该方法不需要服务器端安装Office组件,即可实现Word或者Excel的相关导出功能。
利用html模板生成Word文件(服务器端不需要安装Word)的更多相关文章
- 使用word模板生成pdf文件
使用word模板生成pdf文件 源码:UserWord
- 根据PDF模板生成PDF文件(基于iTextSharp)
根据PDF模板生成PDF文件,这里主要借助iTextSharp工具来完成.场景是这样的,假如要做一个电子协议,用过通过在线填写表单数据,然后系统根据用户填写的数据,生成电子档的协议.原理很简单,但是每 ...
- 利用T4模板生成ASP.NET Core控制器的构造函数和参数
前言 在ASP.NET Core中引入了DI,并且通过构造函数注入参数,控制器中会大量使用DI注入各种的配置参数,如果配置注入的参数比较多,而且各个控制器需要的配置参数都基本一样的话,那么不断重复的复 ...
- 利用Python 脚本生成 .h5 文件 代码
利用Python 脚本生成 .h5 文件 import os, json, argparse from threading import Thread from Queue import Queue ...
- 【Linux开发】【DSP开发】利用CCS6.1生成out文件的同时生成bin文件
[Linux开发][DSP开发]利用CCS6.1生成out文件的同时生成bin文件 标签:[DSP开发] [Linux开发] 尝试在windows上安装的CCS6.1开发AM4378-Linux下的应 ...
- django生成文件txt、pdf(在生成 PDF 文件之前,需要安装 ReportLab 库)
from django.http import HttpResponse def download_file(request): # Text file #response = HttpRespons ...
- itextsharp利用模板生成pdf文件笔记
iTextSharp是一款开源的PDF操作类库,使用它可以快速的创建PDF文件. 中文参考网站:http://hardrock.cnblogs.com/ http://pdfhome.hope.com ...
- 【VS外接程序】利用T4模板生成模块代码
引言 记得第一次做asp.net mvc项目时,可以用model直接生成Html的增删改查页面, 没什么特殊要求都可以不用修改直接用了, 觉得很神奇,效率太高了.后来在做客户端开发时,发现很多模块都是 ...
- java通过FreeMarker模板生成Excel文件之.ftl模板制作
关于怎么通过freemarker模板生成excel的文章很多,关键点在于怎么制作模板文件.ftl 网上的办法是: (1)把Excel模板的格式调好,另存为xml文件 (2)新建一个.ftl文件,把xm ...
随机推荐
- [转载] Zookeeper中的 ACL(Access Control List)访问控制列表
zk做为分布式架构中的重要中间件,通常会在上面以节点的方式存储一些关键信息,默认情况下,所有应用都可以读写任何节点,在复杂的应用中,这不太安全,ZK通过ACL机制来解决访问权限问题,详见官网文档:ht ...
- RESTLET开发实例
1 前提 由于近期工作的需要,要把RESTLET应用到项目中,于是在网上参考了一些资料的基础上,实践了一个关于RESTLET接口的小例子. Restlet的思想是:HTTP客户端与HTTP服务器之间的 ...
- jira破解
JIRA是一个优秀的问题(or bugs,task,improvement,new feature )跟踪及管理软件. 它由Atlassian开发,采用J2EE技术.它正被广泛的开源软件组织,以 ...
- ORA-02041: client database did not begin a transaction
.NET中访问Oracle数据库链接:ORA-02041: client database did not begin a transaction 问题的处理. .NET中访问Oracle中带有DB_ ...
- DTD - Elements
In a DTD, elements are declared with an ELEMENT declaration. Declaring Elements In a DTD, XML elemen ...
- 转载有个小孩跟我说LINQ(重点讲述Linq中GroupBy的原理及用法)
转载原出处: http://www.cnblogs.com/AaronYang/archive/2013/04/02/2994635.html 小孩LINQ系列导航:(一)(二)(三)(四)(五)(六 ...
- UI基础 获取当前屏幕显示的viewcontroller
#pragma mark - 获取当前屏幕显示的viewcontroller - (UIViewController *)getCurrentVC { UIViewController *result ...
- .net MVC全局定时器执行作业
首先的一个需求是在OA系统中定时跑一些定时作业,例如发放年假等事务,之前的做法是在服务器上加入一个服务,用系统定时作业去跑服务,这样有个问题就是当系统在发布的过程中,有可能忘记启动服务而导致无法定时执 ...
- POJ 2003 Hire and Fire (Tree)
题目:Hire and Fire 题目翻译成数据结构就是:建树,加结点,删除结点,打印结点.只有删除结点稍微复杂点,因为删除设计掉树的调整. 首先要考虑树怎么存储才能使解题更顺手. 1.我们要存储每个 ...
- 中国大概能用的NTPserver地址
133.100.11.8 prefer210.72.145.44203.117.180.36131.107.1.10time.asia.apple.com64.236.96.53130.149.17. ...