一、编辑模板 替换地方以变量标记如“案件编号”可写成{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文件的更多相关文章

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

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

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

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

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

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

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

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

  5. .NET中XML 注释 SandCastle 帮助文件.hhp 使用HTML Help Workshop生成CHM文件

    一.摘要 在本系列的第一篇文章介绍了.NET中XML注释的用途, 本篇文章将讲解如何使用XML注释生成与MSDN一样的帮助文件.主要介绍NDoc的继承者:SandCastle. .SandCastle ...

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

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

  7. c#创建目录和文件夹,数据写入并生成txt文件

    c#创建目录: // 获取程序的基目录.System.AppDomain.CurrentDomain.BaseDirectory // 获取模块的完整路径.System.Diagnostics.Pro ...

  8. java 根据word xml模板生成word

    这里用的是poi相关jar包以及freemarker插值技术实现,poi相关jar包这里不再述说 1,编辑word并保存为xml 2,把xml后缀改为ftl文件 3,前端代码 // alert(jso ...

  9. Android根据pdf模板生成pdf文件

    我们需要生成一些固定格式的pdf文件或者一些报表数据,那么我们可以用 iText包去做. 需要包含的jar包:iText-5.0.6.jar    iTextAsian.jar ,怎样jar包导入工程 ...

随机推荐

  1. yield与递归的组合运用

  2. git如何拉取指定分支的代码

    问题背景: 新项目还在开发阶段,没有正式对外发布,所以开发同事合并代码到develop上(或者其他名称分支上),而不是到master分支上 通过git拉取代码的时候,默认拉取的是master分支,如下 ...

  3. BZOJ2213[Poi2011]Difference——DP

    题目描述 A word consisting of lower-case letters of the English alphabet ('a'-'z') is given. We would li ...

  4. BZOJ3075[USACO 2013 Mar Gold 3.Necklace]——AC自动机+DP

    题目描述 给你一个长度为n的字符串A,再给你一个长度为m的字符串B,求至少在A中删去多少个字符才能使得B不是A的子串.注:该题只读入A和B,不读入长度,先读入A,再读入B.数据保证A和B中只含小写字母 ...

  5. Dining POJ - 3281

    题意: f个食物,d杯饮料,每个牛都有想吃的食物和想喝的饮料,但食物和饮料每个只有一份 求最多能满足多少头牛.... 解析: 一道简单的无源汇拆点最大流   无源汇的一个最大流,先建立超级源s和超级汇 ...

  6. Codeforces Round #426 (Div. 2) A,B,C

    A. The Useless Toy 题目链接:http://codeforces.com/contest/834/problem/A 思路: 水题 实现代码: #include<bits/st ...

  7. 04 Zabbix4.0系统配置触发器trigger

    点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 04 Zabbix4.0系统配置触发器trigger 请点击查看Zabbix3.0.8版本trig ...

  8. HTML 百度地图API调用示例源码

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  9. 【BZOJ4813】[CQOI2017]小Q的棋盘(贪心)

    [BZOJ4813][CQOI2017]小Q的棋盘(贪心) 题面 BZOJ 洛谷 题解 果然是老年选手了,这种题都不会做了.... 先想想一个点如果被访问过只有两种情况,第一种是进入了这个点所在的子树 ...

  10. 【BZOJ1802】[AHOI2009]checker(动态规划)

    [BZOJ1802][AHOI2009]checker(动态规划) 题面 BZOJ 洛谷 题解 首先自己观察一波,发现如果有相邻两个格子都是红色的话,那么显然可以在任意位置都存在一个跳棋.可以让两个位 ...