easyexcel在一个excel中建立多个sheet

参考文档:【EasyExcel 单个多个sheet文件导入导出,通用超简单】

controller

@PostMapping("/xxxx")
public void export(HttpServletResponse response,@RequestBody XXXX xxx){
	xxxIService.export(response,request);

实现类

List list0 = ....
List list1 = ...
String fileName = FileUtils.encodeFileName("文件名_"+System.currentTimeMillis() + ".xls");
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition","attachment;filename*=utf-8''" + fileName);
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).registerWriteHandler(new AutoHeadColumnWithStyleStrategy()).build();
WriteSheet writeSheet1 = EasyExcel.writeSheet(0,"sheet1kkkk").head(XXX0.class).build();
WriteSheet writeSheet2 = EasyExcel.writeSheet(1,"sheet2kkk").head(XXX1.class).build();
excelWriter.write(list0,writeSheet1);
excelWriter.write(list1,writeSheet2);
excelWriter.finish();
response.flushBuffer();

其中AutoHeadColumnWithStyleStrategy是一个自定义的可以调整excel列宽的工具类,网上有好多,按需抄一个就好
其中FileUtils的encodeFileName如下

public static String encodeFileName(String fileName) {
	String name = fileName;
	try{
		name = URLEncoder.encode(name,"utf-8");
	} catch(UnsupportedEncodingException e){
		log.error("URLEncoder编码异常",e);
	}
	return name;
}

你可能感兴趣的:(工作总结,excel)