【文件】使用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包导入工程 ...
随机推荐
- 任意目录下启动tomcat
DOS中启动tomcat 1.将tomcat的bin目录添加到Path变量中 2.添加catalina_home变量 3.命令行输入catalina run ojbk
- git命令行界面
学习目标:掌握git命令行界面的操作.掌握最基本的clone add commit push pull操作. 先下载客户端:http://github-windows.s3.amazonaws.com ...
- HNOI2017礼物
礼物 这估计是最水,最无脑的一道题了 首先发现总和最接近时答案最小 发现答案就是\((\sum_{i=1}^{n}a[i]^2+b[i]^2)-2*max(\sum_{i=1}^{n}a[i]*b[i ...
- emwin之2D图形绘制问题
@2018-09-03 [问题] 在 WM_PAINT 消息分支里绘制2D图形可以正常显示,而在外部函数或按钮按下事件的响应消息分支下等处,绘制2D图形则不显示. [解决] 在除消息WM_PAINT分 ...
- 洛谷 P2764 最小路径覆盖问题 解题报告
P2764 最小路径覆盖问题 问题描述: 给定有向图\(G=(V,E)\).设\(P\) 是\(G\) 的一个简单路(顶点不相交)的集合.如果\(V\) 中每个顶点恰好在\(P\) 的一条路上,则称\ ...
- 洛谷P4413 R2
好,这是一道巨水题...... #include <cstdio> using namespace std; typedef long long LL; int main() { LL a ...
- A1029. Median
Given an increasing sequence S of N integers, the median is the number at the middle position. For e ...
- (转)Visual Studio控制台程序输出窗口一闪而过的解决方法
背景:熟悉visiual studio工具的使用 刚接触 Visual Studio的时候大多数人会写个Hello World的程序试一下,有的人会发现执行结束后输出窗口会一闪而过,并没有出现Pres ...
- (转)Java程序员的面试经历和题库
背景:最近我在找工作,前期就像打了鸡血的一样,隔一段时间没有面试,就又松懈了下来,看到别人写的面经,感觉就像打脸一般,以后要多多总结前人的经验,时刻保持压力状态才是. 作者:nuaazhaofeng2 ...
- maven项目检出后报错(包括编译报错和运行报错)的常见检查处理方式
maven项目检出后报错(包括编译报错和运行报错)的常见检查处理方式: 1.更改项目的jdk为我们安装的jdk2.更改build配置里的 output folder 目录为 xxx项目名/target ...