diff --git a/pom.xml b/pom.xml index 9f1a4c72..6be301da 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.alibaba easyexcel - 2.2.10 + 2.2.11 jar easyexcel diff --git a/src/main/java/com/alibaba/excel/analysis/v07/handlers/AbstractCellValueTagHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/handlers/AbstractCellValueTagHandler.java index 580e6704..7a982d5d 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/handlers/AbstractCellValueTagHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/handlers/AbstractCellValueTagHandler.java @@ -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); - } diff --git a/src/main/java/com/alibaba/excel/analysis/v07/handlers/CellInlineStringValueTagHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/handlers/CellInlineStringValueTagHandler.java index 277348a7..46cfc0d6 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/handlers/CellInlineStringValueTagHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/handlers/CellInlineStringValueTagHandler.java @@ -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()); - } - } diff --git a/src/main/java/com/alibaba/excel/analysis/v07/handlers/CellTagHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/handlers/CellTagHandler.java index fe5a6421..c71b744c 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/handlers/CellTagHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/handlers/CellTagHandler.java @@ -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); + } } diff --git a/src/main/java/com/alibaba/excel/analysis/v07/handlers/CellValueTagHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/handlers/CellValueTagHandler.java index 7e685c97..d9a54ff2 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/handlers/CellValueTagHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/handlers/CellValueTagHandler.java @@ -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: - } - } - } diff --git a/src/main/java/com/alibaba/excel/enums/CellDataTypeEnum.java b/src/main/java/com/alibaba/excel/enums/CellDataTypeEnum.java index 44efc179..b48e40b2 100644 --- a/src/main/java/com/alibaba/excel/enums/CellDataTypeEnum.java +++ b/src/main/java/com/alibaba/excel/enums/CellDataTypeEnum.java @@ -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); diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java b/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java index 92202642..ae2e03fe 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java @@ -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 list = EasyExcel.read(file).sheet(0).headRowNumber(0).doReadSync(); LOGGER.info("数据:{}", list.size()); diff --git a/update.md b/update.md index f2d889a4..c90c4aea 100644 --- a/update.md +++ b/update.md @@ -1,3 +1,6 @@ +# 2.2.11 +* 修复有些xlsx解析失败的bug [Issue #1595](https://github.com/alibaba/easyexcel/issues/1595) + # 2.2.10 * 修复读取的时候用string接收数字 可能四舍五入不一致的bug