From a147f714622322e48245331751b513cae07b72f9 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Tue, 26 Oct 2021 10:40:25 +0800 Subject: [PATCH] =?UTF-8?q?*=20=E4=BF=AE=E5=A4=8D=E5=86=99=E5=85=A5?= =?UTF-8?q?=E7=9A=84=E6=80=A7=E8=83=BD=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../excel/context/WriteContextImpl.java | 11 ++--- .../alibaba/excel/util/WriteHandlerUtils.java | 43 +++++++++++-------- .../executor/AbstractExcelWriteExecutor.java | 9 ++-- .../write/executor/ExcelWriteAddExecutor.java | 33 ++++++++------ .../impl/FillStyleCellWriteHandler.java | 5 +-- update.md | 3 ++ 6 files changed, 60 insertions(+), 44 deletions(-) diff --git a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java index f31705f7..1c3727bf 100644 --- a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java +++ b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java @@ -228,9 +228,9 @@ public class WriteContextImpl implements WriteContext { + newRowIndex; i++, relativeRowIndex++) { WriteHandlerUtils.beforeRowCreate(this, newRowIndex, relativeRowIndex, Boolean.TRUE); Row row = WorkBookUtil.createRow(writeSheetHolder.getSheet(), i); - WriteHandlerUtils.afterRowCreate(this, row, relativeRowIndex, Boolean.TRUE); + WriteHandlerUtils.afterRowCreate(this, row, i, relativeRowIndex, Boolean.TRUE); addOneRowOfHeadDataToExcel(row, excelWriteHeadProperty.getHeadMap(), relativeRowIndex); - WriteHandlerUtils.afterRowDispose(this, row, relativeRowIndex, Boolean.TRUE); + WriteHandlerUtils.afterRowDispose(this, row, i, relativeRowIndex, Boolean.TRUE); } } @@ -254,13 +254,14 @@ public class WriteContextImpl implements WriteContext { Cell cell = row.createCell(columnIndex); - WriteHandlerUtils.afterCellCreate(this, cell, head, relativeRowIndex, Boolean.TRUE, excelContentProperty); + WriteHandlerUtils.afterCellCreate(this, cell, row, head, columnIndex, relativeRowIndex, Boolean.TRUE, + excelContentProperty); WriteCellData writeCellData = new WriteCellData<>(head.getHeadNameList().get(relativeRowIndex)); cell.setCellValue(writeCellData.getStringValue()); - WriteHandlerUtils.afterCellDispose(this, writeCellData, cell, head, relativeRowIndex, Boolean.TRUE, - excelContentProperty); + WriteHandlerUtils.afterCellDispose(this, writeCellData, cell, row, head, columnIndex, relativeRowIndex, + Boolean.TRUE, excelContentProperty); } } diff --git a/src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java b/src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java index 78ce8d8e..1ea362b0 100644 --- a/src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java +++ b/src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java @@ -133,16 +133,16 @@ public class WriteHandlerUtils { } } - public static void afterCellCreate(WriteContext writeContext, Cell cell, Head head, Integer relativeRowIndex, - Boolean isHead, ExcelContentProperty excelContentProperty) { + public static void afterCellCreate(WriteContext writeContext, Cell cell, Row row, Head head, + Integer columnIndex, Integer relativeRowIndex, Boolean isHead, ExcelContentProperty excelContentProperty) { List handlerList = writeContext.currentWriteHolder().writeHandlerMap().get(CellWriteHandler.class); if (handlerList == null || handlerList.isEmpty()) { return; } CellWriteHandlerContext context = new CellWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), - writeContext.writeSheetHolder(), writeContext.writeTableHolder(), cell.getRow(), cell, - cell.getColumnIndex(), relativeRowIndex, head, null, null, isHead, excelContentProperty); + writeContext.writeSheetHolder(), writeContext.writeTableHolder(), row, cell, columnIndex, relativeRowIndex, + head, null, null, isHead, excelContentProperty); for (WriteHandler writeHandler : handlerList) { if (writeHandler instanceof CellWriteHandler) { ((CellWriteHandler)writeHandler).afterCellCreate(context); @@ -150,8 +150,9 @@ public class WriteHandlerUtils { } } - public static void afterCellDataConverted(WriteContext writeContext, WriteCellData cellData, Cell cell, - Head head, Integer relativeRowIndex, Boolean isHead, ExcelContentProperty excelContentProperty) { + public static void afterCellDataConverted(WriteContext writeContext, WriteCellData cellData, Cell cell, Row row, + Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead, + ExcelContentProperty excelContentProperty) { List handlerList = writeContext.currentWriteHolder().writeHandlerMap().get(CellWriteHandler.class); if (handlerList == null || handlerList.isEmpty()) { @@ -159,8 +160,8 @@ public class WriteHandlerUtils { } List> cellDataList = cellData == null ? null : ListUtils.newArrayList(cellData); CellWriteHandlerContext context = new CellWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), - writeContext.writeSheetHolder(), writeContext.writeTableHolder(), cell.getRow(), cell, - cell.getColumnIndex(), relativeRowIndex, head, cellDataList, cellData, isHead, excelContentProperty); + writeContext.writeSheetHolder(), writeContext.writeTableHolder(), row, cell, + columnIndex, relativeRowIndex, head, cellDataList, cellData, isHead, excelContentProperty); for (WriteHandler writeHandler : handlerList) { if (writeHandler instanceof CellWriteHandler) { ((CellWriteHandler)writeHandler).afterCellDataConverted(context); @@ -168,14 +169,17 @@ public class WriteHandlerUtils { } } - public static void afterCellDispose(WriteContext writeContext, WriteCellData cellData, Cell cell, Head head, - Integer relativeRowIndex, Boolean isHead, ExcelContentProperty excelContentProperty) { + public static void afterCellDispose(WriteContext writeContext, WriteCellData cellData, Cell cell, Row row, + Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead, + ExcelContentProperty excelContentProperty) { List> cellDataList = cellData == null ? null : ListUtils.newArrayList(cellData); - afterCellDispose(writeContext, cellDataList, cell, head, relativeRowIndex, isHead, excelContentProperty); + afterCellDispose(writeContext, cellDataList, cell, row, head, columnIndex, relativeRowIndex, isHead, + excelContentProperty); } public static void afterCellDispose(WriteContext writeContext, List> cellDataList, Cell cell, - Head head, Integer relativeRowIndex, Boolean isHead, ExcelContentProperty excelContentProperty) { + Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead, + ExcelContentProperty excelContentProperty) { List handlerList = writeContext.currentWriteHolder().writeHandlerMap().get(CellWriteHandler.class); if (handlerList == null || handlerList.isEmpty()) { @@ -186,8 +190,8 @@ public class WriteHandlerUtils { cellData = cellDataList.get(0); } CellWriteHandlerContext context = new CellWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), - writeContext.writeSheetHolder(), writeContext.writeTableHolder(), cell.getRow(), cell, - cell.getColumnIndex(), relativeRowIndex, head, cellDataList, cellData, isHead, excelContentProperty); + writeContext.writeSheetHolder(), writeContext.writeTableHolder(), row, cell, columnIndex, relativeRowIndex, + head, cellDataList, cellData, isHead, excelContentProperty); for (WriteHandler writeHandler : handlerList) { if (writeHandler instanceof CellWriteHandler) { ((CellWriteHandler)writeHandler).afterCellDispose(context); @@ -210,13 +214,14 @@ public class WriteHandlerUtils { } } - public static void afterRowCreate(WriteContext writeContext, Row row, Integer relativeRowIndex, Boolean isHead) { + public static void afterRowCreate(WriteContext writeContext, Row row, Integer rowIndex, Integer relativeRowIndex, + Boolean isHead) { List handlerList = writeContext.currentWriteHolder().writeHandlerMap().get(RowWriteHandler.class); if (handlerList == null || handlerList.isEmpty()) { return; } RowWriteHandlerContext context = new RowWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), - writeContext.writeSheetHolder(), writeContext.writeTableHolder(), row.getRowNum(), row, relativeRowIndex, + writeContext.writeSheetHolder(), writeContext.writeTableHolder(), rowIndex, row, relativeRowIndex, isHead); for (WriteHandler writeHandler : handlerList) { if (writeHandler instanceof RowWriteHandler) { @@ -225,14 +230,14 @@ public class WriteHandlerUtils { } } - public static void afterRowDispose(WriteContext writeContext, Row row, Integer relativeRowIndex, Boolean isHead) { + public static void afterRowDispose(WriteContext writeContext, Row row, Integer rowIndex, Integer relativeRowIndex, + Boolean isHead) { List handlerList = writeContext.currentWriteHolder().writeHandlerMap().get(RowWriteHandler.class); if (handlerList == null || handlerList.isEmpty()) { return; } RowWriteHandlerContext context = new RowWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), - writeContext.writeSheetHolder(), writeContext.writeTableHolder(), row.getRowNum(), row, relativeRowIndex, - isHead); + writeContext.writeSheetHolder(), writeContext.writeTableHolder(), rowIndex, row, relativeRowIndex, isHead); for (WriteHandler writeHandler : handlerList) { if (writeHandler instanceof RowWriteHandler) { ((RowWriteHandler)writeHandler).afterRowDispose(context); diff --git a/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java b/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java index 597d94de..7c94e178 100644 --- a/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java +++ b/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java @@ -32,6 +32,7 @@ import org.apache.poi.ss.usermodel.Comment; import org.apache.poi.ss.usermodel.CreationHelper; import org.apache.poi.ss.usermodel.Drawing; import org.apache.poi.ss.usermodel.Hyperlink; +import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.xssf.usermodel.XSSFClientAnchor; @@ -48,16 +49,16 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { } protected WriteCellData converterAndSet(WriteHolder currentWriteHolder, Class clazz, - CellDataTypeEnum targetType, Cell cell, Object value, ExcelContentProperty excelContentProperty, Head head, - Integer relativeRowIndex, int rowIndex, int columnIndex) { + CellDataTypeEnum targetType, Cell cell, Row row, Object value, ExcelContentProperty excelContentProperty, + Head head, Integer relativeRowIndex, int rowIndex, int columnIndex) { boolean needTrim = value != null && (value instanceof String && currentWriteHolder.globalConfiguration() .getAutoTrim()); if (needTrim) { value = ((String)value).trim(); } WriteCellData cellData = convert(currentWriteHolder, clazz, targetType, cell, value, excelContentProperty); - WriteHandlerUtils.afterCellDataConverted(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE, - excelContentProperty); + WriteHandlerUtils.afterCellDataConverted(writeContext, cellData, cell, row, head, columnIndex, relativeRowIndex, + Boolean.FALSE, excelContentProperty); // Fill in picture information fillImage(cell, cellData.getImageDataList()); diff --git a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java index bdfea49e..db0f7247 100644 --- a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java +++ b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java @@ -66,7 +66,7 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { } WriteHandlerUtils.beforeRowCreate(writeContext, rowIndex, relativeRowIndex, Boolean.FALSE); Row row = WorkBookUtil.createRow(writeContext.writeSheetHolder().getSheet(), rowIndex); - WriteHandlerUtils.afterRowCreate(writeContext, row, relativeRowIndex, Boolean.FALSE); + WriteHandlerUtils.afterRowCreate(writeContext, row, rowIndex, relativeRowIndex, Boolean.FALSE); if (oneRowData instanceof Collection) { addBasicTypeToExcel(new CollectionRowData((Collection)oneRowData), row, rowIndex, relativeRowIndex); @@ -75,7 +75,7 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { } else { addJavaObjectToExcel(oneRowData, row, rowIndex, relativeRowIndex, sortedAllFiledMap); } - WriteHandlerUtils.afterRowDispose(writeContext, row, relativeRowIndex, Boolean.FALSE); + WriteHandlerUtils.afterRowDispose(writeContext, row, rowIndex, relativeRowIndex, Boolean.FALSE); } private void addBasicTypeToExcel(RowData oneRowData, Row row, int rowIndex, int relativeRowIndex) { @@ -117,12 +117,14 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { WriteHandlerUtils.beforeCellCreate(writeContext, row, head, columnIndex, relativeRowIndex, Boolean.FALSE, excelContentProperty); Cell cell = WorkBookUtil.createCell(row, columnIndex); - WriteHandlerUtils.afterCellCreate(writeContext, cell, head, relativeRowIndex, Boolean.FALSE, + WriteHandlerUtils.afterCellCreate(writeContext, cell, row, head, columnIndex, relativeRowIndex, Boolean.FALSE, excelContentProperty); Object value = oneRowData.get(dataIndex); WriteCellData cellData = converterAndSet(writeContext.currentWriteHolder(), - FieldUtils.getFieldClass(value), null, cell, value, null, head, relativeRowIndex, rowIndex, columnIndex); - WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE, + FieldUtils.getFieldClass(value), null, cell, row, value, null, head, relativeRowIndex, + rowIndex, columnIndex); + WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, row, head, columnIndex, relativeRowIndex, + Boolean.FALSE, excelContentProperty); } @@ -147,12 +149,14 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { WriteHandlerUtils.beforeCellCreate(writeContext, row, head, columnIndex, relativeRowIndex, Boolean.FALSE, excelContentProperty); Cell cell = WorkBookUtil.createCell(row, columnIndex); - WriteHandlerUtils.afterCellCreate(writeContext, cell, head, relativeRowIndex, Boolean.FALSE, + WriteHandlerUtils.afterCellCreate(writeContext, cell, row, head, columnIndex, relativeRowIndex, + Boolean.FALSE, excelContentProperty); Object value = beanMap.get(name); WriteCellData cellData = converterAndSet(currentWriteHolder, head.getField().getType(), - null, cell, value, excelContentProperty, head, relativeRowIndex, rowIndex, columnIndex); - WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE, + null, cell, row, value, excelContentProperty, head, relativeRowIndex, rowIndex, columnIndex); + WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, row, head, columnIndex, + relativeRowIndex, Boolean.FALSE, excelContentProperty); beanMapHandledSet.add(name); maxCellIndex = Math.max(maxCellIndex, columnIndex); @@ -181,14 +185,17 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { excelContentProperty); // fix https://github.com/alibaba/easyexcel/issues/1870 // If there is data, it is written to the next cell - Cell cell = WorkBookUtil.createCell(row, maxCellIndex++); - WriteHandlerUtils.afterCellCreate(writeContext, cell, null, relativeRowIndex, Boolean.FALSE, + Cell cell = WorkBookUtil.createCell(row, maxCellIndex); + WriteHandlerUtils.afterCellCreate(writeContext, cell, row, null, maxCellIndex, relativeRowIndex, + Boolean.FALSE, excelContentProperty); WriteCellData cellData = converterAndSet(currentWriteHolder, - FieldUtils.getFieldClass(beanMap, filedName, value), null, cell, value, null, null, relativeRowIndex, - rowIndex, maxCellIndex); - WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, null, relativeRowIndex, Boolean.FALSE, + FieldUtils.getFieldClass(beanMap, filedName, value), null, cell, row, value, null, null, + relativeRowIndex, rowIndex, maxCellIndex); + WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, row, null, maxCellIndex, relativeRowIndex, + Boolean.FALSE, excelContentProperty); + maxCellIndex++; } } diff --git a/src/main/java/com/alibaba/excel/write/handler/impl/FillStyleCellWriteHandler.java b/src/main/java/com/alibaba/excel/write/handler/impl/FillStyleCellWriteHandler.java index c8116b4d..819da9b1 100644 --- a/src/main/java/com/alibaba/excel/write/handler/impl/FillStyleCellWriteHandler.java +++ b/src/main/java/com/alibaba/excel/write/handler/impl/FillStyleCellWriteHandler.java @@ -6,7 +6,6 @@ import com.alibaba.excel.constant.OrderConstant; import com.alibaba.excel.metadata.data.WriteCellData; import com.alibaba.excel.write.handler.CellWriteHandler; import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; -import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; import com.alibaba.excel.write.metadata.style.WriteCellStyle; import lombok.extern.slf4j.Slf4j; @@ -41,8 +40,8 @@ public class FillStyleCellWriteHandler implements CellWriteHandler { if (writeCellStyle == null && originCellStyle == null) { return; } - WriteWorkbookHolder writeWorkbookHolder = context.getWriteWorkbookHolder(); - context.getCell().setCellStyle(writeWorkbookHolder.createCellStyle(writeCellStyle, originCellStyle)); + //WriteWorkbookHolder writeWorkbookHolder = context.getWriteWorkbookHolder(); + //context.getCell().setCellStyle(writeWorkbookHolder.createCellStyle(writeCellStyle, originCellStyle)); } } diff --git a/update.md b/update.md index fdec0ee3..6cc837de 100644 --- a/update.md +++ b/update.md @@ -1,3 +1,6 @@ +# 3.0.2 +* 修复写入的性能问题 + # 3.0.1 * 升级到正式版 * 修复填充样式可能丢失的问题 [Issue #2124](https://github.com/alibaba/easyexcel/issues/2124)