public static <T> void exportDownBoxExcel(String fileName, String sheetName,Class<T> t, String titleName, List<List<String>> headers, HttpServletResponse response, Map<Integer, List<String>> selectMap,List<T> dataList, boolean isExportNullField) {标签:outputStream,sheet,String,列宽行,List,EasyExcel,setColumnWidth,注解,response From: https://www.cnblogs.com/zxy-come-on/p/18554903
ServletOutputStream outputStream = null;
try {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
String fileNameEncode = URLEncoder.encode(fileName, "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileNameEncode + ExcelTypeEnum.XLSX.getValue());
outputStream = response.getOutputStream();
EasyExcelFactory.write(outputStream, t)
.registerWriteHandler(new SelectSheetWriteHandler(selectMap))
//设置表头样式
.registerWriteHandler(getHorizontalCellStyleStrategy())
// 重写AbstractColumnWidthStyleStrategy策略的setColumnWidth方法
.registerWriteHandler(new AbstractColumnWidthStyleStrategy() {
@Override
protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
Sheet sheet = writeSheetHolder.getSheet();
int columnIndex = cell.getColumnIndex();
// 列宽16
sheet.setColumnWidth(columnIndex, (short) 16);
}
})
.excelType(ExcelTypeEnum.XLSX)
// .head(headers)
.sheet(sheetName)
.doWrite(dataList);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (outputStream != null) {
outputStream.flush();
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}