From cd2e0fe91ed3aabed4d7f1e9f1440aec1fe9f0e8 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Thu, 5 Sep 2019 17:05:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=9C=A8mac=202016=202017?= =?UTF-8?q?=E5=AF=BC=E5=87=BA=E7=9A=84excel=20=E5=8F=AF=E8=83=BD=E5=AD=98?= =?UTF-8?q?=E5=9C=A8=E5=A4=9A=E4=BD=99=E5=AD=97=E6=AE=B5=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../v07/SharedStringsTableHandler.java | 22 +++++++++++++++++++ .../test/temp/poi/PoiFormatTest.java | 2 +- .../easyexcel/test/temp/simple/HgTest.java | 4 ++-- update.md | 1 + 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/alibaba/excel/analysis/v07/SharedStringsTableHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/SharedStringsTableHandler.java index cca10f6..957dd91 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/SharedStringsTableHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/SharedStringsTableHandler.java @@ -13,6 +13,11 @@ import com.alibaba.excel.cache.ReadCache; public class SharedStringsTableHandler extends DefaultHandler { private static final String T_TAG = "t"; private static final String SI_TAG = "si"; + /** + * Mac 2016 2017 will have this extra field to ignore + */ + private static final String RPH_TAG = "rPh"; + /** * The final piece of data */ @@ -23,6 +28,14 @@ public class SharedStringsTableHandler extends DefaultHandler { private StringBuilder currentElementData; private ReadCache readCache; + /** + * Some fields in the T tag need to be ignored + */ + private boolean ignoreTagt = false; + /** + * The only time you need to read the characters in the T tag is when it is used + */ + private boolean isTagt = false; public SharedStringsTableHandler(ReadCache readCache) { this.readCache = readCache; @@ -32,8 +45,11 @@ public class SharedStringsTableHandler extends DefaultHandler { public void startElement(String uri, String localName, String name, Attributes attributes) { if (T_TAG.equals(name)) { currentElementData = null; + isTagt = true; } else if (SI_TAG.equals(name)) { currentData = null; + } else if (RPH_TAG.equals(name)) { + ignoreTagt = true; } } @@ -46,17 +62,23 @@ public class SharedStringsTableHandler extends DefaultHandler { } currentData.append(currentElementData); } + isTagt = false; } else if (SI_TAG.equals(name)) { if (currentData == null) { readCache.put(null); } else { readCache.put(currentData.toString()); } + } else if (RPH_TAG.equals(name)) { + ignoreTagt = false; } } @Override public void characters(char[] ch, int start, int length) { + if (!isTagt || ignoreTagt) { + return; + } if (currentElementData == null) { currentElementData = new StringBuilder(); } diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiFormatTest.java b/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiFormatTest.java index 99885fa..1f31f9c 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiFormatTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiFormatTest.java @@ -28,7 +28,7 @@ public class PoiFormatTest { @Test public void lastRowNum() throws IOException { - String file = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx"; + String file = "D:\\test\\原文件.xlsx"; SXSSFWorkbook xssfWorkbook = new SXSSFWorkbook(new XSSFWorkbook(file)); SXSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0); LOGGER.info("一共行数:{}", xssfSheet.getLastRowNum()); diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/simple/HgTest.java b/src/test/java/com/alibaba/easyexcel/test/temp/simple/HgTest.java index d2ebb9f..62e5b15 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/simple/HgTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/simple/HgTest.java @@ -23,8 +23,8 @@ public class HgTest { @Test public void hh() throws IOException { - List list = EasyExcel.read(new FileInputStream("D:\\test\\hg2.xls")).autoTrim(Boolean.FALSE).sheet(2) - .headRowNumber(0).doReadSync(); + List list = + EasyExcel.read(new FileInputStream("D:\\test\\原文件.xlsx")).headRowNumber(0).sheet().doReadSync(); for (Object data : list) { LOGGER.info("返回数据:{}", JSON.toJSONString(data)); } diff --git a/update.md b/update.md index 2b350d4..0a4387d 100644 --- a/update.md +++ b/update.md @@ -1,5 +1,6 @@ # 2.0.0-beta4 * 修改在传入List>判断行数错误 [Issue #526](https://github.com/alibaba/easyexcel/issues/526) +* 修复在mac 2016 2017导出的excel 可能存在多余字段的问题 # 2.0.0-beta3 * 导出完成移除临时目录 [Issue #386](https://github.com/alibaba/easyexcel/issues/386)