This article is "Using poi to generate word documents based on templates and convert them into PDF files" follow-up to solve the processing method of incoming files as doc documents or docx
/**
* 根据模板生成word
*
* @param path 模板的路径
* @param params 需要替换的参数
* @param tableList 需要插入的参数
* @param fileName 生成word文件的文件名
* @param response
*/
public void getWord(String path, Map<String, Object> params, List<String[]> tableList, String fileName,
HttpServletResponse response) throws Exception {
//模板文件路径
String modelFilePath = PathConfig.temporaryFilePath+PathConfig.imgPath;
//临时文件路径
String temporaryFilePath = PathConfig.temporaryFilePath+PathConfig.temporaryPath;
String filePath = modelFilePath+path;
File file = new File(filePath);
InputStream is = new FileInputStream(file);
try{
//解析docx,如果异常就用doc解析
CustomXWPFDocument docx = new CustomXWPFDocument(is);
docx(docx,params,tableList,fileName,temporaryFilePath,response);
}catch (Exception e){
//解析doc
is = new FileInputStream(file);
HWPFDocument doc = new HWPFDocument(is);
doc(doc,params,tableList,fileName,temporaryFilePath,response);
}
this.close(is);
}
/**
* 解析doc类型文件
* @param params
* @param tableList
* @param fileName
* @param temporaryFilePath
* @param response
* @throws Exception
*/
public void doc(HWPFDocument doc, Map<String, Object> params, List<String[]> tableList, String fileName, String temporaryFilePath,
HttpServletResponse response) throws Exception{
Range range = doc.getRange();
for (Map.Entry<String, Object> entry : params.entrySet()) {
range.replaceText(entry.getKey(), entry.getValue()+"");
}
OutputStream os = null;
fileName = java.net.URLDecoder.decode(fileName, "UTF-8");
os = new FileOutputStream(temporaryFilePath + fileName);
doc.write(os);
try {
response.setContentType("application/xls");
response.addHeader("Content-Disposition",
"attachment;filename=" + new String(fileName.getBytes("UTF-8"), "iso-8859-1"));
InputStream input = new FileInputStream(temporaryFilePath + fileName);
byte buffBytes[] = new byte[1024];
OutputStream as = response.getOutputStream();
int read = 0;
while ((read = input.read(buffBytes)) != -1) {
as.write(buffBytes, 0, read);
}
as.flush();
as.close();
input.close();
} catch (IOException ex) {
ex.printStackTrace();
}
this.close(os);
//删除临时文件
File f = new File(temporaryFilePath + fileName);
f.delete();
}
/**
* 解析docx类型文件
* @param params
* @param tableList
* @param fileName
* @param temporaryFilePath
* @param response
* @throws Exception
*/
public void docx(CustomXWPFDocument docx, Map<String, Object> params, List<String[]> tableList, String fileName, String temporaryFilePath,
HttpServletResponse response) throws Exception{
this.replaceInPara(docx, params); // 替换文本里面的变量
this.replaceInTable(docx, params, tableList); // 替换表格里面的变量
OutputStream os = null;
fileName = java.net.URLDecoder.decode(fileName, "UTF-8");
os = new FileOutputStream(temporaryFilePath + fileName);
docx.write(os);
try {
response.setContentType("application/xls");
response.addHeader("Content-Disposition",
"attachment;filename=" + new String(fileName.getBytes("UTF-8"), "iso-8859-1"));
InputStream input = new FileInputStream(temporaryFilePath + fileName);
byte buffBytes[] = new byte[1024];
OutputStream as = response.getOutputStream();
int read = 0;
while ((read = input.read(buffBytes)) != -1) {
as.write(buffBytes, 0, read);
}
as.flush();
as.close();
input.close();
} catch (IOException ex) {
ex.printStackTrace();
}
this.close(os);
//删除临时文件
File f = new File(temporaryFilePath + fileName);
f.delete();
}
Reference link: https://www.cnblogs.com/mr-wuxiansheng/p/6131494.html
Main reference method:
//获取模板文件的目录地址
String fileDir = new File(base.getFile(), "../../../../../../doc/").getCanonicalPath();
//获取模板文件
File demoFile=new File(fileDir + "/1.doc");
FileInputStream in = new FileInputStream(demoFile);
//doc文件转换
HWPFDocument hdt = new HWPFDocument(in);
//替换读取到的word模板内容的指定字段
Range range = hdt.getRange();
Map<String, String> map = new HashMap<String, String>();
map.put("$PMBD$", goodsName);
map.put("$PMKSSJ$", dateFormater.format(startTime));
map.put("$MSRHP$", brandNo);
map.put("$PMCJJ$", numberToHanZiUtility.number2CNMontrayUnit(dealPrice));
map.put("$PMYJ$", numberToHanZiUtility.number2CNMontrayUnit(clientCommison));
map.put("$HJ$", numberToHanZiUtility.number2CNMontrayUnit(totalPrice));
map.put("$PMCJJXX$", dealPrice + "");
map.put("$PMYJXX$", clientCommison + "");
map.put("$HJXX$", totalPrice + "");
map.put("$PMJSSJ$", dateFormater.format(endTime));
//替换内容
for (Map.Entry<String,String> entry:map.entrySet()) {
range.replaceText(entry.getKey(),entry.getValue());
}