From 3b89f724dd45bbeb864ce8a93cd88cdfec32c558 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=84=A6=E6=9D=BE?= Date: Thu, 15 Apr 2021 17:26:45 +0800 Subject: [PATCH 1/4] Fix bug list classType not same with head type when write --- .../write/executor/ExcelWriteAddExecutor.java | 4 +- .../easyexcel/test/temp/bug/DataType.java | 33 ++++++++++++++++ .../easyexcel/test/temp/bug/ExcelCreat.java | 38 +++++++++++++++++++ .../easyexcel/test/temp/bug/HeadType.java | 28 ++++++++++++++ 4 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/alibaba/easyexcel/test/temp/bug/DataType.java create mode 100644 src/test/java/com/alibaba/easyexcel/test/temp/bug/ExcelCreat.java create mode 100644 src/test/java/com/alibaba/easyexcel/test/temp/bug/HeadType.java 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 9f229029..4a148ac1 100644 --- a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java +++ b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java @@ -149,7 +149,7 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { Map ignoreMap = writeContext.currentWriteHolder().excelWriteHeadProperty().getIgnoreMap(); initSortedAllFiledMapFieldList(oneRowData.getClass(), sortedAllFiledMap); for (Map.Entry entry : sortedAllFiledMap.entrySet()) { - cellIndex = entry.getKey(); + // cellIndex = entry.getKey(); Field field = entry.getValue(); String filedName = field.getName(); boolean uselessData = !beanMap.containsKey(filedName) || beanMapHandledSet.contains(filedName) @@ -158,7 +158,7 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { continue; } Object value = beanMap.get(filedName); - WriteHandlerUtils.beforeCellCreate(writeContext, row, null, cellIndex, relativeRowIndex, Boolean.FALSE); + WriteHandlerUtils.beforeCellCreate(writeContext, row, null, cellIndex++, relativeRowIndex, Boolean.FALSE); Cell cell = WorkBookUtil.createCell(row, cellIndex); WriteHandlerUtils.afterCellCreate(writeContext, cell, null, relativeRowIndex, Boolean.FALSE); CellData cellData = converterAndSet(currentWriteHolder, value == null ? null : value.getClass(), cell, diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/bug/DataType.java b/src/test/java/com/alibaba/easyexcel/test/temp/bug/DataType.java new file mode 100644 index 00000000..37b8dc2b --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/temp/bug/DataType.java @@ -0,0 +1,33 @@ +package com.alibaba.easyexcel.test.temp.bug; + +import com.alibaba.excel.annotation.ExcelProperty; +import lombok.Data; + +/** + * @author jiaosong + * @desc + * @date 2021/4/6 + */ +@Data +public class DataType { + /** + * 任务id + */ + @ExcelProperty("任务ID") + private Integer id; + + @ExcelProperty("多余字段1") + private String firstSurplus; + + @ExcelProperty("多余字段2") + private String secSurplus; + + @ExcelProperty("多余字段3") + private String thirdSurplus; + + @ExcelProperty(value = "备注1") + private String firstRemark; + + @ExcelProperty(value = "备注2") + private String secRemark; +} diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/bug/ExcelCreat.java b/src/test/java/com/alibaba/easyexcel/test/temp/bug/ExcelCreat.java new file mode 100644 index 00000000..f9d97463 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/temp/bug/ExcelCreat.java @@ -0,0 +1,38 @@ +package com.alibaba.easyexcel.test.temp.bug; + +import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.ExcelWriter; +import com.alibaba.excel.write.metadata.WriteSheet; + +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.util.Collections; +import java.util.List; + +/** + * @author jiaosong + * @desc + * @date 2020/4/6 + */ +public class ExcelCreat { + + + public static void main(String[] args) throws FileNotFoundException { + List data = getData(); + ExcelWriter excelWriter = null; + excelWriter = EasyExcel.write(new FileOutputStream("all.xlsx")).build(); + WriteSheet writeSheet = EasyExcel.writerSheet(1, "test") + .head(HeadType.class) + .build(); + excelWriter.write(data, writeSheet); + excelWriter.finish(); + } + + private static List getData() { + DataType vo = new DataType(); + vo.setId(738); + vo.setFirstRemark("1222"); + vo.setSecRemark("22222"); + return Collections.singletonList(vo); + } +} diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/bug/HeadType.java b/src/test/java/com/alibaba/easyexcel/test/temp/bug/HeadType.java new file mode 100644 index 00000000..15ee006d --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/temp/bug/HeadType.java @@ -0,0 +1,28 @@ +package com.alibaba.easyexcel.test.temp.bug; + +import com.alibaba.excel.annotation.ExcelProperty; +import lombok.Data; + +/** + * @author jiaosong + * @desc + * @date 2021/4/6 + */ +@Data +public class HeadType { + + + /** + * 任务id + */ + @ExcelProperty("任务ID") + private Integer id; + + + @ExcelProperty(value = "备注1") + private String firstRemark; + + @ExcelProperty(value = "备注2") + private String secRemark; + +} From b3c8a699d4b7455f52dbaa9c7b58e4f3fac753e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=84=A6=E6=9D=BE?= Date: Thu, 15 Apr 2021 17:48:38 +0800 Subject: [PATCH 2/4] Remove commented code --- .../com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java | 1 - 1 file changed, 1 deletion(-) 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 4a148ac1..98dc4f9a 100644 --- a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java +++ b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java @@ -149,7 +149,6 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { Map ignoreMap = writeContext.currentWriteHolder().excelWriteHeadProperty().getIgnoreMap(); initSortedAllFiledMapFieldList(oneRowData.getClass(), sortedAllFiledMap); for (Map.Entry entry : sortedAllFiledMap.entrySet()) { - // cellIndex = entry.getKey(); Field field = entry.getValue(); String filedName = field.getName(); boolean uselessData = !beanMap.containsKey(filedName) || beanMapHandledSet.contains(filedName) From 4d04fe3163718c47fb627909359474898327a0df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=84=A6=E6=9D=BE?= Date: Fri, 16 Apr 2021 14:24:39 +0800 Subject: [PATCH 3/4] Fix bug: sort map no head --- .../write/executor/ExcelWriteAddExecutor.java | 30 +++++++------------ 1 file changed, 11 insertions(+), 19 deletions(-) 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 98dc4f9a..1168bde3 100644 --- a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java +++ b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java @@ -1,16 +1,5 @@ package com.alibaba.excel.write.executor; -import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.TreeMap; - -import org.apache.poi.ss.usermodel.Cell; -import org.apache.poi.ss.usermodel.Row; - import com.alibaba.excel.context.WriteContext; import com.alibaba.excel.enums.HeadKindEnum; import com.alibaba.excel.metadata.CellData; @@ -20,13 +9,15 @@ import com.alibaba.excel.util.ClassUtils; import com.alibaba.excel.util.CollectionUtils; import com.alibaba.excel.util.WorkBookUtil; import com.alibaba.excel.util.WriteHandlerUtils; -import com.alibaba.excel.write.metadata.WriteWorkbook; -import com.alibaba.excel.write.metadata.holder.AbstractWriteHolder; import com.alibaba.excel.write.metadata.holder.WriteHolder; import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; - import net.sf.cglib.beans.BeanMap; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Row; + +import java.lang.reflect.Field; +import java.util.*; /** * Add the data into excel @@ -59,7 +50,7 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { } private void addOneRowOfDataToExcel(Object oneRowData, int n, int relativeRowIndex, - Map sortedAllFiledMap) { + Map sortedAllFiledMap) { if (oneRowData == null) { return; } @@ -103,7 +94,7 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { } private void doAddBasicTypeToExcel(List oneRowData, Head head, Row row, int relativeRowIndex, int dataIndex, - int cellIndex) { + int cellIndex) { WriteHandlerUtils.beforeCellCreate(writeContext, row, head, cellIndex, relativeRowIndex, Boolean.FALSE); Cell cell = WorkBookUtil.createCell(row, cellIndex); WriteHandlerUtils.afterCellCreate(writeContext, cell, head, relativeRowIndex, Boolean.FALSE); @@ -114,7 +105,7 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { } private void addJavaObjectToExcel(Object oneRowData, Row row, int relativeRowIndex, - Map sortedAllFiledMap) { + Map sortedAllFiledMap) { WriteHolder currentWriteHolder = writeContext.currentWriteHolder(); BeanMap beanMap = BeanMap.create(oneRowData); Set beanMapHandledSet = new HashSet(); @@ -141,6 +132,7 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE); beanMapHandledSet.add(name); } + cellIndex++; } // Finish if (beanMapHandledSet.size() == beanMap.size()) { @@ -157,8 +149,8 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { continue; } Object value = beanMap.get(filedName); - WriteHandlerUtils.beforeCellCreate(writeContext, row, null, cellIndex++, relativeRowIndex, Boolean.FALSE); - Cell cell = WorkBookUtil.createCell(row, cellIndex); + WriteHandlerUtils.beforeCellCreate(writeContext, row, null, cellIndex, relativeRowIndex, Boolean.FALSE); + Cell cell = WorkBookUtil.createCell(row, cellIndex++); WriteHandlerUtils.afterCellCreate(writeContext, cell, null, relativeRowIndex, Boolean.FALSE); CellData cellData = converterAndSet(currentWriteHolder, value == null ? null : value.getClass(), cell, value, null, null, relativeRowIndex); From 79453b6c70e5ef9354131c7c2ea4da868733279b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=84=A6=E6=9D=BE?= Date: Mon, 19 Apr 2021 14:00:11 +0800 Subject: [PATCH 4/4] Update import package, remove import * --- .../excel/write/executor/ExcelWriteAddExecutor.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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 1168bde3..fe83cc22 100644 --- a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java +++ b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java @@ -17,7 +17,12 @@ import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import java.lang.reflect.Field; -import java.util.*; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; /** * Add the data into excel