EasyExcel 非注解方式设置列宽行高的几种实现方式

 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) {
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();
}
}

}
posted @ 2024-11-19 15:10  星空物语之韵  阅读(4)  评论(0编辑  收藏  举报