Browse Source

有样式没有数据的情况下也算空行 [Issue #2294]

pull/2317/head
Jiaju Zhuang 3 years ago
parent
commit
5ecff59048
  1. 20
      easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/RowTagHandler.java
  2. 2
      easyexcel-core/src/test/java/com/alibaba/easyexcel/test/temp/LockTest.java
  3. 17
      easyexcel-core/src/test/java/com/alibaba/easyexcel/test/temp/write/TempWriteData.java
  4. 22
      easyexcel-core/src/test/java/com/alibaba/easyexcel/test/temp/write/TempWriteTest.java
  5. 3
      update.md

20
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.constant.ExcelXmlConstants;
import com.alibaba.excel.context.xlsx.XlsxReadContext; import com.alibaba.excel.context.xlsx.XlsxReadContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.enums.RowTypeEnum; import com.alibaba.excel.enums.RowTypeEnum;
import com.alibaba.excel.metadata.Cell; 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.ReadRowHolder;
import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder; import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder;
import com.alibaba.excel.util.PositionUtils; import com.alibaba.excel.util.PositionUtils;
@ -41,6 +43,24 @@ public class RowTagHandler extends AbstractXlsxTagHandler {
public void endElement(XlsxReadContext xlsxReadContext, String name) { public void endElement(XlsxReadContext xlsxReadContext, String name) {
XlsxReadSheetHolder xlsxReadSheetHolder = xlsxReadContext.xlsxReadSheetHolder(); XlsxReadSheetHolder xlsxReadSheetHolder = xlsxReadContext.xlsxReadSheetHolder();
RowTypeEnum rowType = MapUtils.isEmpty(xlsxReadSheetHolder.getCellMap()) ? RowTypeEnum.EMPTY : RowTypeEnum.DATA; 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, xlsxReadContext.readRowHolder(new ReadRowHolder(xlsxReadSheetHolder.getRowIndex(), rowType,
xlsxReadSheetHolder.getGlobalConfiguration(), xlsxReadSheetHolder.getCellMap())); xlsxReadSheetHolder.getGlobalConfiguration(), xlsxReadSheetHolder.getCellMap()));
xlsxReadContext.analysisEventProcessor().endRow(xlsxReadContext); xlsxReadContext.analysisEventProcessor().endRow(xlsxReadContext);

2
easyexcel-core/src/test/java/com/alibaba/easyexcel/test/temp/LockTest.java

@ -24,7 +24,7 @@ public class LockTest {
@Test @Test
public void test() throws Exception { public void test() throws Exception {
List<Object> list = List<Object> 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) { for (Object data : list) {
LOGGER.info("返回数据:{}", JSON.toJSONString(data)); LOGGER.info("返回数据:{}", JSON.toJSONString(data));
} }

17
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;
}

22
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));
}
}

3
update.md

@ -1,5 +1,6 @@
# 3.1.0 # 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 # 3.0.5
* 修复`ReadListener` 转换异常不抛出的问题 * 修复`ReadListener` 转换异常不抛出的问题

Loading…
Cancel
Save