easypoi 導出數據為excel報錯信息: The maximum number of cell styles was exceeded. You can define up to 64000styles in a .xlsx workbook
解決方案自定義樣式: 重復利用已有樣式.
package com.hm.hny.common.utils.style; import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; import cn.afterturn.easypoi.excel.export.styler.ExcelExportStylerDefaultImpl; import org.apache.poi.ss.usermodel.*; import org.springblade.core.tool.utils.Func; public class ExcelExportNumberStyler extends ExcelExportStylerDefaultImpl { public CellStyle numberCellStyle; public ExcelExportNumberStyler(Workbook workbook) { super(workbook); createNumberCellStyler();//預生成樣式 } private void createNumberCellStyler() { //自定義樣式不為空時,重復利用 if(Func.isEmpty(numberCellStyle)){ numberCellStyle = workbook.createCellStyle(); numberCellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中 numberCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中 /*numberCellStyle.setDataFormat((short) BuiltinFormats.getBuiltinFormat("0.00"));//格式化 numberCellStyle.setWrapText(true); //換行 */ } } @Override public CellStyle getStyles(boolean noneStyler, ExcelExportEntity entity) { if (entity != null && 10==entity.getType()) { //數值類型用自定義樣式 return numberCellStyle; } return super.getStyles(noneStyler, entity); } }
使用:
ExportParams params = new ExportParams("xx導出", "xx查詢導出", ExcelType.XSSF);
params.setStyle(ExcelExportNumberStyler.class);//設置數字格式自定義導出
此問題導致原因是由於createCellStyle();
workbook.createCellStyle();
最終會到StyleTable#createCellStyle方法
/** * Create a cell style in this style table. * Note - End users probably want to call {@link XSSFWorkbook#createCellStyle()} * rather than working with the styles table directly. * @throws IllegalStateException if the maximum number of cell styles has been reached. */ public XSSFCellStyle createCellStyle() { if (getNumCellStyles() > MAXIMUM_STYLE_ID) { throw new IllegalStateException("The maximum number of Cell Styles was exceeded. " + "You can define up to " + MAXIMUM_STYLE_ID + " style in a .xlsx Workbook"); } int xfSize = styleXfs.size(); CTXf xf = CTXf.Factory.newInstance(); xf.setNumFmtId(0); xf.setFontId(0); xf.setFillId(0); xf.setBorderId(0); xf.setXfId(0); int indexXf = putCellXf(xf); return new XSSFCellStyle(indexXf - 1, xfSize - 1, this, theme); }
MAXIMUM_STYLE_ID = 64000
這個限制是有道理的,不然你創建了很多個CellStyle對象.
朋友們對easyPOi導出由於注解@Excel type = 10 導致樣式沒有重復利用的問題,有更好的辦法,歡迎留言!
