From d4bc63a5e2fbd2e00b7c3c746af03410055d5863 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Thu, 16 Sep 2021 21:44:45 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=9C=89=E4=BA=9Bxlsx?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=E5=A4=B1=E8=B4=A5=E7=9A=84bug=20[Issue=20#15?= =?UTF-8?q?95]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../handlers/AbstractCellValueTagHandler.java | 59 ------------------- .../CellInlineStringValueTagHandler.java | 13 ---- .../analysis/v07/handlers/CellTagHandler.java | 57 +++++++++++++++++- .../v07/handlers/CellValueTagHandler.java | 29 --------- .../alibaba/excel/enums/CellDataTypeEnum.java | 2 +- .../easyexcel/test/temp/Lock2Test.java | 18 +++--- update.md | 3 + 8 files changed, 69 insertions(+), 114 deletions(-) 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 From c9bc503bd1f57a288b27bebc242a0f6cbe4f4735 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Fri, 17 Sep 2021 10:05:27 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E6=9E=84=E5=BB=BA=E6=94=B9=E6=88=90coveral?= =?UTF-8?q?ls?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .coveralls.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .coveralls.yml diff --git a/.coveralls.yml b/.coveralls.yml new file mode 100644 index 00000000..7b214e4d --- /dev/null +++ b/.coveralls.yml @@ -0,0 +1,2 @@ +service_name: travis-pro +repo_token: hsLKjeN0L4gFu4fnVf6izluzIrmvQbhkw \ No newline at end of file From ff84b03d6dd8d374f322a6bd4a2490f602bff8fb Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Fri, 17 Sep 2021 10:36:32 +0800 Subject: [PATCH 3/8] Create ci.yml MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增ci --- .github/workflows/ci.yml | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000..efdd7452 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,22 @@ +# This workflow will build a Java project with Ant +# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-ant + +name: Java CI + +on: [push, pull_request] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Set up JDK 8 + uses: actions/setup-java@v2 + with: + java-version: '8' + distribution: 'adopt' + - name: CI + run: ./mvnw install -B -V -Dmaven.test.skip=true + - name: Java Doc + run: ./mvnw javadoc:javadoc From f4ef6e151b2834ee49b7071e5c25a406c2a2189f Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Fri, 17 Sep 2021 10:44:46 +0800 Subject: [PATCH 4/8] Update ci.yml ci --- .github/workflows/ci.yml | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index efdd7452..43bc6ff9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,5 +1,18 @@ -# This workflow will build a Java project with Ant -# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-ant +# +# Copyright 2009-2021 the original author or authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# name: Java CI @@ -16,6 +29,10 @@ jobs: with: java-version: '8' distribution: 'adopt' + - name: Cache Maven + uses: actions/cache@v2.1.6 + with: + path: ~/.m2 - name: CI run: ./mvnw install -B -V -Dmaven.test.skip=true - name: Java Doc From 1925bba2e4edb4e433a726455d3a0eefb88510c8 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Fri, 17 Sep 2021 10:45:45 +0800 Subject: [PATCH 5/8] Update ci.yml ci --- .github/workflows/ci.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 43bc6ff9..4c33ea7e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -30,9 +30,9 @@ jobs: java-version: '8' distribution: 'adopt' - name: Cache Maven - uses: actions/cache@v2.1.6 - with: - path: ~/.m2 + uses: actions/cache@v2.1.6 + with: + path: ~/.m2 - name: CI run: ./mvnw install -B -V -Dmaven.test.skip=true - name: Java Doc From cfaddb5c885fe257e8a70c28ebe2c6f5c683fed6 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Fri, 17 Sep 2021 10:50:18 +0800 Subject: [PATCH 6/8] Update ci.yml ci --- .github/workflows/ci.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4c33ea7e..986b8571 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,7 +33,9 @@ jobs: uses: actions/cache@v2.1.6 with: path: ~/.m2 - - name: CI + - name: Chmod + run: chmod +x mvnw + - name: Maven Build run: ./mvnw install -B -V -Dmaven.test.skip=true - name: Java Doc run: ./mvnw javadoc:javadoc From d99651a6050592699e4b1be6023522eafb086127 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Fri, 17 Sep 2021 10:56:20 +0800 Subject: [PATCH 7/8] Update ci.yml ci --- .github/workflows/ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 986b8571..c9da457d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,6 +33,8 @@ jobs: uses: actions/cache@v2.1.6 with: path: ~/.m2 + key: m2 + restore-keys: m2 - name: Chmod run: chmod +x mvnw - name: Maven Build From a131a4e4ec46cf69483c3bcd64e2709b5316eeb6 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Fri, 17 Sep 2021 11:11:34 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9java=20ci?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .coveralls.yml | 2 -- .travis.yml | 10 ---------- README.md | 2 +- 3 files changed, 1 insertion(+), 13 deletions(-) delete mode 100644 .coveralls.yml delete mode 100644 .travis.yml diff --git a/.coveralls.yml b/.coveralls.yml deleted file mode 100644 index 7b214e4d..00000000 --- a/.coveralls.yml +++ /dev/null @@ -1,2 +0,0 @@ -service_name: travis-pro -repo_token: hsLKjeN0L4gFu4fnVf6izluzIrmvQbhkw \ No newline at end of file diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 74750ec0..00000000 --- a/.travis.yml +++ /dev/null @@ -1,10 +0,0 @@ -language: java -jdk: openjdk8 -cache: - directories: - - $HOME/.m2 -before_install: - - chmod +x mvnw -install: - - ./mvnw install -B -V -Dmaven.test.skip=true - - ./mvnw javadoc:javadoc \ No newline at end of file diff --git a/README.md b/README.md index bc091e8c..e961d412 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ EasyExcel ====================== -[![Build Status](https://travis-ci.org/alibaba/easyexcel.svg?branch=master)](https://travis-ci.org/alibaba/easyexcel) +[![Build Status](https://github.com/alibaba/easyexcel/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/alibaba/easyexcel/actions/workflows/ci.yml?query=branch%3Amaster) [![Maven central](https://maven-badges.herokuapp.com/maven-central/com.alibaba/easyexcel/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.alibaba/easyexcel) [![License](http://img.shields.io/:license-apache-brightgreen.svg)](http://www.apache.org/licenses/LICENSE-2.0.html)