Java用freemarker导出word

1.先用word制作一个模版

 

 

2.将文件存为.xml格式

 

 

 

3.打开xml文件,将模版中需要替换的地方外面加上${}

表格的添加方式:在<w:tr>外面加上<#list rows as list>,

在需要替换的地方改为${list.} 

 

4.改完后,将文件存在沸ftl格式保存

 

java代码如下:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class test2 {

private Configuration configuration = null;

public test2() {
configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
}

public void createDoc(Map<String,Object> dataMap,String fileName) throws UnsupportedEncodingException {
//dataMap 要填入模本的数据文件
//设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载,
//这里我们的模板是放在template包下面
configuration.setClassForTemplateLoading(this.getClass(), "/template");
Template t=null;
try {
//word.ftl为要装载的模板
t = configuration.getTemplate("word.ftl");
} catch (IOException e) {
e.printStackTrace();
}
//输出文档路径及名称
File outFile = new File(fileName);
Writer out = null;
FileOutputStream fos=null;
try {
fos = new FileOutputStream(outFile);
OutputStreamWriter oWriter = new OutputStreamWriter(fos,"UTF-8");
//这个地方对流的编码不可或缺,使用main()单独调用时,应该可以,但是如果是web请求导出时导出后word文档就会打不开,并且包XML文件错误。主要是编码格式不正确,无法解析。
//out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)));
out = new BufferedWriter(oWriter);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}

try {
t.process(dataMap, out);
out.close();
fos.close();
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

//System.out.println("---------------------------");
}
public static void main(String[] args) throws UnsupportedEncodingException {;

Map<String, Object> map = new HashMap<String, Object>();
map.put("projectCode", "BY2019071005");
// 项目名称
map.put("projectName", "广州市白云区城市管理机械化作业管理中心机动车保险采购项目");
// 项目概述
map.put("projectNature", "新建");
map.put("applyGovName", "广州市白云区政府政务管理办公室");
map.put("applyGovPerson", "李伟");
map.put("pmName", "李伟");

map.put("constructionObjectives", "BY2019071005");
map.put("investmentTotal", "108");
map.put("projectCycle", "12");
map.put("projectDateBg", "2018年08月");
map.put("projectDateEd", "2019年08月");
List<Map<String, Object>> newsList=new ArrayList<Map<String,Object>>();
for(int i=1;i<=10;i++){
Map<String, Object> list=new HashMap<String, Object>();
list.put("adsada", "王"+i);
list.put("bdasdas", "137"+i);
list.put("cdadasdsa", "年龄"+i);
newsList.add(list);
}
map.put("rows", newsList);
map.put("financialFrom", "项目建设总投资163万元,由区政府财政局拨款投资建设。");
test2 mdoc = new test2();
mdoc.createDoc(map, "/Users/kongjj/Desktop/v4.docx");//导出的文件路径
}
}


需要导入maven:
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>

 

模版的文件word.ftl的路径如下 

 

 

 

导出word的效果: 

 

posted @ 2019-07-15 14:22  JasonKong  阅读(115)  评论(0编辑  收藏  举报