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=wordExportData.getResponse();
OutputStream out = response.getOutputStream();;
XWPFTemplate template =null;
try{
ClassPathResource classPathResource = new ClassPathResource(wordExportData.getTemplateDocPath());
String resource = classPathResource.getURL().getPath();
resource= PdfUtil1.handleFontPath(resource);
//渲染表格
HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();
Configure config = Configure.newBuilder().bind(wordExportData.getTableDataField(), policy).build();
template = XWPFTemplate.compile(resource, config).
render(wordExportData.getWordData());
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 !=wordExportData.getFileName()){
return wordExportData.getFileName()+".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;
}
4.controller層調用
@RequestMapping("/printWord")
public void printWord(HttpServletRequest request, HttpServletResponse response) throws IOException{
String[] ids=request.getParameter("ids").split(";");
List<DealerDto> goodsDataList=goodsService.getDealerListByIds(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);
WordExportServer.export(wordExportData);
}
5.前端調用
var ids = [];
for (var index in checkData) {
ids.push(checkData[index].id);
}
var batchIds = ids.join(";");
layer.confirm('確定下載選中的數據嗎?', function (index) {
layer.close(index);
window.location.href =
'/goods/printWord?ids=' + batchIds;
});
6.總結
優點:使用方法很簡單,使用工具類的方法,方便復用於其他模塊。使用者不需要關注word的復雜樣式(可直接在模板中編輯好),只需要將數據包裝好就行了。
缺點:在其他模塊中使用,可能需要編輯新的模板word。