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

Loading…
Cancel
Save