Browse Source

Merge pull request #2897 from alibaba/bugfix

Bugfix
pull/2906/head v3.1.5
Jiaju Zhuang 2 years ago committed by GitHub
parent
commit
2776949811
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      README.md
  2. 6
      easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/SharedStringsTableHandler.java
  3. 10
      easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/XlsxRowHandler.java
  4. 17
      easyexcel-core/src/main/java/com/alibaba/excel/constant/ExcelXmlConstants.java
  5. 16
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/compatibility/CompatibilityTest.java
  6. BIN
      easyexcel-test/src/test/resources/compatibility/t04.xlsx
  7. 2
      pom.xml
  8. 4
      update.md

2
README.md

@ -36,7 +36,7 @@ Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.1.4</version>
<version>3.1.5</version>
</dependency>
```

6
easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/SharedStringsTableHandler.java

@ -44,15 +44,18 @@ public class SharedStringsTableHandler extends DefaultHandler {
switch (name) {
case ExcelXmlConstants.SHAREDSTRINGS_T_TAG:
case ExcelXmlConstants.SHAREDSTRINGS_X_T_TAG:
case ExcelXmlConstants.SHAREDSTRINGS_NS2_T_TAG:
currentElementData = null;
isTagt = true;
break;
case ExcelXmlConstants.SHAREDSTRINGS_SI_TAG:
case ExcelXmlConstants.SHAREDSTRINGS_X_SI_TAG:
case ExcelXmlConstants.SHAREDSTRINGS_NS2_SI_TAG:
currentData = null;
break;
case ExcelXmlConstants.SHAREDSTRINGS_RPH_TAG:
case ExcelXmlConstants.SHAREDSTRINGS_X_RPH_TAG:
case ExcelXmlConstants.SHAREDSTRINGS_NS2_RPH_TAG:
ignoreTagt = true;
break;
default:
@ -68,6 +71,7 @@ public class SharedStringsTableHandler extends DefaultHandler {
switch (name) {
case ExcelXmlConstants.SHAREDSTRINGS_T_TAG:
case ExcelXmlConstants.SHAREDSTRINGS_X_T_TAG:
case ExcelXmlConstants.SHAREDSTRINGS_NS2_T_TAG:
if (currentElementData != null) {
if (currentData == null) {
currentData = new StringBuilder();
@ -78,6 +82,7 @@ public class SharedStringsTableHandler extends DefaultHandler {
break;
case ExcelXmlConstants.SHAREDSTRINGS_SI_TAG:
case ExcelXmlConstants.SHAREDSTRINGS_X_SI_TAG:
case ExcelXmlConstants.SHAREDSTRINGS_NS2_SI_TAG:
if (currentData == null) {
readCache.put(null);
} else {
@ -86,6 +91,7 @@ public class SharedStringsTableHandler extends DefaultHandler {
break;
case ExcelXmlConstants.SHAREDSTRINGS_RPH_TAG:
case ExcelXmlConstants.SHAREDSTRINGS_X_RPH_TAG:
case ExcelXmlConstants.SHAREDSTRINGS_NS2_RPH_TAG:
ignoreTagt = false;
break;
default:

10
easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/XlsxRowHandler.java

@ -26,33 +26,41 @@ import org.xml.sax.helpers.DefaultHandler;
@Slf4j
public class XlsxRowHandler extends DefaultHandler {
private final XlsxReadContext xlsxReadContext;
private static final Map<String, XlsxTagHandler> XLSX_CELL_HANDLER_MAP = new HashMap<String, XlsxTagHandler>(32);
private static final Map<String, XlsxTagHandler> XLSX_CELL_HANDLER_MAP = new HashMap<>(64);
static {
CellFormulaTagHandler cellFormulaTagHandler = new CellFormulaTagHandler();
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.CELL_FORMULA_TAG, cellFormulaTagHandler);
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.X_CELL_FORMULA_TAG, cellFormulaTagHandler);
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.NS2_CELL_FORMULA_TAG, cellFormulaTagHandler);
CellInlineStringValueTagHandler cellInlineStringValueTagHandler = new CellInlineStringValueTagHandler();
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.CELL_INLINE_STRING_VALUE_TAG, cellInlineStringValueTagHandler);
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.X_CELL_INLINE_STRING_VALUE_TAG, cellInlineStringValueTagHandler);
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.NS2_CELL_INLINE_STRING_VALUE_TAG, cellInlineStringValueTagHandler);
CellTagHandler cellTagHandler = new CellTagHandler();
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.CELL_TAG, cellTagHandler);
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.X_CELL_TAG, cellTagHandler);
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.NS2_CELL_TAG, cellTagHandler);
CellValueTagHandler cellValueTagHandler = new CellValueTagHandler();
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.CELL_VALUE_TAG, cellValueTagHandler);
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.X_CELL_VALUE_TAG, cellValueTagHandler);
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.NS2_CELL_VALUE_TAG, cellValueTagHandler);
CountTagHandler countTagHandler = new CountTagHandler();
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.DIMENSION_TAG, countTagHandler);
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.X_DIMENSION_TAG, countTagHandler);
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.NS2_DIMENSION_TAG, countTagHandler);
HyperlinkTagHandler hyperlinkTagHandler = new HyperlinkTagHandler();
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.HYPERLINK_TAG, hyperlinkTagHandler);
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.X_HYPERLINK_TAG, hyperlinkTagHandler);
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.NS2_HYPERLINK_TAG, hyperlinkTagHandler);
MergeCellTagHandler mergeCellTagHandler = new MergeCellTagHandler();
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.MERGE_CELL_TAG, mergeCellTagHandler);
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.X_MERGE_CELL_TAG, mergeCellTagHandler);
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.NS2_MERGE_CELL_TAG, mergeCellTagHandler);
RowTagHandler rowTagHandler = new RowTagHandler();
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.ROW_TAG, rowTagHandler);
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.X_ROW_TAG, rowTagHandler);
XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.NS2_ROW_TAG, rowTagHandler);
}
public XlsxRowHandler(XlsxReadContext xlsxReadContext) {

17
easyexcel-core/src/main/java/com/alibaba/excel/constant/ExcelXmlConstants.java

@ -17,16 +17,28 @@ public class ExcelXmlConstants {
public static final String HYPERLINK_TAG = "hyperlink";
public static final String X_DIMENSION_TAG = "x:dimension";
public static final String NS2_DIMENSION_TAG = "ns2:dimension";
public static final String X_ROW_TAG = "x:row";
public static final String NS2_ROW_TAG = "ns2:row";
public static final String X_CELL_FORMULA_TAG = "x:f";
public static final String NS2_CELL_FORMULA_TAG = "ns2:f";
public static final String X_CELL_VALUE_TAG = "x:v";
public static final String NS2_CELL_VALUE_TAG = "ns2:v";
/**
* When the data is "inlineStr" his tag is "t"
*/
public static final String X_CELL_INLINE_STRING_VALUE_TAG = "x:t";
public static final String NS2_CELL_INLINE_STRING_VALUE_TAG = "ns2:t";
public static final String X_CELL_TAG = "x:c";
public static final String NS2_CELL_TAG = "ns2:c";
public static final String X_MERGE_CELL_TAG = "x:mergeCell";
public static final String NS2_MERGE_CELL_TAG = "ns2:mergeCell";
public static final String X_HYPERLINK_TAG = "x:hyperlink";
public static final String NS2_HYPERLINK_TAG = "ns2:hyperlink";
/**
* s attribute
@ -66,17 +78,22 @@ public class ExcelXmlConstants {
*/
public static final String SHAREDSTRINGS_T_TAG = "t";
public static final String SHAREDSTRINGS_X_T_TAG = "x:t";
public static final String SHAREDSTRINGS_NS2_T_TAG = "ns2:t";
/**
* SharedStringItem
*/
public static final String SHAREDSTRINGS_SI_TAG = "si";
public static final String SHAREDSTRINGS_X_SI_TAG = "x:si";
public static final String SHAREDSTRINGS_NS2_SI_TAG = "ns2:si";
/**
* Mac 2016 2017 will have this extra field to ignore
*/
public static final String SHAREDSTRINGS_RPH_TAG = "rPh";
public static final String SHAREDSTRINGS_X_RPH_TAG = "x:rPh";
public static final String SHAREDSTRINGS_NS2_RPH_TAG = "ns2:rPh";
}

16
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/compatibility/CompatibilityTest.java

@ -45,11 +45,23 @@ public class CompatibilityTest {
@Test
public void t03() {
// Exist in `sharedStrings.xml` `x:t` start tag, need to be compatible
List<Map<Integer, Object>> list = EasyExcel.read(TestFileUtil.getPath() + "compatibility/t03.xlsx").sheet().doReadSync();
// In the presence of the first line of a lot of null columns, ignore null columns
List<Map<Integer, Object>> list = EasyExcel.read(TestFileUtil.getPath() + "compatibility/t03.xlsx").sheet()
.doReadSync();
log.info("data:{}", JSON.toJSONString(list));
Assert.assertEquals(1, list.size());
Map<Integer, Object> row0 = list.get(0);
Assert.assertEquals(12, row0.size());
}
@Test
public void t04() {
// Exist in `sheet1.xml` `ns2:t` start tag, need to be compatible
List<Map<Integer, Object>> list = EasyExcel.read(TestFileUtil.getPath() + "compatibility/t04.xlsx").sheet()
.doReadSync();
log.info("data:{}", JSON.toJSONString(list));
Assert.assertEquals(56, list.size());
Map<Integer, Object> row0 = list.get(0);
Assert.assertEquals("QQSJK28F152A012242S0081", row0.get(5));
}
}

BIN
easyexcel-test/src/test/resources/compatibility/t04.xlsx

Binary file not shown.

2
pom.xml

@ -20,7 +20,7 @@
<properties>
<revision>3.1.4</revision>
<revision>3.1.5</revision>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdk.version>1.8</jdk.version>
<gpg.skip>true</gpg.skip>

4
update.md

@ -1,3 +1,7 @@
# 3.1.5
* 提高xlsx读取兼容性:兼用ns2开头的标签
# 3.1.4
* 提高xlsx读取兼容性:在存在第一行很多空列的情况下,忽略空列

Loading…
Cancel
Save