代码:
public void themeParty(HttpServletResponse response, BScoreQuery query) throws Exception {
//固定的头信息
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
//防止中文乱码
String fileName = URLEncoder.encode(query.getYear() + "年第" + query.getQuarter() + "季度-数据", "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-Disposition", "attachment;filename*=UTF-8''" + fileName + ".xlsx");
try {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("poi/" + "数据导出--模板.xlsx");
List<BMassesInfoImportData> dataList = bService.selectExportDataList(query);
EasyExcel.write(response.getOutputStream(), BMassesInfoImportData.class)
.withTemplate(inputStream)
.sheet()
.doFill(dataList);
} catch (Exception FileNotFoundException) {
EasyExcel.write(response.getOutputStream()).sheet("数据");
}
}
doFill 为数据追加
数据追加需要配合模板中配置参数,如果为list参数,则{**.**参数名}
如果为map则直接{参数名}
doWrite 会将表头一起写入