【文件】使用word的xml模板生成.doc文件
一、编辑模板 替换地方以变量标记如“案件编号”可写成{caseNo}
template.xml
二、准备数据 以HashMap封装数据,原理是替换模板中的变量
三、替换操作
选择输出位置:writePath
WordUtil.printWordbyXMLWithOutputPath(templateFile, writePath, filename, dataMap);
/**
* 打印word文档(传入参数需要传入输出文件的保存路径的)
* @param templateFile
* @param writeFilePath
* @param writeFileName
* @param analysisData 要替换的数据,map的形式传递
* @throws Exception
*/
public static void printWordbyXMLWithOutputPath(String templateFile, String writeFilePath,
String writeFileName, Map<String, String> analysisData) throws Exception{
writeFilePath = makePathFile(writeFilePath);
String writeFile = "";
if(writeFilePath.endsWith("\\") || writeFilePath.endsWith("/")){
writeFile += writeFilePath + writeFileName;
}else{
writeFile = writeFilePath + FILE_SEPARATOR + writeFileName;
}
printWordByXML(templateFile, writeFile, analysisData);
}
/**
* 打印word文档(输出路径不需要单独指出,保存在writeFile中即可)
* @param templateFile
* @param writeFile
* @param analysisData
* @throws Exception
*/
public static void printWordByXML(String templateFile,
String writeFile, Map<String, String> analysisData) throws Exception{
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader in = null;
FileOutputStream fos = null;
OutputStreamWriter osw = null;
BufferedWriter out = null;
try {
fis = new FileInputStream(templateFile);
isr = new InputStreamReader(fis, "UTF-8");
in = new BufferedReader(isr);
fos = new FileOutputStream(writeFile);
osw = new OutputStreamWriter(fos, "UTF-8");
out = new BufferedWriter(osw);
String s;
while ((s = in.readLine()) != null) {
for (String key : analysisData.keySet()) {
String value = analysisData.get(key);
if (value != null) {
s = s.replace(key, value);
} else {
s = s.replace(key, "");
}
}
out.write(s);
out.newLine();
} } catch (Exception e) {
e.printStackTrace();
} finally{
if(out != null){
out.close();
}
if(osw != null){
osw.close();
}
if(fos != null){
fos.close();
}
if(in != null){
in.close();
}
if(isr != null){
isr.close();
}
if(fis != null){
fis.close();
} // DeleteFile(templateFile);
} }
/**
* 验证目录是否存在,如果不存在,则创建对应目录。
* @param filePathName
*/
public static String makePathFile(String filePathName){
File pathFile = new File(filePathName);
if(!pathFile.exists()){
pathFile.mkdirs();
}
File childFile = new File(pathFile + "\\\\"
+ DateTools.dateToString( new Date(),"yyyyMM"));//xiexiangning
if (!childFile.exists()) {
childFile.mkdir();
}
return childFile + "\\\\";
}
【文件】使用word的xml模板生成.doc文件的更多相关文章
- 使用word模板生成pdf文件
使用word模板生成pdf文件 源码:UserWord
- 根据PDF模板生成PDF文件(基于iTextSharp)
根据PDF模板生成PDF文件,这里主要借助iTextSharp工具来完成.场景是这样的,假如要做一个电子协议,用过通过在线填写表单数据,然后系统根据用户填写的数据,生成电子档的协议.原理很简单,但是每 ...
- java通过FreeMarker模板生成Excel文件之.ftl模板制作
关于怎么通过freemarker模板生成excel的文章很多,关键点在于怎么制作模板文件.ftl 网上的办法是: (1)把Excel模板的格式调好,另存为xml文件 (2)新建一个.ftl文件,把xm ...
- itextsharp利用模板生成pdf文件笔记
iTextSharp是一款开源的PDF操作类库,使用它可以快速的创建PDF文件. 中文参考网站:http://hardrock.cnblogs.com/ http://pdfhome.hope.com ...
- .NET中XML 注释 SandCastle 帮助文件.hhp 使用HTML Help Workshop生成CHM文件
一.摘要 在本系列的第一篇文章介绍了.NET中XML注释的用途, 本篇文章将讲解如何使用XML注释生成与MSDN一样的帮助文件.主要介绍NDoc的继承者:SandCastle. .SandCastle ...
- django生成文件txt、pdf(在生成 PDF 文件之前,需要安装 ReportLab 库)
from django.http import HttpResponse def download_file(request): # Text file #response = HttpRespons ...
- c#创建目录和文件夹,数据写入并生成txt文件
c#创建目录: // 获取程序的基目录.System.AppDomain.CurrentDomain.BaseDirectory // 获取模块的完整路径.System.Diagnostics.Pro ...
- java 根据word xml模板生成word
这里用的是poi相关jar包以及freemarker插值技术实现,poi相关jar包这里不再述说 1,编辑word并保存为xml 2,把xml后缀改为ftl文件 3,前端代码 // alert(jso ...
- Android根据pdf模板生成pdf文件
我们需要生成一些固定格式的pdf文件或者一些报表数据,那么我们可以用 iText包去做. 需要包含的jar包:iText-5.0.6.jar iTextAsian.jar ,怎样jar包导入工程 ...
随机推荐
- mysql 启动和关闭外键约束
在MySQL中删除一张表或一条数据的时候,出现 [Err] 1451 -Cannot delete or update a parent row: a foreign key constraint f ...
- BZOJ1036[ZJOI2008]树的统计——树链剖分+线段树
题目描述 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. QMAX u v ...
- Redis报错总结
MISCONF Redis is configured to save RDB snapshots MISCONF Redis is configured to save RDB snapshots, ...
- MT【218】交点个数
若函数$f(x)=x^3+ax^2+bx+c$有极值点$x_1,x_2$,且$f(x_1)=x_1$,则关于$x$的方程$3(f(x))^2+2af(x)+b=0$的不同实数根个数为_____ 注意到 ...
- 【刷题】LOJ 2863 「IOI2018」组合动作
题目描述 你在玩一个动作游戏.游戏控制器有 \(4\) 个按键,A.B.X 和 Y.在游戏中,你用组合动作来赚金币.你可以依次按这些按键来完成一个组合动作. 这个游戏有一个隐藏的按键序列,可以表示为由 ...
- 自学Zabbix12.2 Zabbix命令-zabbix_get
点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 自学Zabbix12.2 Zabbix命令-zabbix_get 1. zabbix_get概念 ...
- 洛谷 P2672 推销员 解题报告
P2672 推销员 题目描述 阿明是一名推销员,他奉命到螺丝街推销他们公司的产品.螺丝街是一条死胡同,出口与入口是同一个,街道的一侧是围墙,另一侧是住户.螺丝街一共有N家住户,第i家住户到入口的距离为 ...
- Centos6.5之ssh免密码登录配置
Centos6.5之ssh免密码登录配置 centos ssh 免密码登录 0.说明 这里为了方便说明问题,假设有A和B两台安装了centos6.5的主机.目标是实现A.B两台主机分别能够通过ssh免 ...
- A1091. Acute Stroke
One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the re ...
- 2018 ACM 网络选拔赛 北京赛区
A Saving Tang Monk II #include <bits/stdc++.h> using namespace std; ; struct node { int x,y,z, ...