固定的WORD模板导出文档
1.打开word文档,另存为xml格式,并切在word文档位置赋值上你想取的值。
2.直接休改xml后缀为ftl格式。并打开检查里面赋值情况
能搜到自己辅助的代码,就算模板制作成功。
3.pom文件
org.freemarker
freemarker-gae
2.3.27-incubating
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.0.1</version>
</dependency>
4.工具类代码
public class toWord {
private static Configuration configuration = null;
//注意这个地方的path是Word文档所在的路径
//private static final String templateFolder = toWord.class.getClassLoader().getResource(“demo”).getPath() + “word/”;
static {
configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
try {
String demo = Thread.currentThread().getContextClassLoader().getResource("demo").getFile();
configuration.setDirectoryForTemplateLoading(new File(demo));
} catch (IOException e) {
e.printStackTrace();
}
}
private toWord() {
throw new AssertionError();
}
public static void exportMillCertificateWord(HttpServletRequest request, HttpServletResponse response, Map map) throws IOException {
Template freemarkerTemplate = configuration.getTemplate("测试.ftl");
System.out.println(freemarkerTemplate);
File file = null;
InputStream fin = null;
ServletOutputStream out = null;
try {
// 调用工具类的createDoc方法生成Word文档
file = createDoc(map, freemarkerTemplate);
fin = new FileInputStream(file);
response.setCharacterEncoding("utf-8");
response.setContentType("application/msword");
// 设置浏览器以下载的方式处理该文件名
String fileName = "授权委托书.doc";
response.setHeader("Content-Disposition", "attachment;filename=".concat(String.valueOf(URLEncoder.encode(fileName, "UTF-8"))));
out = response.getOutputStream();
byte[] buffer = new byte[512];
// 缓冲区
int bytesToRead = -1;
// 通过循环将读入的Word文件的内容输出到浏览器中
while ((bytesToRead = fin.read(buffer)) != -1) {
out.write(buffer, 0, bytesToRead);
}
} finally {
if (fin != null) {
fin.close();
}
if (out != null) {
out.close();
}
if (file != null) {
file.delete();
}
}
}
private static <Writer> File createDoc(Map<?, ?> dataMap, Template template) {
String name = "模板生成Word.doc";
File f = new File(name);
Template t = template;
try {
// 这个地方不能使用FileWriter因为需要指定编码类型否则生成的Word文档会因为有无法识别的编码而无法打开
OutputStreamWriter w = new OutputStreamWriter(new FileOutputStream(f), "utf-8");
t.process(dataMap, w);
w.close();
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
return f;
}
}
5.controller
@RestController
@RequestMapping(value = “/re”)
public class ReportController {
@RequestMapping("/report")
public void report(HttpServletRequest request , HttpServletResponse response){
try{
// 循环数据
/* List<Object> list = new ArrayList<>();
for (int i = 0; i < 100; i++) {
Map<String,Object> data = new HashMap<>();
data.put("a1", (int)(Math.random()*100) );
data.put("a2", (int)(Math.random()*100) );
data.put("a3", (int)(Math.random()*100) );
data.put("a4", (int)(Math.random()*100) );
data.put("a5", (int)(Math.random()*100) );
list.add(data);
}*/
// 表格使用的数据
Map map = new HashMap();
//map.put("data",list);
map.put("title","java基于模板导出excel表格");
map.put("val","演示合并单元格的数据显示");
// 获取模板文件
String a="E:\\demoMY\\src\\main\\resources\\demo\\xx.xlsx";
// String a= Thread.currentThread().getContextClassLoader().getResource("demo/man.xlsx").getFile();
System.out.println(a);
InputStream is = new FileInputStream(a);
// 实例化 XLSTransformer 对象
XLSTransformer xlsTransformer = new XLSTransformer();
// 获取 Workbook ,传入 模板 和 数据
Workbook workbook = xlsTransformer.transformXLS(is, map);
// Workbook workbook = xlsTransformer.transformXLS(is,map);
// 设置文件名
response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("报表.xlsx" ,"UTF-8"));
// 写出文件
OutputStream os = new BufferedOutputStream( response.getOutputStream() );
// 输出
workbook.write(os);
// 关闭和刷新管道,不然可能会出现表格数据不齐,打不开之类的问题
is.close();
os.flush();
os.close();
}catch (Exception e){
}
}
}