@Slf4j
public class ExcelExportUtil {
private ExcelExportUtil() {
}
public static void writeExcel(HttpServletResponse response, List<ExportEquipmentVO> data, String fileName, String sheetName, Object object) throws Exception {
if (CollUtil.isEmpty(data)) {
log.warn("Excel导出数据为空!");
}
EasyExcelFactory.write(getOutputStream(fileName, response), object.getClass())
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
.sheet(sheetName)
.doWrite(data);
}
private static OutputStream getOutputStream(String fileName, HttpServletResponse response) throws Exception {
try {
fileName = URLEncoder.encode(fileName, "UTF-8");
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ";filename*=utf-8''" + fileName + ".xlsx");
response.setHeader("Pragma", "public");
response.setHeader("Cache-Control", "no-store");
response.addHeader("Cache-Control", "max-age=0");
return response.getOutputStream();
} catch (IOException e) {
log.error("导出excel表格失败,失败原因{}", e.getMessage());
throw new Exception("导出excel表格失败!", e);
}
}
}
|