利用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 ...
随机推荐
- python basic programs
- hdu4812-D Tree (树的点分治)
昨天学了下树分治,今天补这道题,还是太不熟练了,写完之后一直超时.后来查出好多错= =比如v,u写倒了,比如+写成了取最值,比如....爆int...查了两个多小时的错..哭...(没想到进首页了 h ...
- mysql登陆报错(ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2))
部署mysql版本信息 version: 5.6.21 具体现象: mysql服务能够正常启动如下: [root@localhost ~]# service mysqld restart Shutti ...
- ST-Link STVP Cannot communicate with the device!
用STLink在ST Visual Programmer中对STM8下载二进制文件有时会出现: 原因:多半是STM8目标板没有电源有问题,或是电源引脚虚焊:
- struts2—拦截器
在Struts2中,如果用户没有指定执行哪些拦截器,struts2有一个默认执行的栈,defaultStack; 一旦如果用户有指定执行哪些拦截器,默认的拦截器栈就不会被执行 拦截器配置举例(stru ...
- (转) 如何在JavaScript与ActiveX之间传递数据1
本文研究如何在JS等脚本语言与ActiveX控件之间通信,如何传递各种类型的参数,以及COM的IDispatch接口.使用类似的方法,可以推广到其他所有脚本型语言,如LUA,AutoCad等.本文将研 ...
- c++简单的ATL COM开发和调用实例(转)
c++简单的ATL COM开发和调用实例 1.打开VS2010,新建ATL COM 项目,步骤:“文件” -->“新建” -->“项目”,选择“Visual C++” -->“ATL ...
- Debian 6配置GNOME桌面环境
1.安装xorgroot@debian:~# apt-get install xorg 2.安装gdm(GNOME Display Manager)root@debian:~# apt-get i ...
- Mysql导出表结构及表数据 mysqldump用法
几个常用用例: 1.导出整个数据库 mysqldump -u 用户名 -p 数据库名 > 导出的文件名 mysqldump -u wcnc -p smgp_apps_wcnc > ...
- 【转】 如何利用Cocos2d-x开发一个游戏
原文:http://blog.csdn.net/honghaier/article/details/7888592 这个问题的结果应该是一个流程.我将从一些长期的PC端游戏开发经验结合Cocos2d- ...