修复有些xlsx解析失败的bug [Issue #1595]
This commit is contained in:
parent
5797cb3476
commit
d4bc63a5e2
2
pom.xml
2
pom.xml
@ -4,7 +4,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>easyexcel</artifactId>
|
||||
<version>2.2.10</version>
|
||||
<version>2.2.11</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>easyexcel</name>
|
||||
|
||||
|
@ -1,13 +1,6 @@
|
||||
package com.alibaba.excel.analysis.v07.handlers;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import com.alibaba.excel.context.xlsx.XlsxReadContext;
|
||||
import com.alibaba.excel.enums.CellDataTypeEnum;
|
||||
import com.alibaba.excel.metadata.CellData;
|
||||
import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder;
|
||||
import com.alibaba.excel.util.BooleanUtils;
|
||||
import com.alibaba.excel.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Cell Value Handler
|
||||
@ -16,61 +9,9 @@ import com.alibaba.excel.util.StringUtils;
|
||||
*/
|
||||
public abstract class AbstractCellValueTagHandler extends AbstractXlsxTagHandler {
|
||||
|
||||
@Override
|
||||
public void endElement(XlsxReadContext xlsxReadContext, String name) {
|
||||
XlsxReadSheetHolder xlsxReadSheetHolder = xlsxReadContext.xlsxReadSheetHolder();
|
||||
CellData tempCellData = xlsxReadSheetHolder.getTempCellData();
|
||||
StringBuilder tempData = xlsxReadSheetHolder.getTempData();
|
||||
String tempDataString = tempData.toString();
|
||||
CellDataTypeEnum oldType = tempCellData.getType();
|
||||
switch (oldType) {
|
||||
case DIRECT_STRING:
|
||||
case STRING:
|
||||
case ERROR:
|
||||
tempCellData.setStringValue(tempData.toString());
|
||||
break;
|
||||
case BOOLEAN:
|
||||
if(StringUtils.isEmpty(tempDataString)){
|
||||
tempCellData.setType(CellDataTypeEnum.EMPTY);
|
||||
break;
|
||||
}
|
||||
tempCellData.setBooleanValue(BooleanUtils.valueOf(tempData.toString()));
|
||||
break;
|
||||
case NUMBER:
|
||||
case EMPTY:
|
||||
if(StringUtils.isEmpty(tempDataString)){
|
||||
tempCellData.setType(CellDataTypeEnum.EMPTY);
|
||||
break;
|
||||
}
|
||||
tempCellData.setType(CellDataTypeEnum.NUMBER);
|
||||
tempCellData.setNumberValue(BigDecimal.valueOf(Double.parseDouble(tempDataString)));
|
||||
break;
|
||||
default:
|
||||
throw new IllegalStateException("Cannot set values now");
|
||||
}
|
||||
|
||||
// set string value
|
||||
setStringValue(xlsxReadContext);
|
||||
|
||||
if (tempCellData.getStringValue() != null
|
||||
&& xlsxReadContext.currentReadHolder().globalConfiguration().getAutoTrim()) {
|
||||
tempCellData.setStringValue(tempCellData.getStringValue());
|
||||
}
|
||||
|
||||
tempCellData.checkEmpty();
|
||||
xlsxReadSheetHolder.getCellMap().put(xlsxReadSheetHolder.getColumnIndex(), tempCellData);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void characters(XlsxReadContext xlsxReadContext, char[] ch, int start, int length) {
|
||||
xlsxReadContext.xlsxReadSheetHolder().getTempData().append(ch, start, length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set string value.
|
||||
*
|
||||
* @param xlsxReadContext
|
||||
*/
|
||||
protected abstract void setStringValue(XlsxReadContext xlsxReadContext);
|
||||
|
||||
}
|
||||
|
@ -1,10 +1,5 @@
|
||||
package com.alibaba.excel.analysis.v07.handlers;
|
||||
|
||||
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
|
||||
|
||||
import com.alibaba.excel.context.xlsx.XlsxReadContext;
|
||||
import com.alibaba.excel.metadata.CellData;
|
||||
|
||||
/**
|
||||
* Cell inline string value handler
|
||||
*
|
||||
@ -12,12 +7,4 @@ import com.alibaba.excel.metadata.CellData;
|
||||
*/
|
||||
public class CellInlineStringValueTagHandler extends AbstractCellValueTagHandler {
|
||||
|
||||
@Override
|
||||
protected void setStringValue(XlsxReadContext xlsxReadContext) {
|
||||
// This is a special form of string
|
||||
CellData tempCellData = xlsxReadContext.xlsxReadSheetHolder().getTempCellData();
|
||||
XSSFRichTextString richTextString = new XSSFRichTextString(tempCellData.getStringValue());
|
||||
tempCellData.setStringValue(richTextString.toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package com.alibaba.excel.analysis.v07.handlers;
|
||||
|
||||
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
|
||||
import org.xml.sax.Attributes;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import com.alibaba.excel.constant.BuiltinFormats;
|
||||
import com.alibaba.excel.constant.ExcelXmlConstants;
|
||||
@ -9,9 +8,13 @@ import com.alibaba.excel.context.xlsx.XlsxReadContext;
|
||||
import com.alibaba.excel.enums.CellDataTypeEnum;
|
||||
import com.alibaba.excel.metadata.CellData;
|
||||
import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder;
|
||||
import com.alibaba.excel.util.BooleanUtils;
|
||||
import com.alibaba.excel.util.PositionUtils;
|
||||
import com.alibaba.excel.util.StringUtils;
|
||||
|
||||
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
|
||||
import org.xml.sax.Attributes;
|
||||
|
||||
/**
|
||||
* Cell Handler
|
||||
*
|
||||
@ -54,4 +57,54 @@ public class CellTagHandler extends AbstractXlsxTagHandler {
|
||||
xssfCellStyle.getDataFormatString(), xlsxReadSheetHolder.getGlobalConfiguration().getLocale()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endElement(XlsxReadContext xlsxReadContext, String name) {
|
||||
XlsxReadSheetHolder xlsxReadSheetHolder = xlsxReadContext.xlsxReadSheetHolder();
|
||||
CellData tempCellData = xlsxReadSheetHolder.getTempCellData();
|
||||
StringBuilder tempData = xlsxReadSheetHolder.getTempData();
|
||||
String tempDataString = tempData.toString();
|
||||
CellDataTypeEnum oldType = tempCellData.getType();
|
||||
switch (oldType) {
|
||||
case STRING:
|
||||
// In some cases, although cell type is a string, it may be an empty tag
|
||||
if (StringUtils.isEmpty(tempDataString)) {
|
||||
break;
|
||||
}
|
||||
String stringValue = xlsxReadContext.readWorkbookHolder().getReadCache().get(
|
||||
Integer.valueOf(tempDataString));
|
||||
tempCellData.setStringValue(stringValue);
|
||||
break;
|
||||
case DIRECT_STRING:
|
||||
case ERROR:
|
||||
tempCellData.setStringValue(tempDataString);
|
||||
tempCellData.setType(CellDataTypeEnum.STRING);
|
||||
break;
|
||||
case BOOLEAN:
|
||||
if (StringUtils.isEmpty(tempDataString)) {
|
||||
tempCellData.setType(CellDataTypeEnum.EMPTY);
|
||||
break;
|
||||
}
|
||||
tempCellData.setBooleanValue(BooleanUtils.valueOf(tempData.toString()));
|
||||
break;
|
||||
case NUMBER:
|
||||
case EMPTY:
|
||||
if (StringUtils.isEmpty(tempDataString)) {
|
||||
tempCellData.setType(CellDataTypeEnum.EMPTY);
|
||||
break;
|
||||
}
|
||||
tempCellData.setType(CellDataTypeEnum.NUMBER);
|
||||
tempCellData.setNumberValue(BigDecimal.valueOf(Double.parseDouble(tempDataString)));
|
||||
break;
|
||||
default:
|
||||
throw new IllegalStateException("Cannot set values now");
|
||||
}
|
||||
|
||||
if (tempCellData.getStringValue() != null
|
||||
&& xlsxReadContext.currentReadHolder().globalConfiguration().getAutoTrim()) {
|
||||
tempCellData.setStringValue(tempCellData.getStringValue());
|
||||
}
|
||||
|
||||
tempCellData.checkEmpty();
|
||||
xlsxReadSheetHolder.getCellMap().put(xlsxReadSheetHolder.getColumnIndex(), tempCellData);
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,5 @@
|
||||
package com.alibaba.excel.analysis.v07.handlers;
|
||||
|
||||
import com.alibaba.excel.context.xlsx.XlsxReadContext;
|
||||
import com.alibaba.excel.enums.CellDataTypeEnum;
|
||||
import com.alibaba.excel.metadata.CellData;
|
||||
import com.alibaba.excel.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Cell Value Handler
|
||||
*
|
||||
@ -12,28 +7,4 @@ import com.alibaba.excel.util.StringUtils;
|
||||
*/
|
||||
public class CellValueTagHandler extends AbstractCellValueTagHandler {
|
||||
|
||||
@Override
|
||||
protected void setStringValue(XlsxReadContext xlsxReadContext) {
|
||||
// Have to go "sharedStrings.xml" and get it
|
||||
CellData tempCellData = xlsxReadContext.xlsxReadSheetHolder().getTempCellData();
|
||||
switch (tempCellData.getType()) {
|
||||
case STRING:
|
||||
// In some cases, although cell type is a string, it may be an empty tag
|
||||
if(StringUtils.isEmpty(tempCellData.getStringValue())){
|
||||
break;
|
||||
}
|
||||
String stringValue = xlsxReadContext.readWorkbookHolder().getReadCache()
|
||||
.get(Integer.valueOf(tempCellData.getStringValue()));
|
||||
if (stringValue != null && xlsxReadContext.currentReadHolder().globalConfiguration().getAutoTrim()) {
|
||||
stringValue = stringValue.trim();
|
||||
}
|
||||
tempCellData.setStringValue(stringValue);
|
||||
break;
|
||||
case DIRECT_STRING:
|
||||
tempCellData.setType(CellDataTypeEnum.STRING);
|
||||
break;
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -45,7 +45,7 @@ public enum CellDataTypeEnum {
|
||||
static {
|
||||
TYPE_ROUTING_MAP.put("s", STRING);
|
||||
TYPE_ROUTING_MAP.put("str", DIRECT_STRING);
|
||||
TYPE_ROUTING_MAP.put("inlineStr", STRING);
|
||||
TYPE_ROUTING_MAP.put("inlineStr", DIRECT_STRING);
|
||||
TYPE_ROUTING_MAP.put("e", ERROR);
|
||||
TYPE_ROUTING_MAP.put("b", BOOLEAN);
|
||||
TYPE_ROUTING_MAP.put("n", NUMBER);
|
||||
|
@ -5,14 +5,6 @@ import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.poi.hssf.util.CellReference;
|
||||
import org.apache.poi.ss.usermodel.FillPatternType;
|
||||
import org.apache.poi.ss.usermodel.IndexedColors;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.alibaba.easyexcel.test.demo.write.DemoData;
|
||||
import com.alibaba.easyexcel.test.util.TestFileUtil;
|
||||
import com.alibaba.excel.EasyExcel;
|
||||
@ -21,6 +13,14 @@ import com.alibaba.excel.write.metadata.style.WriteFont;
|
||||
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
|
||||
import org.apache.poi.hssf.util.CellReference;
|
||||
import org.apache.poi.ss.usermodel.FillPatternType;
|
||||
import org.apache.poi.ss.usermodel.IndexedColors;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* 临时测试
|
||||
*
|
||||
@ -34,7 +34,7 @@ public class Lock2Test {
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
// File file = TestFileUtil.readUserHomeFile("test/test6.xls");
|
||||
File file = new File("/Users/zhuangjiaju/test/签到金模板-0507-v5.xlsx");
|
||||
File file = new File("/Users/zhuangjiaju/IdeaProjects/easyexcel/src/test/resources/converter/converter07.xlsx");
|
||||
|
||||
List<Object> list = EasyExcel.read(file).sheet(0).headRowNumber(0).doReadSync();
|
||||
LOGGER.info("数据:{}", list.size());
|
||||
|
Loading…
x
Reference in New Issue
Block a user