利用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 ...
随机推荐
- HW7.5
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- 【spoj SEQN】【hdu 3439】Sequence
题意: 给出n.m.k 求C(n,k)*H(n-k)%m的值 H(n-k)为错排公式 题解: 先算H(n-k) 计算H(n)有个通式: H(n)=(-1)^n+((-1)^(n-1))n+((-1)^ ...
- BNUOJ 26579 Andrew the Ant
LINK:Andrew the Ant 题意:给一根长度为L的木头,上面有A只蚂蚁[每只蚂蚁给出了初始行走的方向,向左或向右].当两只蚂蚁相碰时,两只蚂蚁就朝相反的方向行走~╮(╯▽╰)╭问的是:最后 ...
- Windows 窗体启动和关闭的事件顺序
本文系转载学习. 对于关注对 Windows 窗体应用程序中引发的每个事件按次序进行处理的开发人员来说,事件引发的顺序特别重要.当某种情况需要小心处理事件时(如重绘窗体的某些部分时),必须知道事件在运 ...
- String的那点小事儿
1.== 比较的是什么? 1 package xupengwei.string; 2 /** 3 * @describe: 4 * @author chenmo-xpw 5 * @version 20 ...
- Oracle DataGuard数据备份方案详解
Oracle DataGuard是一种数据库级别的HA方案,最主要功能是冗灾.数据保护.故障恢复等. 在生产数据库的"事务一致性"时,使用生产库的物理全备份(或物理COPY)创建备 ...
- 树套树专题——bzoj 3110: [Zjoi2013] K大数查询 & 3236 [Ahoi2013] 作业 题解
[原题1] 3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec Memory Limit: 512 MB Submit: 978 Solved: 476 Descri ...
- 实现android activity之间的跳转
android程序一般不会只有一个activity,会碰到activity之间的跳转.以下是使用Intent做应用程序内部的activity做跳转.比如,应用程序第一个activity是: 点击“下一 ...
- Nginx详细配置
#运行用户#user nobody; #启动进程,通常设置成和cpu的数量相等或者2倍于cpu的个数(具体结合cpu和内存).默认为1worker_processes 1; #全局的错误日志和日志 ...
- Ajax防止重复提交
转:http://www.cnblogs.com/jinguangguo/archive/2013/05/20/3086925.html 谈谈防止重复点击提交 首先说说防止重复点击提交是什么意思. ...