Browse Source

修复写入过慢的bug

pull/2144/head
Jiaju Zhuang 3 years ago
parent
commit
a42a511a55
  1. 6
      src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java
  2. 49
      src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java
  3. 2
      src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java

6
src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java

@ -49,7 +49,7 @@ 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, Object value, ExcelContentProperty excelContentProperty, Head head,
Integer relativeRowIndex) { 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) {
@ -72,8 +72,8 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
fillFormula(cell, cellData.getFormulaData()); fillFormula(cell, cellData.getFormulaData());
// Fill index // Fill index
cellData.setRowIndex(cell.getRowIndex()); cellData.setRowIndex(rowIndex);
cellData.setColumnIndex(cell.getColumnIndex()); cellData.setColumnIndex(columnIndex);
if (cellData.getType() == null) { if (cellData.getType() == null) {
cellData.setType(CellDataTypeEnum.EMPTY); cellData.setType(CellDataTypeEnum.EMPTY);

49
src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java

@ -59,26 +59,26 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
} }
} }
private void addOneRowOfDataToExcel(Object oneRowData, int n, int relativeRowIndex, private void addOneRowOfDataToExcel(Object oneRowData, int rowIndex, int relativeRowIndex,
Map<Integer, Field> sortedAllFiledMap) { Map<Integer, Field> sortedAllFiledMap) {
if (oneRowData == null) { if (oneRowData == null) {
return; return;
} }
WriteHandlerUtils.beforeRowCreate(writeContext, n, relativeRowIndex, Boolean.FALSE); WriteHandlerUtils.beforeRowCreate(writeContext, rowIndex, relativeRowIndex, Boolean.FALSE);
Row row = WorkBookUtil.createRow(writeContext.writeSheetHolder().getSheet(), n); Row row = WorkBookUtil.createRow(writeContext.writeSheetHolder().getSheet(), rowIndex);
WriteHandlerUtils.afterRowCreate(writeContext, row, relativeRowIndex, Boolean.FALSE); WriteHandlerUtils.afterRowCreate(writeContext, row, relativeRowIndex, Boolean.FALSE);
if (oneRowData instanceof Collection<?>) { if (oneRowData instanceof Collection<?>) {
addBasicTypeToExcel(new CollectionRowData((Collection<?>)oneRowData), row, relativeRowIndex); addBasicTypeToExcel(new CollectionRowData((Collection<?>)oneRowData), row, rowIndex, relativeRowIndex);
} else if (oneRowData instanceof Map) { } else if (oneRowData instanceof Map) {
addBasicTypeToExcel(new MapRowData((Map<Integer, ?>)oneRowData), row, relativeRowIndex); addBasicTypeToExcel(new MapRowData((Map<Integer, ?>)oneRowData), row, rowIndex, relativeRowIndex);
} else { } else {
addJavaObjectToExcel(oneRowData, row, relativeRowIndex, sortedAllFiledMap); addJavaObjectToExcel(oneRowData, row, rowIndex, relativeRowIndex, sortedAllFiledMap);
} }
WriteHandlerUtils.afterRowDispose(writeContext, row, relativeRowIndex, Boolean.FALSE); WriteHandlerUtils.afterRowDispose(writeContext, row, relativeRowIndex, Boolean.FALSE);
} }
private void addBasicTypeToExcel(RowData oneRowData, Row row, int relativeRowIndex) { private void addBasicTypeToExcel(RowData oneRowData, Row row, int rowIndex, int relativeRowIndex) {
if (oneRowData.isEmpty()) { if (oneRowData.isEmpty()) {
return; return;
} }
@ -89,10 +89,10 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
if (dataIndex >= oneRowData.size()) { if (dataIndex >= oneRowData.size()) {
return; return;
} }
int cellIndex = entry.getKey(); int columnIndex = entry.getKey();
Head head = entry.getValue(); Head head = entry.getValue();
doAddBasicTypeToExcel(oneRowData, head, row, relativeRowIndex, dataIndex++, cellIndex); doAddBasicTypeToExcel(oneRowData, head, row, rowIndex, relativeRowIndex, dataIndex++, columnIndex);
maxCellIndex = Math.max(maxCellIndex, cellIndex); maxCellIndex = Math.max(maxCellIndex, columnIndex);
} }
// Finish // Finish
if (dataIndex >= oneRowData.size()) { if (dataIndex >= oneRowData.size()) {
@ -104,29 +104,29 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
int size = oneRowData.size() - dataIndex; int size = oneRowData.size() - dataIndex;
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
doAddBasicTypeToExcel(oneRowData, null, row, relativeRowIndex, dataIndex++, maxCellIndex++); doAddBasicTypeToExcel(oneRowData, null, row, rowIndex, relativeRowIndex, dataIndex++, maxCellIndex++);
} }
} }
private void doAddBasicTypeToExcel(RowData oneRowData, Head head, Row row, int relativeRowIndex, int dataIndex, private void doAddBasicTypeToExcel(RowData oneRowData, Head head, Row row, int rowIndex, int relativeRowIndex,
int cellIndex) { int dataIndex, int columnIndex) {
ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(null, ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(null,
writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadClazz(), writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadClazz(),
head == null ? null : head.getFieldName()); head == null ? null : head.getFieldName());
WriteHandlerUtils.beforeCellCreate(writeContext, row, head, cellIndex, relativeRowIndex, Boolean.FALSE, WriteHandlerUtils.beforeCellCreate(writeContext, row, head, columnIndex, relativeRowIndex, Boolean.FALSE,
excelContentProperty); excelContentProperty);
Cell cell = WorkBookUtil.createCell(row, cellIndex); Cell cell = WorkBookUtil.createCell(row, columnIndex);
WriteHandlerUtils.afterCellCreate(writeContext, cell, head, relativeRowIndex, Boolean.FALSE, WriteHandlerUtils.afterCellCreate(writeContext, cell, head, 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); FieldUtils.getFieldClass(value), null, cell, value, null, head, relativeRowIndex, rowIndex, columnIndex);
WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE, WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE,
excelContentProperty); excelContentProperty);
} }
private void addJavaObjectToExcel(Object oneRowData, Row row, int relativeRowIndex, private void addJavaObjectToExcel(Object oneRowData, Row row, int rowIndex, int relativeRowIndex,
Map<Integer, Field> sortedAllFiledMap) { Map<Integer, Field> sortedAllFiledMap) {
WriteHolder currentWriteHolder = writeContext.currentWriteHolder(); WriteHolder currentWriteHolder = writeContext.currentWriteHolder();
BeanMap beanMap = BeanMapUtils.create(oneRowData); BeanMap beanMap = BeanMapUtils.create(oneRowData);
@ -136,7 +136,7 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
if (HeadKindEnum.CLASS.equals(writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadKind())) { if (HeadKindEnum.CLASS.equals(writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadKind())) {
Map<Integer, Head> headMap = writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadMap(); Map<Integer, Head> headMap = writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadMap();
for (Map.Entry<Integer, Head> entry : headMap.entrySet()) { for (Map.Entry<Integer, Head> entry : headMap.entrySet()) {
int cellIndex = entry.getKey(); int columnIndex = entry.getKey();
Head head = entry.getValue(); Head head = entry.getValue();
String name = head.getFieldName(); String name = head.getFieldName();
if (!beanMap.containsKey(name)) { if (!beanMap.containsKey(name)) {
@ -144,18 +144,18 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
} }
ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(beanMap, ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(beanMap,
currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), name); currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), name);
WriteHandlerUtils.beforeCellCreate(writeContext, row, head, cellIndex, relativeRowIndex, Boolean.FALSE, WriteHandlerUtils.beforeCellCreate(writeContext, row, head, columnIndex, relativeRowIndex,
excelContentProperty); Boolean.FALSE, excelContentProperty);
Cell cell = WorkBookUtil.createCell(row, cellIndex); Cell cell = WorkBookUtil.createCell(row, columnIndex);
WriteHandlerUtils.afterCellCreate(writeContext, cell, head, relativeRowIndex, Boolean.FALSE, WriteHandlerUtils.afterCellCreate(writeContext, cell, head, 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); null, cell, value, excelContentProperty, head, relativeRowIndex, rowIndex, columnIndex);
WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE, WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE,
excelContentProperty); excelContentProperty);
beanMapHandledSet.add(name); beanMapHandledSet.add(name);
maxCellIndex = Math.max(maxCellIndex, cellIndex); maxCellIndex = Math.max(maxCellIndex, columnIndex);
} }
} }
// Finish // Finish
@ -185,7 +185,8 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
WriteHandlerUtils.afterCellCreate(writeContext, cell, null, relativeRowIndex, Boolean.FALSE, WriteHandlerUtils.afterCellCreate(writeContext, cell, null, 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, value, null, null, relativeRowIndex,
rowIndex, maxCellIndex);
WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, null, relativeRowIndex, Boolean.FALSE, WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, null, relativeRowIndex, Boolean.FALSE,
excelContentProperty); excelContentProperty);
} }

2
src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java

@ -203,7 +203,7 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
WriteCellData<?> cellData = converterAndSet(writeSheetHolder, WriteCellData<?> cellData = converterAndSet(writeSheetHolder,
FieldUtils.getFieldClass(dataMap, variable, value), null, cell, value, excelContentProperty, null, FieldUtils.getFieldClass(dataMap, variable, value), null, cell, value, excelContentProperty, null,
relativeRowIndex); relativeRowIndex, analysisCell.getRowIndex(), analysisCell.getColumnIndex());
cellData.setAnalysisCell(analysisCell); cellData.setAnalysisCell(analysisCell);
// Restyle // Restyle

Loading…
Cancel
Save