Browse Source

* 修复写入的性能问题

test
Jiaju Zhuang 3 years ago
parent
commit
a147f71462
  1. 11
      src/main/java/com/alibaba/excel/context/WriteContextImpl.java
  2. 43
      src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java
  3. 9
      src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java
  4. 33
      src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java
  5. 5
      src/main/java/com/alibaba/excel/write/handler/impl/FillStyleCellWriteHandler.java
  6. 3
      update.md

11
src/main/java/com/alibaba/excel/context/WriteContextImpl.java

@ -228,9 +228,9 @@ public class WriteContextImpl implements WriteContext {
+ newRowIndex; i++, relativeRowIndex++) { + newRowIndex; i++, relativeRowIndex++) {
WriteHandlerUtils.beforeRowCreate(this, newRowIndex, relativeRowIndex, Boolean.TRUE); WriteHandlerUtils.beforeRowCreate(this, newRowIndex, relativeRowIndex, Boolean.TRUE);
Row row = WorkBookUtil.createRow(writeSheetHolder.getSheet(), i); 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); 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); 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<String> writeCellData = new WriteCellData<>(head.getHeadNameList().get(relativeRowIndex)); WriteCellData<String> writeCellData = new WriteCellData<>(head.getHeadNameList().get(relativeRowIndex));
cell.setCellValue(writeCellData.getStringValue()); cell.setCellValue(writeCellData.getStringValue());
WriteHandlerUtils.afterCellDispose(this, writeCellData, cell, head, relativeRowIndex, Boolean.TRUE, WriteHandlerUtils.afterCellDispose(this, writeCellData, cell, row, head, columnIndex, relativeRowIndex,
excelContentProperty); Boolean.TRUE, excelContentProperty);
} }
} }

43
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, public static void afterCellCreate(WriteContext writeContext, Cell cell, Row row, Head head,
Boolean isHead, ExcelContentProperty excelContentProperty) { Integer columnIndex, Integer relativeRowIndex, Boolean isHead, ExcelContentProperty excelContentProperty) {
List<WriteHandler> handlerList = List<WriteHandler> handlerList =
writeContext.currentWriteHolder().writeHandlerMap().get(CellWriteHandler.class); writeContext.currentWriteHolder().writeHandlerMap().get(CellWriteHandler.class);
if (handlerList == null || handlerList.isEmpty()) { if (handlerList == null || handlerList.isEmpty()) {
return; return;
} }
CellWriteHandlerContext context = new CellWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), CellWriteHandlerContext context = new CellWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(),
writeContext.writeSheetHolder(), writeContext.writeTableHolder(), cell.getRow(), cell, writeContext.writeSheetHolder(), writeContext.writeTableHolder(), row, cell, columnIndex, relativeRowIndex,
cell.getColumnIndex(), relativeRowIndex, head, null, null, isHead, excelContentProperty); head, null, null, isHead, excelContentProperty);
for (WriteHandler writeHandler : handlerList) { for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof CellWriteHandler) { if (writeHandler instanceof CellWriteHandler) {
((CellWriteHandler)writeHandler).afterCellCreate(context); ((CellWriteHandler)writeHandler).afterCellCreate(context);
@ -150,8 +150,9 @@ public class WriteHandlerUtils {
} }
} }
public static void afterCellDataConverted(WriteContext writeContext, WriteCellData<?> cellData, Cell cell, public static void afterCellDataConverted(WriteContext writeContext, WriteCellData<?> cellData, Cell cell, Row row,
Head head, Integer relativeRowIndex, Boolean isHead, ExcelContentProperty excelContentProperty) { Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead,
ExcelContentProperty excelContentProperty) {
List<WriteHandler> handlerList = List<WriteHandler> handlerList =
writeContext.currentWriteHolder().writeHandlerMap().get(CellWriteHandler.class); writeContext.currentWriteHolder().writeHandlerMap().get(CellWriteHandler.class);
if (handlerList == null || handlerList.isEmpty()) { if (handlerList == null || handlerList.isEmpty()) {
@ -159,8 +160,8 @@ public class WriteHandlerUtils {
} }
List<WriteCellData<?>> cellDataList = cellData == null ? null : ListUtils.newArrayList(cellData); List<WriteCellData<?>> cellDataList = cellData == null ? null : ListUtils.newArrayList(cellData);
CellWriteHandlerContext context = new CellWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), CellWriteHandlerContext context = new CellWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(),
writeContext.writeSheetHolder(), writeContext.writeTableHolder(), cell.getRow(), cell, writeContext.writeSheetHolder(), writeContext.writeTableHolder(), row, cell,
cell.getColumnIndex(), relativeRowIndex, head, cellDataList, cellData, isHead, excelContentProperty); columnIndex, relativeRowIndex, head, cellDataList, cellData, isHead, excelContentProperty);
for (WriteHandler writeHandler : handlerList) { for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof CellWriteHandler) { if (writeHandler instanceof CellWriteHandler) {
((CellWriteHandler)writeHandler).afterCellDataConverted(context); ((CellWriteHandler)writeHandler).afterCellDataConverted(context);
@ -168,14 +169,17 @@ public class WriteHandlerUtils {
} }
} }
public static void afterCellDispose(WriteContext writeContext, WriteCellData<?> cellData, Cell cell, Head head, public static void afterCellDispose(WriteContext writeContext, WriteCellData<?> cellData, Cell cell, Row row,
Integer relativeRowIndex, Boolean isHead, ExcelContentProperty excelContentProperty) { Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead,
ExcelContentProperty excelContentProperty) {
List<WriteCellData<?>> cellDataList = cellData == null ? null : ListUtils.newArrayList(cellData); List<WriteCellData<?>> 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<WriteCellData<?>> cellDataList, Cell cell, public static void afterCellDispose(WriteContext writeContext, List<WriteCellData<?>> cellDataList, Cell cell,
Head head, Integer relativeRowIndex, Boolean isHead, ExcelContentProperty excelContentProperty) { Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead,
ExcelContentProperty excelContentProperty) {
List<WriteHandler> handlerList = List<WriteHandler> handlerList =
writeContext.currentWriteHolder().writeHandlerMap().get(CellWriteHandler.class); writeContext.currentWriteHolder().writeHandlerMap().get(CellWriteHandler.class);
if (handlerList == null || handlerList.isEmpty()) { if (handlerList == null || handlerList.isEmpty()) {
@ -186,8 +190,8 @@ public class WriteHandlerUtils {
cellData = cellDataList.get(0); cellData = cellDataList.get(0);
} }
CellWriteHandlerContext context = new CellWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), CellWriteHandlerContext context = new CellWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(),
writeContext.writeSheetHolder(), writeContext.writeTableHolder(), cell.getRow(), cell, writeContext.writeSheetHolder(), writeContext.writeTableHolder(), row, cell, columnIndex, relativeRowIndex,
cell.getColumnIndex(), relativeRowIndex, head, cellDataList, cellData, isHead, excelContentProperty); head, cellDataList, cellData, isHead, excelContentProperty);
for (WriteHandler writeHandler : handlerList) { for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof CellWriteHandler) { if (writeHandler instanceof CellWriteHandler) {
((CellWriteHandler)writeHandler).afterCellDispose(context); ((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<WriteHandler> handlerList = writeContext.currentWriteHolder().writeHandlerMap().get(RowWriteHandler.class); List<WriteHandler> handlerList = writeContext.currentWriteHolder().writeHandlerMap().get(RowWriteHandler.class);
if (handlerList == null || handlerList.isEmpty()) { if (handlerList == null || handlerList.isEmpty()) {
return; return;
} }
RowWriteHandlerContext context = new RowWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), RowWriteHandlerContext context = new RowWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(),
writeContext.writeSheetHolder(), writeContext.writeTableHolder(), row.getRowNum(), row, relativeRowIndex, writeContext.writeSheetHolder(), writeContext.writeTableHolder(), rowIndex, row, relativeRowIndex,
isHead); isHead);
for (WriteHandler writeHandler : handlerList) { for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof RowWriteHandler) { 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<WriteHandler> handlerList = writeContext.currentWriteHolder().writeHandlerMap().get(RowWriteHandler.class); List<WriteHandler> handlerList = writeContext.currentWriteHolder().writeHandlerMap().get(RowWriteHandler.class);
if (handlerList == null || handlerList.isEmpty()) { if (handlerList == null || handlerList.isEmpty()) {
return; return;
} }
RowWriteHandlerContext context = new RowWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), RowWriteHandlerContext context = new RowWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(),
writeContext.writeSheetHolder(), writeContext.writeTableHolder(), row.getRowNum(), row, relativeRowIndex, writeContext.writeSheetHolder(), writeContext.writeTableHolder(), rowIndex, row, relativeRowIndex, isHead);
isHead);
for (WriteHandler writeHandler : handlerList) { for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof RowWriteHandler) { if (writeHandler instanceof RowWriteHandler) {
((RowWriteHandler)writeHandler).afterRowDispose(context); ((RowWriteHandler)writeHandler).afterRowDispose(context);

9
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.CreationHelper;
import org.apache.poi.ss.usermodel.Drawing; import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Hyperlink; 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.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor; import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
@ -48,16 +49,16 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
} }
protected WriteCellData<?> converterAndSet(WriteHolder currentWriteHolder, Class<?> clazz, protected WriteCellData<?> converterAndSet(WriteHolder currentWriteHolder, Class<?> clazz,
CellDataTypeEnum targetType, Cell cell, Object value, ExcelContentProperty excelContentProperty, Head head, CellDataTypeEnum targetType, Cell cell, Row row, Object value, ExcelContentProperty excelContentProperty,
Integer relativeRowIndex, int rowIndex, int columnIndex) { Head head, Integer relativeRowIndex, int rowIndex, int columnIndex) {
boolean needTrim = value != null && (value instanceof String && currentWriteHolder.globalConfiguration() boolean needTrim = value != null && (value instanceof String && currentWriteHolder.globalConfiguration()
.getAutoTrim()); .getAutoTrim());
if (needTrim) { if (needTrim) {
value = ((String)value).trim(); value = ((String)value).trim();
} }
WriteCellData<?> cellData = convert(currentWriteHolder, clazz, targetType, cell, value, excelContentProperty); WriteCellData<?> cellData = convert(currentWriteHolder, clazz, targetType, cell, value, excelContentProperty);
WriteHandlerUtils.afterCellDataConverted(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE, WriteHandlerUtils.afterCellDataConverted(writeContext, cellData, cell, row, head, columnIndex, relativeRowIndex,
excelContentProperty); Boolean.FALSE, excelContentProperty);
// Fill in picture information // Fill in picture information
fillImage(cell, cellData.getImageDataList()); fillImage(cell, cellData.getImageDataList());

33
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); WriteHandlerUtils.beforeRowCreate(writeContext, rowIndex, relativeRowIndex, Boolean.FALSE);
Row row = WorkBookUtil.createRow(writeContext.writeSheetHolder().getSheet(), rowIndex); 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<?>) { if (oneRowData instanceof Collection<?>) {
addBasicTypeToExcel(new CollectionRowData((Collection<?>)oneRowData), row, rowIndex, relativeRowIndex); addBasicTypeToExcel(new CollectionRowData((Collection<?>)oneRowData), row, rowIndex, relativeRowIndex);
@ -75,7 +75,7 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
} else { } else {
addJavaObjectToExcel(oneRowData, row, rowIndex, relativeRowIndex, sortedAllFiledMap); 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) { 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, WriteHandlerUtils.beforeCellCreate(writeContext, row, head, columnIndex, relativeRowIndex, Boolean.FALSE,
excelContentProperty); excelContentProperty);
Cell cell = WorkBookUtil.createCell(row, columnIndex); 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); excelContentProperty);
Object value = oneRowData.get(dataIndex); Object value = oneRowData.get(dataIndex);
WriteCellData<?> cellData = converterAndSet(writeContext.currentWriteHolder(), WriteCellData<?> cellData = converterAndSet(writeContext.currentWriteHolder(),
FieldUtils.getFieldClass(value), null, cell, value, null, head, relativeRowIndex, rowIndex, columnIndex); FieldUtils.getFieldClass(value), null, cell, row, value, null, head, relativeRowIndex,
WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE, rowIndex, columnIndex);
WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, row, head, columnIndex, relativeRowIndex,
Boolean.FALSE,
excelContentProperty); excelContentProperty);
} }
@ -147,12 +149,14 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
WriteHandlerUtils.beforeCellCreate(writeContext, row, head, columnIndex, relativeRowIndex, WriteHandlerUtils.beforeCellCreate(writeContext, row, head, columnIndex, relativeRowIndex,
Boolean.FALSE, excelContentProperty); Boolean.FALSE, excelContentProperty);
Cell cell = WorkBookUtil.createCell(row, columnIndex); 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); excelContentProperty);
Object value = beanMap.get(name); Object value = beanMap.get(name);
WriteCellData<?> cellData = converterAndSet(currentWriteHolder, head.getField().getType(), WriteCellData<?> cellData = converterAndSet(currentWriteHolder, head.getField().getType(),
null, cell, value, excelContentProperty, head, relativeRowIndex, rowIndex, columnIndex); null, cell, row, value, excelContentProperty, head, relativeRowIndex, rowIndex, columnIndex);
WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE, WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, row, head, columnIndex,
relativeRowIndex, Boolean.FALSE,
excelContentProperty); excelContentProperty);
beanMapHandledSet.add(name); beanMapHandledSet.add(name);
maxCellIndex = Math.max(maxCellIndex, columnIndex); maxCellIndex = Math.max(maxCellIndex, columnIndex);
@ -181,14 +185,17 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
excelContentProperty); excelContentProperty);
// fix https://github.com/alibaba/easyexcel/issues/1870 // fix https://github.com/alibaba/easyexcel/issues/1870
// If there is data, it is written to the next cell // If there is data, it is written to the next cell
Cell cell = WorkBookUtil.createCell(row, maxCellIndex++); Cell cell = WorkBookUtil.createCell(row, maxCellIndex);
WriteHandlerUtils.afterCellCreate(writeContext, cell, null, relativeRowIndex, Boolean.FALSE, WriteHandlerUtils.afterCellCreate(writeContext, cell, row, null, maxCellIndex, relativeRowIndex,
Boolean.FALSE,
excelContentProperty); excelContentProperty);
WriteCellData<?> cellData = converterAndSet(currentWriteHolder, WriteCellData<?> cellData = converterAndSet(currentWriteHolder,
FieldUtils.getFieldClass(beanMap, filedName, value), null, cell, value, null, null, relativeRowIndex, FieldUtils.getFieldClass(beanMap, filedName, value), null, cell, row, value, null, null,
rowIndex, maxCellIndex); relativeRowIndex, rowIndex, maxCellIndex);
WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, null, relativeRowIndex, Boolean.FALSE, WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, row, null, maxCellIndex, relativeRowIndex,
Boolean.FALSE,
excelContentProperty); excelContentProperty);
maxCellIndex++;
} }
} }

5
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.metadata.data.WriteCellData;
import com.alibaba.excel.write.handler.CellWriteHandler; import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; 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 com.alibaba.excel.write.metadata.style.WriteCellStyle;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -41,8 +40,8 @@ public class FillStyleCellWriteHandler implements CellWriteHandler {
if (writeCellStyle == null && originCellStyle == null) { if (writeCellStyle == null && originCellStyle == null) {
return; return;
} }
WriteWorkbookHolder writeWorkbookHolder = context.getWriteWorkbookHolder(); //WriteWorkbookHolder writeWorkbookHolder = context.getWriteWorkbookHolder();
context.getCell().setCellStyle(writeWorkbookHolder.createCellStyle(writeCellStyle, originCellStyle)); //context.getCell().setCellStyle(writeWorkbookHolder.createCellStyle(writeCellStyle, originCellStyle));
} }
} }

3
update.md

@ -1,3 +1,6 @@
# 3.0.2
* 修复写入的性能问题
# 3.0.1 # 3.0.1
* 升级到正式版 * 升级到正式版
* 修复填充样式可能丢失的问题 [Issue #2124](https://github.com/alibaba/easyexcel/issues/2124) * 修复填充样式可能丢失的问题 [Issue #2124](https://github.com/alibaba/easyexcel/issues/2124)

Loading…
Cancel
Save