diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/SharedStringsTableHandler.java b/easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/SharedStringsTableHandler.java index 83e92df3..203db6c7 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/SharedStringsTableHandler.java +++ b/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: diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/XlsxRowHandler.java b/easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/XlsxRowHandler.java index 61805cd5..7b48c8a6 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/XlsxRowHandler.java +++ b/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 XLSX_CELL_HANDLER_MAP = new HashMap(32); + private static final Map 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) { diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/constant/ExcelXmlConstants.java b/easyexcel-core/src/main/java/com/alibaba/excel/constant/ExcelXmlConstants.java index 8d50c18d..c1d33592 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/constant/ExcelXmlConstants.java +++ b/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"; } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/compatibility/CompatibilityTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/compatibility/CompatibilityTest.java index 3b0b525a..eadfd0a8 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/compatibility/CompatibilityTest.java +++ b/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> 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> list = EasyExcel.read(TestFileUtil.getPath() + "compatibility/t03.xlsx").sheet() + .doReadSync(); log.info("data:{}", JSON.toJSONString(list)); Assert.assertEquals(1, list.size()); Map 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> list = EasyExcel.read(TestFileUtil.getPath() + "compatibility/t04.xlsx").sheet() + .doReadSync(); + log.info("data:{}", JSON.toJSONString(list)); + Assert.assertEquals(56, list.size()); + Map row0 = list.get(0); + Assert.assertEquals("QQSJK28F152A012242S0081", row0.get(5)); + } } diff --git a/easyexcel-test/src/test/resources/compatibility/t04.xlsx b/easyexcel-test/src/test/resources/compatibility/t04.xlsx new file mode 100644 index 00000000..7c95d425 Binary files /dev/null and b/easyexcel-test/src/test/resources/compatibility/t04.xlsx differ diff --git a/pom.xml b/pom.xml index eca83ca2..de5d0681 100644 --- a/pom.xml +++ b/pom.xml @@ -20,7 +20,7 @@ - 3.1.4 + 3.1.5 UTF-8 1.8 true diff --git a/update.md b/update.md index 2c698a0b..782e7a80 100644 --- a/update.md +++ b/update.md @@ -1,3 +1,7 @@ +# 3.1.5 + +* 提高xlsx读取兼容性:兼用ns2开头的标签 + # 3.1.4 * 提高xlsx读取兼容性:在存在第一行很多空列的情况下,忽略空列