利用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 ...
随机推荐
- 配置nginx,支持php的pathinfo路径模式
nginx模式默认是不支持pathinfo模式的,类似index.php/index形式的url会被提示找不到页面.下面的通过正则找出实际文件路径和pathinfo部分的方法,让nginx支持path ...
- Java SAX Parser
SAX is an abbreviation and means "Simple API for XML". A Java SAX XML parser is a stream o ...
- define定义方法
define('a',function(){ ; return x; }); define('b',['a'],function(a){ ; return y+a; }); M.use(['b'],f ...
- jquery基础篇
1.jquery选择器和css选择器的关系: jquery的选择器是源于css,jquery支持css1和css2的全部和css3 的部分选择器,同时它也有少量独有的选择器. 2.常用jquery选择 ...
- CodeIgniter网站静态化管理系统
CodeIgniter本身带了一套静态化系统 使用方法如下: $this->output->cache( 3 );//每三分钟重新生成一次静态页面 不过这个在系统化的编辑中不方便管理 由此 ...
- 菜单设计器(Menu Designer)及其B/S,C/S双重实现(B/S开源)
ERP/MIS开发 菜单设计器(Menu Designer)及其B/S,C/S双重实现(B/S开源) 一直从事ERP/MIS的开发工作,今天来展现一下菜单设计器的设计,及其用途,并对B/S部分代码 ...
- ecshop在线手册前言及程序结构
该在线手册是有模版堂转载而来:仅供参考 一.前言 为什么我们ecshop模板堂要重制ecshop在线手册呢?因为目前网上的一些教程有些是比较老的,有些是不全面的,官方的手册也已经很久没有更 新,很多刚 ...
- linux系统基础(一)
Linux简介与安装Unix ;windows; linux; apple(mac) linux=kernel (内核)=OSlinux全是文件============================ ...
- Volley使用指南第一回(来自developer.android)
最近闲来想看看android网络方面的东西.google在2013年发布了一个叫做Volley的网络请求框架,我看了一下官网,居然在training里面就有教程.首先,英文的东西看着 还是挺不爽的,特 ...
- maven配置编译路径
在build标签下添加 <build> <sourceDirectory>src/main/java</sourceDirectory> <resources ...