easypoi模板导出excel
导包
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.1.0</version>
</dependency>
代码
@RequestMapping(value = "/spendListExport", method = RequestMethod.GET)
public void excel(HttpServletResponse response, HttpServletRequest reques, @RequestParam Map params) throws IOException {
TemplateExportParams exportParams = new TemplateExportParams();
// 设置模板地址
exportParams.setTemplateUrl("report/excel/spendOrderRecord.xlsx");
// 创建工作簿,实现类获取数据
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, spendService.getRecordExport(params));
// 为工作簿添加样式(相同单元格合并)
Map<Integer, int[]> mergeMap = new HashMap<>();
mergeMap.put(0, new int[0]);
PoiMergeCellUtil.mergeCells(workbook.getSheetAt(0), mergeMap, 2);
String downFileName = "消费收银记录" + BaseUtil.toDateText(new Date(), "yyyyMMddHHmm") + ".xlsx";
response.setContentType("application/x-msdownload");
response.setHeader("Content-disposition", "attachment;filename=" + encodeChineseDownloadFileName(reques, downFileName));
workbook.write(response.getOutputStream());
}
/**
* 对文件流输出下载的中文文件名进行编码 屏蔽各种浏览器版本的差异性
*/
public static String encodeChineseDownloadFileName(HttpServletRequest request, String pFileName) {
String agent = request.getHeader("USER-AGENT");
try {
if (null != agent && agent.contains("MSIE")) {
pFileName = URLEncoder.encode(pFileName, "utf-8");
} else {
pFileName = new String(pFileName.getBytes("utf-8"), "iso8859-1");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return pFileName;
}
模板取数据
{{字段名}} 例如{{name}}
当字段类型为Map并且Map的value为List
data 是map 的key,t是遍历list的别名 别名.字段名