Java使⽤Poi-tlword模板导出word 1.导⼊依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.7.3</version>
</dependency>
2.新建⼀个word,制作导出模板
模板放⼊ resource/static/word/template⽂件夹下
3.编写⼯具类
⼯具类--WordExportServer.java
public class WordExportServer {
/**
* 导出word
**/
public static void export(WordExportData wordExportData) throws IOException {
HttpServletResponse Response();
OutputStream out = OutputStream();;
XWPFTemplate template =null;
try{
ClassPathResource classPathResource = new TemplateDocPath());
String resource = URL().getPath();
resource= PdfUtil1.handleFontPath(resource);
//渲染表格
HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();
Configure config = wBuilder().TableDataField(), policy).build();
template = XWPFTemplatepile(resource, config).
WordData());
String fileName=getFileName(wordExportData);
/** ===============⽣成word到设置浏览默认下载地址=============== **/
// 设置强制下载不打开
response.setContentType("application/force-download");
// 设置⽂件名
response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
template.write(out);
}catch (Exception e){
e.printStackTrace();
}finally {
out.flush();
out.close();
template.close();
}
}
/**
* 获取导出下载的word名称
* @param wordExportData
* @return java.lang.String
**/
public static String getFileName(WordExportData wordExportData){
if(null !=FileName()){
FileName()+".docx";
}
return System.currentTimeMillis()+".docx";
}
}
word数据包装类--WordExportData .java
@Data
public class WordExportData {
/**
* word模板路径(static/wordTemplate/dealerListDocTemplate.docx)
**/
private String templateDocPath;
/**
* word填充数据(key值与模板中的key值要保持⼀致)
**/
private Map<String,Object> wordData;
/**
* word表格数据key值
**/
private String tableDataField;
/**
* word导出后的⽂件名(不填则⽤当前时间代替)
**/
private String fileName;
private HttpServletResponse response;
}
@RequestMapping("/printWord")
public void printWord(HttpServletRequest request, HttpServletResponse response) throws IOException{
String[] Parameter("ids").split(";");
List<DealerDto> DealerListByIds(ids);
Map<String,Object> docData=new HashMap<>(3);
docData.put("detailList",goodsDataList);
docData.put("title",标题);
docData.put("subTitle",副标题);
WordExportData wordExportData=new WordExportData();
wordExportData.setResponse(response);
wordExportData.setTableDataField("detailList");
wordExportData.setTemplateDocPath(DEALER_DOC_TEMPLATE_PATH);//副本存放路径
wordExportData.setWordData(docData);
}
5.前端调⽤
var ids = [];
for (var index in checkData) {
ids.push(checkData[index].id);
}
var batchIds = ids.join(";");
前端页面模板
layer.close(index);
window.location.href =
'/goods/printWord?ids=' + batchIds;
});
6.总结
优点:使⽤⽅法很简单,使⽤⼯具类的⽅法,⽅便复⽤于其他模块。使⽤者不需要关注word的复杂样式(可直接在模板中编辑好),只需要将数据包装好就⾏了。缺点:在其他模块中使⽤,可能需要编辑新的模板word。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。