利用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)的更多相关文章

  1. 使用word模板生成pdf文件

    使用word模板生成pdf文件 源码:UserWord

  2. 根据PDF模板生成PDF文件(基于iTextSharp)

    根据PDF模板生成PDF文件,这里主要借助iTextSharp工具来完成.场景是这样的,假如要做一个电子协议,用过通过在线填写表单数据,然后系统根据用户填写的数据,生成电子档的协议.原理很简单,但是每 ...

  3. 利用T4模板生成ASP.NET Core控制器的构造函数和参数

    前言 在ASP.NET Core中引入了DI,并且通过构造函数注入参数,控制器中会大量使用DI注入各种的配置参数,如果配置注入的参数比较多,而且各个控制器需要的配置参数都基本一样的话,那么不断重复的复 ...

  4. 利用Python 脚本生成 .h5 文件 代码

    利用Python 脚本生成 .h5 文件 import os, json, argparse from threading import Thread from Queue import Queue ...

  5. 【Linux开发】【DSP开发】利用CCS6.1生成out文件的同时生成bin文件

    [Linux开发][DSP开发]利用CCS6.1生成out文件的同时生成bin文件 标签:[DSP开发] [Linux开发] 尝试在windows上安装的CCS6.1开发AM4378-Linux下的应 ...

  6. django生成文件txt、pdf(在生成 PDF 文件之前,需要安装 ReportLab 库)

    from django.http import HttpResponse def download_file(request): # Text file #response = HttpRespons ...

  7. itextsharp利用模板生成pdf文件笔记

    iTextSharp是一款开源的PDF操作类库,使用它可以快速的创建PDF文件. 中文参考网站:http://hardrock.cnblogs.com/ http://pdfhome.hope.com ...

  8. 【VS外接程序】利用T4模板生成模块代码

    引言 记得第一次做asp.net mvc项目时,可以用model直接生成Html的增删改查页面, 没什么特殊要求都可以不用修改直接用了, 觉得很神奇,效率太高了.后来在做客户端开发时,发现很多模块都是 ...

  9. java通过FreeMarker模板生成Excel文件之.ftl模板制作

    关于怎么通过freemarker模板生成excel的文章很多,关键点在于怎么制作模板文件.ftl 网上的办法是: (1)把Excel模板的格式调好,另存为xml文件 (2)新建一个.ftl文件,把xm ...

随机推荐

  1. PetShop学习第四天

    ASP.NET缓存 1.页输出缓存分为整页缓存和部分页缓存.我们可以通过@OutputCache指令来完成对Web页面的输出缓存.

  2. 关于硬盘和几种RAID

    1 硬盘的基本工作原理 1.1 硬盘部件结构图 1.2 主要参数术语解释 磁头:在与硬盘交换数据的过程 中,读操作远远快于写操作,硬盘厂商开发一种读/写分离磁头. 转速(Rotationl Speed ...

  3. 创建本地CM 离线服务器

    一.包管理工具及CentOS的yum 1.包管理工具如何发现可以用的包 包管理工具依赖一系列软件源,工具下载源的信息存储在配置文件中,其位置随某包管理工具不同而变化 使用yum的RedHat/Cent ...

  4. 各种排序算法代码(C语言版)

    选择排序 #include <stdio.h> /* * 选择排序 * 稳定性:不稳定 * 时间复杂度:O(N^2) **/ void select_sort(int a[], int l ...

  5. Android中使用sqlite笔记

    1.实现SQLiteHelper来在android中使用SQLite.代码如下,来自android官网. public class FeedReaderDbHelper extends SQLiteO ...

  6. Arrays.asList的源码分析

    以前一直很奇怪为什么Arrays.asList的数组不能插入新的数据,后来看了源码发现是因为内部是一个final的数组支持起来的Arraylist,下面贴入源码与分析. 1.先看Arrays的方法 我 ...

  7. MySQL数据库加密与解密

    数据加密.解密在安全领域非常重要.对程序员而言,在数据库中以密文方式存储用户密码对入侵者剽窃用户隐私意义重大. 有多种前端加密算法可用于数据加密.解密,下面我向您推荐一种简单的数据库级别的数据加密.解 ...

  8. cocos2d-x Action

    转自:http://codingnow.cn/cocos2d-x/775.html 从结构图可以看出,动作类的基类是CCAction,通过继承它可以实现很多种动作. CCFiniteTimeActio ...

  9. UITextfield设置Placeholder颜色 控件 内边距、自适应高度

    //创建UITextField对象 UITextField * tf=[[UITextField alloc]init];    //设置Placeholder颜色 [text setAttribut ...

  10. 【Android开发】完美解决Android完全退出程序

    背景:假说有两个Activity, Activity1和Activity2, 1跳转到2,如果要在2退出程序,一般网上比较常见的说法是用 System.exit(0) 或是 android.os.Pr ...