From 5ecff59048272e7b661d2a5699768ee5dd6fe096 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Thu, 10 Feb 2022 17:41:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=89=E6=A0=B7=E5=BC=8F=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=9A=84=E6=83=85=E5=86=B5=E4=B8=8B=E4=B9=9F?= =?UTF-8?q?=E7=AE=97=E7=A9=BA=E8=A1=8C=20[Issue=20#2294]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../analysis/v07/handlers/RowTagHandler.java | 20 +++++++++++++++++ .../alibaba/easyexcel/test/temp/LockTest.java | 2 +- .../test/temp/write/TempWriteData.java | 17 ++++++++++++++ .../test/temp/write/TempWriteTest.java | 22 +++++++++++++++++++ update.md | 3 ++- 5 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 easyexcel-core/src/test/java/com/alibaba/easyexcel/test/temp/write/TempWriteData.java create mode 100644 easyexcel-core/src/test/java/com/alibaba/easyexcel/test/temp/write/TempWriteTest.java diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/RowTagHandler.java b/easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/RowTagHandler.java index 37965254..51c192ca 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/RowTagHandler.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/RowTagHandler.java @@ -4,8 +4,10 @@ import java.util.LinkedHashMap; import com.alibaba.excel.constant.ExcelXmlConstants; import com.alibaba.excel.context.xlsx.XlsxReadContext; +import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.RowTypeEnum; import com.alibaba.excel.metadata.Cell; +import com.alibaba.excel.metadata.data.ReadCellData; import com.alibaba.excel.read.metadata.holder.ReadRowHolder; import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder; import com.alibaba.excel.util.PositionUtils; @@ -41,6 +43,24 @@ public class RowTagHandler extends AbstractXlsxTagHandler { public void endElement(XlsxReadContext xlsxReadContext, String name) { XlsxReadSheetHolder xlsxReadSheetHolder = xlsxReadContext.xlsxReadSheetHolder(); RowTypeEnum rowType = MapUtils.isEmpty(xlsxReadSheetHolder.getCellMap()) ? RowTypeEnum.EMPTY : RowTypeEnum.DATA; + // It's possible that all of the cells in the row are empty + if (rowType == RowTypeEnum.DATA) { + boolean hasData = false; + for (Cell cell : xlsxReadSheetHolder.getCellMap().values()) { + if (!(cell instanceof ReadCellData)) { + hasData = true; + break; + } + ReadCellData readCellData = (ReadCellData)cell; + if (readCellData.getType() != CellDataTypeEnum.EMPTY) { + hasData = true; + break; + } + } + if (!hasData) { + rowType = RowTypeEnum.EMPTY; + } + } xlsxReadContext.readRowHolder(new ReadRowHolder(xlsxReadSheetHolder.getRowIndex(), rowType, xlsxReadSheetHolder.getGlobalConfiguration(), xlsxReadSheetHolder.getCellMap())); xlsxReadContext.analysisEventProcessor().endRow(xlsxReadContext); diff --git a/easyexcel-core/src/test/java/com/alibaba/easyexcel/test/temp/LockTest.java b/easyexcel-core/src/test/java/com/alibaba/easyexcel/test/temp/LockTest.java index 031e5114..322891da 100644 --- a/easyexcel-core/src/test/java/com/alibaba/easyexcel/test/temp/LockTest.java +++ b/easyexcel-core/src/test/java/com/alibaba/easyexcel/test/temp/LockTest.java @@ -24,7 +24,7 @@ public class LockTest { @Test public void test() throws Exception { List list = - EasyExcel.read(new FileInputStream("/Users/zhuangjiaju/test/pic.xls")).doReadAllSync(); + EasyExcel.read(new FileInputStream("/Users/zhuangjiaju/Downloads/test.xlsx")).useDefaultListener(false).doReadAllSync(); for (Object data : list) { LOGGER.info("返回数据:{}", JSON.toJSONString(data)); } diff --git a/easyexcel-core/src/test/java/com/alibaba/easyexcel/test/temp/write/TempWriteData.java b/easyexcel-core/src/test/java/com/alibaba/easyexcel/test/temp/write/TempWriteData.java new file mode 100644 index 00000000..b26ce4b6 --- /dev/null +++ b/easyexcel-core/src/test/java/com/alibaba/easyexcel/test/temp/write/TempWriteData.java @@ -0,0 +1,17 @@ +package com.alibaba.easyexcel.test.temp.write; + +import com.alibaba.excel.annotation.ExcelProperty; +import com.alibaba.excel.annotation.write.style.ContentStyle; +import com.alibaba.excel.annotation.write.style.HeadStyle; +import com.alibaba.excel.enums.BooleanEnum; + +import lombok.Data; + +@Data +public class TempWriteData { + + @ExcelProperty(" 换行\r\n \\ \r\n的名字") + @HeadStyle(wrapped = BooleanEnum.TRUE) + @ContentStyle(wrapped = BooleanEnum.TRUE) + private String name; +} diff --git a/easyexcel-core/src/test/java/com/alibaba/easyexcel/test/temp/write/TempWriteTest.java b/easyexcel-core/src/test/java/com/alibaba/easyexcel/test/temp/write/TempWriteTest.java new file mode 100644 index 00000000..0f218e45 --- /dev/null +++ b/easyexcel-core/src/test/java/com/alibaba/easyexcel/test/temp/write/TempWriteTest.java @@ -0,0 +1,22 @@ +package com.alibaba.easyexcel.test.temp.write; + +import com.alibaba.easyexcel.test.util.TestFileUtil; +import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.util.ListUtils; + +import org.junit.Ignore; +import org.junit.Test; + +@Ignore +public class TempWriteTest { + + @Test + public void write() { + TempWriteData tempWriteData = new TempWriteData(); + tempWriteData.setName("zs\r\n \\ \r\n t4"); + EasyExcel.write(TestFileUtil.getPath() + "TempWriteTest" + System.currentTimeMillis() + ".xlsx", + TempWriteData.class) + .sheet() + .doWrite(ListUtils.newArrayList(tempWriteData)); + } +} diff --git a/update.md b/update.md index f6831f4b..57cced3c 100644 --- a/update.md +++ b/update.md @@ -1,5 +1,6 @@ # 3.1.0 -* 去除cglib&asm依赖,改成重新拷贝一份 [Issue #2240](https://github.com/alibaba/easyexcel/issues/2240) +* 支持jdk17,去除cglib&asm依赖,改成重新拷贝一份 [Issue #2240](https://github.com/alibaba/easyexcel/issues/2240) +* 有样式没有数据的情况下也算空行 [Issue #2294](https://github.com/alibaba/easyexcel/issues/2294) # 3.0.5 * 修复`ReadListener` 转换异常不抛出的问题