package com.whyc.util;
|
|
import com.deepoove.poi.XWPFTemplate;
|
|
import java.io.FileOutputStream;
|
import java.io.IOException;
|
import java.util.HashMap;
|
import java.util.List;
|
|
public class WordUtil {
|
|
public static void fillValue(String inFilePath, String outFilePath, List valueList) throws IOException {
|
HashMap<String,Object> hashMap = new HashMap<>();
|
hashMap.put("fzr", valueList.get(0));
|
hashMap.put("bz", valueList.get(1));
|
hashMap.put("zz", valueList.get(2));
|
hashMap.put("pb", valueList.get(3));
|
XWPFTemplate template = XWPFTemplate.compile(inFilePath).render(hashMap);
|
template.write(new FileOutputStream(outFilePath));
|
|
|
/*//获取临时文件
|
File file = new File(filePath);
|
HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();
|
Configure config = Configure.newBuilder().bind("detailList", policy).build();
|
XWPFTemplate template = XWPFTemplate.compile(filePath, config).render(
|
new HashMap<String, Object>() {{
|
put("fzr", "负责人");
|
put("bz", "编制");
|
put("zz", "制造");
|
put("pb", "品保");
|
}}
|
);
|
//生成文件名
|
Long time = new Date().getTime();
|
// 生成的word格式
|
String fileName = time + ".docx";
|
System.out.println("文件名:" + fileName);
|
//=================生成word到设置浏览默认下载地址=================
|
//解决文件下载名称变为 ____下划线的BUG
|
//针对IE或者以IE为内核的浏览器:
|
String userAgent = request.getHeader("User-Agent");
|
if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
|
fileName = java.net.URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString());
|
} else {
|
//非IE浏览器:
|
fileName = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
|
// 设置强制下载不打开
|
response.setContentType("application/force-download");
|
// 设置文件名
|
response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
|
response.setCharacterEncoding("UTF-8");
|
OutputStream out = response.getOutputStream();
|
template.write(out);
|
out.flush();
|
out.close();
|
template.close();
|
}*/
|
}
|
}
|