【文件】使用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包导入工程 ...
随机推荐
- SP5973 SELTEAM - Selecting Teams
SP5973 SELTEAM - Selecting Teams [题目描述] 他已经有 n 个心仪的妹子了,但随着时间的流逝,只有 m(1<=m<=k)个直伴随在他的身边,而小小迪发现他 ...
- Nginx REWRITE阶段
rewrite_log on ;#每次rewrite都会记录一条记录在errorlog里 error_log logs/xxx.log notice; location /first { rewrit ...
- MyBatis:一对多关联查询
MyBatis从入门到放弃四:一对多关联查询 前言 上篇学习了一对一关联查询,这篇我们学习一对多关联查询.一对多关联查询关键点则依然是配置resultMap,在resultMap中配置collecti ...
- spring cloud 入门系列一:初识spring cloud
最近看到微服务很火,也是未来的趋势, 所以就去学习下,在dubbo和spring cloud之间我选择了从spring cloud,主要有如下几种原因: dubbo主要专注于微服务中的一个环节--服务 ...
- Mysterious Bacteria LightOJ - 1220
题意: 给出一个数x 求 x = bp 的p的最大值 解析: 算术基本定理 分解质因数 任何一个数x都可以表示为 x == p1a1 * p2a2 * ````` * pnan 即 b ...
- hdu 4348 To the moon (主席树 区间更新)
链接: http://acm.hdu.edu.cn/showproblem.php?pid=4348 题意: 4种操作: C l r c 区间[l,r]加c,时间+1 Q l r 询问当前时 ...
- Matplotlib python 基本用法
1.简单的绘制函数 import matplotlib.pyplot as plt import numpy as np x = np.linspace(-1, 1, 50) y1 = x + 1 p ...
- Android ListView 列表视图
列表显示的三个元素 ListView : 用来显示列表的View Adapter : 适配器 用来把数据映射到ListView 上的中介 Data: 数据 将被映射的字符串,图片或者基本组件等资源 根 ...
- [hgoi#2019/2/18]比较水
T1--调换纸牌(card) Alex有 n张纸牌,每张纸牌上都有一个值ai,Alex把这些纸牌排成一排,希望将纸牌按值从小到大的顺序排好.现在他把这个任务交给你,你只能进行一种操作:选中一张牌,然后 ...
- 3分钟上手log4net
1. config里 <log4net> <appender name="ConsoleAppender" type="log4net.Appender ...