Skip to content

Commit

Permalink
修复在mac 2016 2017导出的excel 可能存在多余字段的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuangjiaju committed Sep 5, 2019
1 parent 120dfbd commit cd2e0fe
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
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
*/
Expand All @@ -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;
Expand All @@ -32,8 +45,11 @@ public SharedStringsTableHandler(ReadCache readCache) {
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;
}
}

Expand All @@ -46,17 +62,23 @@ public void endElement(String uri, String localName, String name) {
}
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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public class HgTest {

@Test
public void hh() throws IOException {
List<Object> list = EasyExcel.read(new FileInputStream("D:\\test\\hg2.xls")).autoTrim(Boolean.FALSE).sheet(2)
.headRowNumber(0).doReadSync();
List<Object> list =
EasyExcel.read(new FileInputStream("D:\\test\\原文件.xlsx")).headRowNumber(0).sheet().doReadSync();
for (Object data : list) {
LOGGER.info("返回数据:{}", JSON.toJSONString(data));
}
Expand Down
1 change: 1 addition & 0 deletions update.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# 2.0.0-beta4
* 修改在传入List<List<Object>>判断行数错误 [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)
Expand Down

0 comments on commit cd2e0fe

Please sign in to comment.