Browse Source

修复在mac 2016 2017导出的excel 可能存在多余字段的问题

bugfix
Jiaju Zhuang 5 years ago
parent
commit
cd2e0fe91e
  1. 22
      src/main/java/com/alibaba/excel/analysis/v07/SharedStringsTableHandler.java
  2. 2
      src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiFormatTest.java
  3. 4
      src/test/java/com/alibaba/easyexcel/test/temp/simple/HgTest.java
  4. 1
      update.md

22
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 { public class SharedStringsTableHandler extends DefaultHandler {
private static final String T_TAG = "t"; private static final String T_TAG = "t";
private static final String SI_TAG = "si"; 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 * The final piece of data
*/ */
@ -23,6 +28,14 @@ public class SharedStringsTableHandler extends DefaultHandler {
private StringBuilder currentElementData; private StringBuilder currentElementData;
private ReadCache readCache; 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) { public SharedStringsTableHandler(ReadCache readCache) {
this.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) { public void startElement(String uri, String localName, String name, Attributes attributes) {
if (T_TAG.equals(name)) { if (T_TAG.equals(name)) {
currentElementData = null; currentElementData = null;
isTagt = true;
} else if (SI_TAG.equals(name)) { } else if (SI_TAG.equals(name)) {
currentData = null; currentData = null;
} else if (RPH_TAG.equals(name)) {
ignoreTagt = true;
} }
} }
@ -46,17 +62,23 @@ public class SharedStringsTableHandler extends DefaultHandler {
} }
currentData.append(currentElementData); currentData.append(currentElementData);
} }
isTagt = false;
} else if (SI_TAG.equals(name)) { } else if (SI_TAG.equals(name)) {
if (currentData == null) { if (currentData == null) {
readCache.put(null); readCache.put(null);
} else { } else {
readCache.put(currentData.toString()); readCache.put(currentData.toString());
} }
} else if (RPH_TAG.equals(name)) {
ignoreTagt = false;
} }
} }
@Override @Override
public void characters(char[] ch, int start, int length) { public void characters(char[] ch, int start, int length) {
if (!isTagt || ignoreTagt) {
return;
}
if (currentElementData == null) { if (currentElementData == null) {
currentElementData = new StringBuilder(); currentElementData = new StringBuilder();
} }

2
src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiFormatTest.java

@ -28,7 +28,7 @@ public class PoiFormatTest {
@Test @Test
public void lastRowNum() throws IOException { 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)); SXSSFWorkbook xssfWorkbook = new SXSSFWorkbook(new XSSFWorkbook(file));
SXSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0); SXSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
LOGGER.info("一共行数:{}", xssfSheet.getLastRowNum()); LOGGER.info("一共行数:{}", xssfSheet.getLastRowNum());

4
src/test/java/com/alibaba/easyexcel/test/temp/simple/HgTest.java

@ -23,8 +23,8 @@ public class HgTest {
@Test @Test
public void hh() throws IOException { public void hh() throws IOException {
List<Object> list = EasyExcel.read(new FileInputStream("D:\\test\\hg2.xls")).autoTrim(Boolean.FALSE).sheet(2) List<Object> list =
.headRowNumber(0).doReadSync(); EasyExcel.read(new FileInputStream("D:\\test\\原文件.xlsx")).headRowNumber(0).sheet().doReadSync();
for (Object data : list) { for (Object data : list) {
LOGGER.info("返回数据:{}", JSON.toJSONString(data)); LOGGER.info("返回数据:{}", JSON.toJSONString(data));
} }

1
update.md

@ -1,5 +1,6 @@
# 2.0.0-beta4 # 2.0.0-beta4
* 修改在传入List<List<Object>>判断行数错误 [Issue #526](https://github.com/alibaba/easyexcel/issues/526) * 修改在传入List<List<Object>>判断行数错误 [Issue #526](https://github.com/alibaba/easyexcel/issues/526)
* 修复在mac 2016 2017导出的excel 可能存在多余字段的问题
# 2.0.0-beta3 # 2.0.0-beta3
* 导出完成移除临时目录 [Issue #386](https://github.com/alibaba/easyexcel/issues/386) * 导出完成移除临时目录 [Issue #386](https://github.com/alibaba/easyexcel/issues/386)

Loading…
Cancel
Save