首页 技术 正文
技术 2022年11月21日
0 收藏 909 点赞 2,290 浏览 3893 个字

                  利用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的相关导出功能。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,026
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,517
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,364
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,145
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,779
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,856