1. 程式人生 > 實用技巧 >開源easyExcel應用

開源easyExcel應用

首先引入excel工具

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.6</version>
</dependency>
入參需傳入 HttpServletResponse response
定義檔名稱 
StringBuilder displayName = new StringBuilder();
displayName.append("AGY_AgentInfoReport");
displayName.append(agentInfoInVO.getUserCode());
displayName.append("_");
displayName.append(DateUtil.getCurrentDateTimeStr().replaceAll(":", "").replaceAll(" ", "_"));
displayName.append(".xlsx");

定義返回體
private void initResponse(HttpServletResponse response, String displayName){
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition", "attachment;filename="
+ displayName);
}
直接傳入引數 進行excel生成 注:agentInfoExcelModelList為模板資料
initResponse(response, displayName.toString());
EasyExcel.write(response.getOutputStream(), AgentInfoExcelModel.class)
.sheet()
.doWrite(agentInfoExcelModelList);

參考網址:
https://www.yuque.com/easyexcel/doc/write#1bea3540