From e2e2971bfb519e50e64a7227b8c28bdef16974e0 Mon Sep 17 00:00:00 2001 From: lethal233 <11811407@mail.sustech.edu.cn> Date: Wed, 19 May 2021 22:18:37 +0800 Subject: [PATCH 1/3] bugfix-1662 --- .../property/ExcelWriteHeadProperty.java | 4 +- .../test/temp/issue1662/Data1662.java | 20 +++++++++ .../test/temp/issue1662/Issue1662Test.java | 42 +++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/alibaba/easyexcel/test/temp/issue1662/Data1662.java create mode 100644 src/test/java/com/alibaba/easyexcel/test/temp/issue1662/Issue1662Test.java diff --git a/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java b/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java index 979a7750..2ba4ff9d 100644 --- a/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java +++ b/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java @@ -158,7 +158,9 @@ public class ExcelWriteHeadProperty extends ExcelHeadProperty { int lastCol = i; int lastRow = j; for (int k = i + 1; k < headList.size(); k++) { - if (headList.get(k).getHeadNameList().get(j).equals(headName)) { + if (headList.get(k).getHeadNameList().get(j).equals(headName) + && (j - 1 >= 0) + && !headList.get(k).getHeadNameList().get(j-1).equals(headName)) { alreadyRangeSet.add(k + "-" + j); lastCol = k; } else { diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/issue1662/Data1662.java b/src/test/java/com/alibaba/easyexcel/test/temp/issue1662/Data1662.java new file mode 100644 index 00000000..53a1a5c8 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/temp/issue1662/Data1662.java @@ -0,0 +1,20 @@ +package com.alibaba.easyexcel.test.temp.issue1662; + +import com.alibaba.excel.annotation.ExcelProperty; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.Date; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class Data1662 { + @ExcelProperty(index = 0) + private String str; + @ExcelProperty(index = 1) + private Date date; + @ExcelProperty(index = 2) + private double r; +} diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/issue1662/Issue1662Test.java b/src/test/java/com/alibaba/easyexcel/test/temp/issue1662/Issue1662Test.java new file mode 100644 index 00000000..5d311c45 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/temp/issue1662/Issue1662Test.java @@ -0,0 +1,42 @@ +package com.alibaba.easyexcel.test.temp.issue1662; + +import com.alibaba.easyexcel.test.util.TestFileUtil; +import com.alibaba.excel.EasyExcel; +import org.junit.Test; + + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +public class Issue1662Test { + @Test + public void test1662() { + String fileName = TestFileUtil.getPath() + "Test1939" + ".xlsx"; + System.out.println(fileName); + EasyExcel.write(fileName).head(head()).sheet("模板").doWrite(dataList()); + } + + + private List> head() { + List> list = new ArrayList<>(); + List head0 = new ArrayList<>(); + List head1 = new ArrayList<>(); + head0.add("xx"); + head0.add("日期"); + list.add(head0); + head1.add("日期"); + list.add(head1); + return list; + } + + private List> dataList() { + List> list = new ArrayList<>(); + List data = new ArrayList<>(); + data.add("字符串"); + data.add(new Date()); + data.add(0.56); + list.add(data); + return list; + } +} From e1bc4743f3b125718ebdf612355edc718d29e653 Mon Sep 17 00:00:00 2001 From: lethal233 <11811407@mail.sustech.edu.cn> Date: Thu, 20 May 2021 12:19:34 +0800 Subject: [PATCH 2/3] modify if-statement to satisfy pmd check --- .../alibaba/excel/write/property/ExcelWriteHeadProperty.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java b/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java index 2ba4ff9d..8e3cb45c 100644 --- a/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java +++ b/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java @@ -158,9 +158,8 @@ public class ExcelWriteHeadProperty extends ExcelHeadProperty { int lastCol = i; int lastRow = j; for (int k = i + 1; k < headList.size(); k++) { - if (headList.get(k).getHeadNameList().get(j).equals(headName) - && (j - 1 >= 0) - && !headList.get(k).getHeadNameList().get(j-1).equals(headName)) { + boolean flag = (j - 1 >= 0) && !headList.get(k).getHeadNameList().get(j - 1).equals(headName); + if (headList.get(k).getHeadNameList().get(j).equals(headName) && flag) { alreadyRangeSet.add(k + "-" + j); lastCol = k; } else { From 8b11873c8b5b58df5aa4d4624e7cb0a3be8ba6cd Mon Sep 17 00:00:00 2001 From: lethal233 <11811407@mail.sustech.edu.cn> Date: Thu, 20 May 2021 12:27:07 +0800 Subject: [PATCH 3/3] modify diamond compatibility using source 1.6 --- .../easyexcel/test/temp/issue1662/Issue1662Test.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/issue1662/Issue1662Test.java b/src/test/java/com/alibaba/easyexcel/test/temp/issue1662/Issue1662Test.java index 5d311c45..8fa5a842 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/issue1662/Issue1662Test.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/issue1662/Issue1662Test.java @@ -19,9 +19,9 @@ public class Issue1662Test { private List> head() { - List> list = new ArrayList<>(); - List head0 = new ArrayList<>(); - List head1 = new ArrayList<>(); + List> list = new ArrayList>(); + List head0 = new ArrayList(); + List head1 = new ArrayList(); head0.add("xx"); head0.add("日期"); list.add(head0); @@ -31,8 +31,8 @@ public class Issue1662Test { } private List> dataList() { - List> list = new ArrayList<>(); - List data = new ArrayList<>(); + List> list = new ArrayList>(); + List data = new ArrayList(); data.add("字符串"); data.add(new Date()); data.add(0.56);