@Slf4j
@UtilityClass
public class EasyExcelUtil {
public void export(HttpServletResponse response, Class columnBean, List dataList, String fileName, String sheetName) {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
try {
String gb2312 = new String(fileName.getBytes("gb2312"), "ISO8859-1");
fileName = gb2312 + "(" + LocalDate.now().toString() + ")";
} catch (UnsupportedEncodingException e) {
log.error("编码异常", e);
}
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".csv");
try {
sheetName = new String(sheetName.getBytes(), "utf-8");
sheetName = sheetName + "(" + LocalDate.now().toString() + ")";
} catch (UnsupportedEncodingException e) {
log.error("编码异常", e);
}
try {
EasyExcel.write(response.getOutputStream(), columnBean).sheet(sheetName).doWrite(dataList);
} catch (IOException e) {
log.error("导出异常", e);
}
}
}