EasyExcel导出工具类
public class ExcelUtil {
private static final int RESPONSE_SUCCESS_CODE = 200;
public static void fileExport(List<?> list, HttpServletResponse response, Class<?> clazz) {
try {
writeExcel(response, list, System.currentTimeMillis() + "", System.currentTimeMillis() + "", clazz);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void writeExcel(HttpServletResponse response, List<? extends Object> data, String fileName, String sheetName, Class clazz) throws IOException {
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
WriteFont headWriteFont = new WriteFont();
headWriteFont.setFontHeightInPoints((short)20);
headWriteCellStyle.setWriteFont(headWriteFont);
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
WriteFont contentWriteFont = new WriteFont();
contentWriteFont.setFontHeightInPoints((short)14);
contentWriteCellStyle.setWriteFont(contentWriteFont);
HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
EasyExcel.write(getOutputStream(fileName, response), clazz).registerWriteHandler(horizontalCellStyleStrategy).sheet(sheetName).doWrite(data);
}
private static OutputStream getOutputStream(String fileName, HttpServletResponse response) throws IOException {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
response.setStatus(RESPONSE_SUCCESS_CODE);
return response.getOutputStream();
}
}