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 cca10f65..957dd91c 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 99885fa9..1f31f9cc 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 d2ebb9fa..62e5b154 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 2b350d44..0a4387d4 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)