From a42a511a55f3f0b518ca65c128f17023df64c68c Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Thu, 21 Oct 2021 16:20:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=86=99=E5=85=A5=E8=BF=87?= =?UTF-8?q?=E6=85=A2=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../executor/AbstractExcelWriteExecutor.java | 6 +-- .../write/executor/ExcelWriteAddExecutor.java | 49 ++++++++++--------- .../executor/ExcelWriteFillExecutor.java | 2 +- 3 files changed, 29 insertions(+), 28 deletions(-) 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 92e42419..597d94de 100644 --- a/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java +++ b/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); 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 6bfe34c2..bdfea49e 100644 --- a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java +++ b/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 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)oneRowData), row, relativeRowIndex); + addBasicTypeToExcel(new MapRowData((Map)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 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 headMap = writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadMap(); for (Map.Entry 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); } diff --git a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java index 10025390..2fb755a7 100644 --- a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java +++ b/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