From 74e1a711df5c515030cab0eef6770d2b15a52928 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Tue, 24 Sep 2019 23:47:29 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=89=B9=E9=87=8F?= =?UTF-8?q?=E8=AF=BB=E5=8F=96=E5=8F=AF=E8=83=BD=E5=AF=BC=E8=87=B4=E6=95=B0?= =?UTF-8?q?=E5=AD=97=E4=B8=A2=E5=A4=B1=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../v07/SharedStringsTableHandler.java | 2 +- .../excel/analysis/v07/XlsxRowHandler.java | 2 +- .../analysis/v07/XlsxRowResultHolder.java | 6 +- .../v07/handlers/DefaultCellHandler.java | 103 +++++++++--------- 5 files changed, 60 insertions(+), 55 deletions(-) diff --git a/pom.xml b/pom.xml index 958a85d..3e76a55 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.alibaba easyexcel - 2.0.2 + 2.0.3 jar easyexcel diff --git a/src/main/java/com/alibaba/excel/analysis/v07/SharedStringsTableHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/SharedStringsTableHandler.java index 957dd91..5e001ed 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/SharedStringsTableHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/SharedStringsTableHandler.java @@ -82,6 +82,6 @@ public class SharedStringsTableHandler extends DefaultHandler { if (currentElementData == null) { currentElementData = new StringBuilder(); } - currentElementData.append(new String(ch, start, length)); + currentElementData.append(ch, start, length); } } diff --git a/src/main/java/com/alibaba/excel/analysis/v07/XlsxRowHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/XlsxRowHandler.java index 8306fc1..ececb47 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/XlsxRowHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/XlsxRowHandler.java @@ -49,7 +49,7 @@ public class XlsxRowHandler extends DefaultHandler { @Override public void characters(char[] ch, int start, int length) throws SAXException { if (rowResultHolder != null) { - rowResultHolder.appendCurrentCellValue(new String(ch, start, length)); + rowResultHolder.appendCurrentCellValue(ch, start, length); } } } diff --git a/src/main/java/com/alibaba/excel/analysis/v07/XlsxRowResultHolder.java b/src/main/java/com/alibaba/excel/analysis/v07/XlsxRowResultHolder.java index 953d45e..d13da73 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/XlsxRowResultHolder.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/XlsxRowResultHolder.java @@ -18,9 +18,11 @@ public interface XlsxRowResultHolder { /** * Append current 'cellValue' * - * @param currentCellValue + * @param ch + * @param start + * @param length */ - void appendCurrentCellValue(String currentCellValue); + void appendCurrentCellValue(char[] ch, int start, int length); /** * Get row content diff --git a/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java index c0ad706..c21d6cc 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java @@ -40,6 +40,9 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder private int curCol; private Map curRowContent = new TreeMap(); private CellData currentCellData; + private StringBuilder dataStringBuilder; + private StringBuilder formulaStringBuilder; + /** * Current style information */ @@ -77,6 +80,7 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder // t is null ,it's means Empty or Number CellDataTypeEnum type = CellDataTypeEnum.buildFromCellType(attributes.getValue(CELL_VALUE_TYPE_TAG)); currentCellData = new CellData(type); + dataStringBuilder = new StringBuilder(); // Put in data transformation information String dateFormatIndex = attributes.getValue(CELL_DATA_FORMAT_TAG); @@ -96,82 +100,81 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder // cell is formula if (CELL_FORMULA_TAG.equals(name)) { currentCellData.setFormula(Boolean.TRUE); + formulaStringBuilder = new StringBuilder(); } } @Override public void endHandle(String name) { - currentTagDeque.pop(); - if (CELL_VALUE_TAG.equals(name)) { - // Have to go "sharedStrings.xml" and get it - if (currentCellData.getType() == CellDataTypeEnum.STRING) { - String stringValue = analysisContext.readWorkbookHolder().getReadCache() - .get(Integer.valueOf(currentCellData.getStringValue())); + // cell is formula + if (CELL_FORMULA_TAG.equals(name)) { + currentCellData.setFormulaValue(formulaStringBuilder.toString()); + return; + } + if (CELL_VALUE_TAG.equals(name) || CELL_INLINE_STRING_VALUE_TAG.equals(name)) { + CellDataTypeEnum oldType = currentCellData.getType(); + switch (oldType) { + case DIRECT_STRING: + case STRING: + case ERROR: + currentCellData.setStringValue(dataStringBuilder.toString()); + break; + case BOOLEAN: + currentCellData.setBooleanValue(BooleanUtils.valueOf(dataStringBuilder.toString())); + break; + case NUMBER: + case EMPTY: + currentCellData.setType(CellDataTypeEnum.NUMBER); + currentCellData.setNumberValue(new BigDecimal(dataStringBuilder.toString())); + break; + default: + throw new IllegalStateException("Cannot set values now"); + } + + if (CELL_VALUE_TAG.equals(name)) { + // Have to go "sharedStrings.xml" and get it + if (currentCellData.getType() == CellDataTypeEnum.STRING) { + String stringValue = analysisContext.readWorkbookHolder().getReadCache() + .get(Integer.valueOf(currentCellData.getStringValue())); + if (stringValue != null + && analysisContext.currentReadHolder().globalConfiguration().getAutoTrim()) { + stringValue = stringValue.trim(); + } + currentCellData.setStringValue(stringValue); + } else if (currentCellData.getType() == CellDataTypeEnum.DIRECT_STRING) { + currentCellData.setType(CellDataTypeEnum.STRING); + } + } + // This is a special form of string + if (CELL_INLINE_STRING_VALUE_TAG.equals(name)) { + XSSFRichTextString richTextString = new XSSFRichTextString(currentCellData.getStringValue()); + String stringValue = richTextString.toString(); if (stringValue != null && analysisContext.currentReadHolder().globalConfiguration().getAutoTrim()) { stringValue = stringValue.trim(); } currentCellData.setStringValue(stringValue); - } else if (currentCellData.getType() == CellDataTypeEnum.DIRECT_STRING) { - currentCellData.setType(CellDataTypeEnum.STRING); } + currentCellData.checkEmpty(); curRowContent.put(curCol, currentCellData); } - // This is a special form of string - if (CELL_INLINE_STRING_VALUE_TAG.equals(name)) { - XSSFRichTextString richTextString = new XSSFRichTextString(currentCellData.getStringValue()); - String stringValue = richTextString.toString(); - if (stringValue != null && analysisContext.currentReadHolder().globalConfiguration().getAutoTrim()) { - stringValue = stringValue.trim(); - } - currentCellData.setStringValue(stringValue); - currentCellData.checkEmpty(); - curRowContent.put(curCol, currentCellData); - } + currentTagDeque.pop(); } @Override - public void appendCurrentCellValue(String currentCellValue) { - if (StringUtils.isEmpty(currentCellValue)) { - return; - } + public void appendCurrentCellValue(char[] ch, int start, int length) { String currentTag = currentTagDeque.peek(); if (currentTag == null) { return; } if (CELL_FORMULA_TAG.equals(currentTag)) { - currentCellData.setFormulaValue(currentCellValue); + formulaStringBuilder.append(ch, start, length); return; } if (!CELL_VALUE_TAG.equals(currentTag) && !CELL_INLINE_STRING_VALUE_TAG.equals(currentTag)) { return; } - CellDataTypeEnum oldType = currentCellData.getType(); - switch (oldType) { - case DIRECT_STRING: - case STRING: - case ERROR: - if (currentCellData.getStringValue() == null) { - currentCellData.setStringValue(currentCellValue); - } else { - currentCellData.setStringValue(currentCellData.getStringValue() + currentCellValue); - } - break; - case BOOLEAN: - if (currentCellData.getBooleanValue() == null) { - currentCellData.setBooleanValue(BooleanUtils.valueOf(currentCellValue)); - } - break; - case NUMBER: - case EMPTY: - currentCellData.setType(CellDataTypeEnum.NUMBER); - if (currentCellData.getNumberValue() == null) { - currentCellData.setNumberValue(new BigDecimal(currentCellValue)); - } - break; - default: - throw new IllegalStateException("Cannot set values now"); - } + dataStringBuilder.append(ch, start, length); } @Override From 173ffaab8ee40b04465052d4063c7777267c7eb5 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Wed, 25 Sep 2019 00:15:57 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=87=8D=E5=A4=A7bug=20?= =?UTF-8?q?=E5=9C=A807=E7=89=88=E8=AF=BB=E5=8F=96=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E7=9A=84=E6=97=B6=E5=80=99=20=E5=B0=8F=E6=A6=82=E7=8E=87?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E6=95=B0=E5=AD=97=E9=83=A8=E5=88=86=E4=B8=A2?= =?UTF-8?q?=E5=A4=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- quickstart.md | 2 ++ update.md | 3 +++ 2 files changed, 5 insertions(+) diff --git a/quickstart.md b/quickstart.md index 9ca53df..cc968c9 100644 --- a/quickstart.md +++ b/quickstart.md @@ -1,4 +1,6 @@ # easyexcel核心功能 +## 各位读取文件务必使用2.0.3起 +2.0.0-beta1到2.0.2有小概率会丢失数字。 ## 目录 ### 前言 #### 以下功能目前不支持 diff --git a/update.md b/update.md index 7c1cbb8..577c414 100644 --- a/update.md +++ b/update.md @@ -1,3 +1,6 @@ +# 2.0.3 +* 修复重大bug 在07版读取文件的时候 小概率导致数字部分丢失 + # 2.0.2 * 修复xls无法获取sheetList的bug [Issue #621](https://github.com/alibaba/easyexcel/issues/621) * 修复监听器转换异常会重复提示的bug