Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix]修复 inlineStr 类型可能存在的数据读取错误 #3861

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.alibaba.excel.analysis.v07.handlers.sax;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.excel.analysis.v07.handlers.CellFormulaTagHandler;
import com.alibaba.excel.analysis.v07.handlers.CellInlineStringValueTagHandler;
import com.alibaba.excel.analysis.v07.handlers.CellTagHandler;
Expand All @@ -14,12 +11,15 @@
import com.alibaba.excel.analysis.v07.handlers.XlsxTagHandler;
import com.alibaba.excel.constant.ExcelXmlConstants;
import com.alibaba.excel.context.xlsx.XlsxReadContext;

import com.alibaba.excel.enums.CellDataTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.util.HashMap;
import java.util.Map;

/**
* @author jipengfei
*/
Expand Down Expand Up @@ -87,6 +87,12 @@ public void characters(char[] ch, int start, int length) throws SAXException {
if (handler == null || !handler.support(xlsxReadContext)) {
return;
}
//If the cell type is 'inLineStr', then the label <v> must be ignored
if (xlsxReadContext.xlsxReadSheetHolder().getTempCellData() != null
&& CellDataTypeEnum.DIRECT_STRING.equals(xlsxReadContext.xlsxReadSheetHolder().getTempCellData().getType())
&& ExcelXmlConstants.CELL_VALUE_TAG.equals(currentTag)) {
return;
}
handler.characters(xlsxReadContext, ch, start, length);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public enum CellDataTypeEnum {

static {
TYPE_ROUTING_MAP.put("s", STRING);
TYPE_ROUTING_MAP.put("str", DIRECT_STRING);
TYPE_ROUTING_MAP.put("str", ERROR);
TYPE_ROUTING_MAP.put("inlineStr", DIRECT_STRING);
TYPE_ROUTING_MAP.put("e", ERROR);
TYPE_ROUTING_MAP.put("b", BOOLEAN);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.alibaba.easyexcel.test.temp.issue3823;

import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.fastjson2.JSON;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;

import java.io.File;

public class Issue3823Test {
//Issue link: https://github.com/alibaba/easyexcel/issues/3823
@Test
public void IssueTest() throws Exception {

String fileName = TestFileUtil.getPath() + "temp" + File.separator + "issue3823" + File.separator + "bug.xlsx";

EasyExcel.read(fileName, new ReadListener() {
@Override
public void invoke(Object data, AnalysisContext context) {
System.out.println(JSON.toJSONString(data));
}

@Override
public void doAfterAllAnalysed(AnalysisContext context) {

}
}).sheet().doRead();


XSSFWorkbook workbook = new XSSFWorkbook(fileName);
XSSFSheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
StringBuilder sb = new StringBuilder();
for (Cell cell : row) {
sb.append(cell.getStringCellValue()).append(" ");
}
System.out.println(sb);
}
}

}
Binary file not shown.