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..aa4893e5 100644 --- a/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java +++ b/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java @@ -154,11 +154,11 @@ public class ExcelWriteHeadProperty extends ExcelHeadProperty { continue; } alreadyRangeSet.add(i + "-" + j); - String headName = headNameList.get(j); + String headName = headNameList.get(j) == null ? "" : headNameList.get(j); 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) == null ? "" : headList.get(k).getHeadNameList().get(j)).equals(headName)) { alreadyRangeSet.add(k + "-" + j); lastCol = k; } else { diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/Test1765/Data1765.xlsx b/src/test/java/com/alibaba/easyexcel/test/demo/Test1765/Data1765.xlsx new file mode 100644 index 00000000..3377bb76 Binary files /dev/null and b/src/test/java/com/alibaba/easyexcel/test/demo/Test1765/Data1765.xlsx differ diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/Test1765/Issue1765.java b/src/test/java/com/alibaba/easyexcel/test/demo/Test1765/Issue1765.java new file mode 100644 index 00000000..7d9f572a --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/demo/Test1765/Issue1765.java @@ -0,0 +1,55 @@ +package com.alibaba.easyexcel.test.demo.Test1765; + +import com.alibaba.easyexcel.test.util.TestFileUtil; +import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.EasyExcelFactory; +import org.junit.Assert; +import org.junit.Test; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +public class Issue1765 { + @Test + public void test1765() throws FileNotFoundException { + String fileName = "src/test/java/com/alibaba/easyexcel/test/demo/Test1765/Data1765.xlsx"; + EasyExcel.write(fileName).head(head()).sheet("模板").doWrite(dataList()); + } + + + private List> head() { + List> list = new ArrayList>(); + List head0 = new ArrayList(); + List head1 = new ArrayList(); + List head2 = new ArrayList(); + List head3 = new ArrayList(); + head0.add("表头"); + head1.add("日期"); + head2.add(null); + head3.add("数字"); + list.add(head0); + list.add(head1); + list.add(head2); + list.add(head3); + + return list; + } + + private List> dataList() { + List> list = new ArrayList>(); + for (int i = 0; i < 5; i++) { + List data = new ArrayList(); + data.add("字符串" + i); + data.add(new Date()); + data.add(i * 2); + data.add(i * 3); + list.add(data); + } + return list; + } +}