Browse Source

* 提供xlsx兼容性:兼容`sharedStrings.xml` 存储在 `x:t`标签

pull/2815/head
Jiaju Zhuang 2 years ago
parent
commit
0d635644ba
  1. 2
      README.md
  2. 73
      easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/SharedStringsTableHandler.java
  3. 24
      easyexcel-core/src/main/java/com/alibaba/excel/constant/ExcelXmlConstants.java
  4. 12
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/compatibility/CompatibilityTest.java
  5. BIN
      easyexcel-test/src/test/resources/compatibility/t02.xlsx
  6. 2
      pom.xml

2
README.md

@ -35,7 +35,7 @@ Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.1.2</version>
<version>3.1.3</version>
</dependency>
```

73
easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/SharedStringsTableHandler.java

@ -4,6 +4,7 @@ import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;
import com.alibaba.excel.cache.ReadCache;
import com.alibaba.excel.constant.ExcelXmlConstants;
/**
* Sax read sharedStringsTable.xml
@ -11,12 +12,6 @@ import com.alibaba.excel.cache.ReadCache;
* @author Jiaju Zhuang
*/
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
@ -43,34 +38,58 @@ public class SharedStringsTableHandler extends DefaultHandler {
@Override
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;
if (name == null) {
return;
}
switch (name) {
case ExcelXmlConstants.SHAREDSTRINGS_T_TAG:
case ExcelXmlConstants.SHAREDSTRINGS_X_T_TAG:
currentElementData = null;
isTagt = true;
break;
case ExcelXmlConstants.SHAREDSTRINGS_SI_TAG:
case ExcelXmlConstants.SHAREDSTRINGS_X_SI_TAG:
currentData = null;
break;
case ExcelXmlConstants.SHAREDSTRINGS_RPH_TAG:
case ExcelXmlConstants.SHAREDSTRINGS_X_RPH_TAG:
ignoreTagt = true;
break;
default:
// ignore
}
}
@Override
public void endElement(String uri, String localName, String name) {
if (T_TAG.equals(name)) {
if (currentElementData != null) {
if (name == null) {
return;
}
switch (name) {
case ExcelXmlConstants.SHAREDSTRINGS_T_TAG:
case ExcelXmlConstants.SHAREDSTRINGS_X_T_TAG:
if (currentElementData != null) {
if (currentData == null) {
currentData = new StringBuilder();
}
currentData.append(currentElementData);
}
isTagt = false;
break;
case ExcelXmlConstants.SHAREDSTRINGS_SI_TAG:
case ExcelXmlConstants.SHAREDSTRINGS_X_SI_TAG:
if (currentData == null) {
currentData = new StringBuilder();
readCache.put(null);
} else {
readCache.put(currentData.toString());
}
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;
break;
case ExcelXmlConstants.SHAREDSTRINGS_RPH_TAG:
case ExcelXmlConstants.SHAREDSTRINGS_X_RPH_TAG:
ignoreTagt = false;
break;
default:
// ignore
}
}

24
easyexcel-core/src/main/java/com/alibaba/excel/constant/ExcelXmlConstants.java

@ -59,4 +59,28 @@ public class ExcelXmlConstants {
*/
public static final String CELL_RANGE_SPLIT = ":";
// The following is a constant read the `SharedStrings.xml`
/**
* text
* https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet
* .text?redirectedfrom=MSDN&view=openxml-2.8.1
*/
public static final String SHAREDSTRINGS_T_TAG = "t";
public static final String SHAREDSTRINGS_X_T_TAG = "x:t";
/**
* SharedStringItem
* https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet
* .sharedstringitem?redirectedfrom=MSDN&view=openxml-2.8.1
*/
public static final String SHAREDSTRINGS_SI_TAG = "si";
public static final String SHAREDSTRINGS_X_SI_TAG = "x: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";
}

12
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/compatibility/CompatibilityTest.java

@ -5,6 +5,7 @@ import java.util.Map;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
@ -30,4 +31,15 @@ public class CompatibilityTest {
Map<Integer, Object> row1 = list.get(1);
Assert.assertEquals("Q235(碳钢)", row1.get(0));
}
@Test
public void t02() {
// Exist in `sharedStrings.xml` `x:t` start tag, need to be compatible
List<Map<Integer, Object>> list = EasyExcel.read(TestFileUtil.getPath() + "compatibility/t02.xlsx").sheet()
.headRowNumber(0).doReadSync();
log.info("data:{}", JSON.toJSONString(list));
Assert.assertEquals(3, list.size());
Map<Integer, Object> row2 = list.get(2);
Assert.assertEquals("1,2-戊二醇", row2.get(2));
}
}

BIN
easyexcel-test/src/test/resources/compatibility/t02.xlsx

Binary file not shown.

2
pom.xml

@ -20,7 +20,7 @@
<properties>
<revision>3.1.2</revision>
<revision>3.1.3</revision>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdk.version>1.8</jdk.version>
<gpg.skip>true</gpg.skip>

Loading…
Cancel
Save