Browse Source

Merge branch 'master' into 2.0.1

# Conflicts:
#	src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java
#	src/main/java/com/alibaba/excel/converters/booleanconverter/BooleanNumberConverter.java
#	src/main/java/com/alibaba/excel/converters/string/StringNumberConverter.java
#	src/test/java/com/alibaba/easyexcel/test/temp/simple/HgTest.java
developing
Jiaju Zhuang 5 years ago
parent
commit
fd860956d9
  1. 6
      pom.xml
  2. 2
      quickstart.md
  3. 4
      src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java
  4. 12
      src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java
  5. 2
      src/main/java/com/alibaba/excel/converters/booleanconverter/BooleanNumberConverter.java
  6. 1
      src/main/java/com/alibaba/excel/converters/string/StringNumberConverter.java
  7. 9
      src/main/java/com/alibaba/excel/support/ExcelTypeEnum.java
  8. 6
      src/main/java/com/alibaba/excel/write/style/column/LongestMatchColumnWidthStyleStrategy.java
  9. 3
      src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java
  10. 2
      src/test/java/com/alibaba/easyexcel/test/temp/simple/HgTest.java
  11. 5
      update.md

6
pom.xml

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId> <artifactId>easyexcel</artifactId>
<version>2.0.0-beta6</version> <version>2.0.0</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>easyexcel</name> <name>easyexcel</name>
@ -59,12 +59,12 @@
<dependency> <dependency>
<groupId>org.apache.poi</groupId> <groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId> <artifactId>poi</artifactId>
<version>4.0.1</version> <version>3.17</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.poi</groupId> <groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId> <artifactId>poi-ooxml</artifactId>
<version>4.0.1</version> <version>3.17</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>cglib</groupId> <groupId>cglib</groupId>

2
quickstart.md

@ -8,7 +8,7 @@
* 读取图片 * 读取图片
* 宏 * 宏
#### 关于版本兼容 #### 关于版本兼容
目前poi用的 4.0.1 建议检查是否该版本。如果出现找不到class之类的,八成就是这个原因 目前poi用的 4.0.1 建议检查是否该版本。如果看到`NoClassDefFoundError`或者`ClassNotFoundException`,请查看poi相关版本是否都为4.0.1
#### 详细参数介绍 #### 详细参数介绍
有些参数不知道怎么用,或者有些功能不知道用什么参数,参照:[详细参数介绍](/docs/API.md) 有些参数不知道怎么用,或者有些功能不知道用什么参数,参照:[详细参数介绍](/docs/API.md)
#### 开源项目不容易,如果觉得本项目对您的工作还是有帮助的话,请在右上角帮忙点个★Star。 #### 开源项目不容易,如果觉得本项目对您的工作还是有帮助的话,请在右上角帮忙点个★Star。

4
src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java

@ -15,7 +15,6 @@ import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.context.AnalysisContextImpl; import com.alibaba.excel.context.AnalysisContextImpl;
import com.alibaba.excel.exception.ExcelAnalysisException; import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.exception.ExcelAnalysisStopException; import com.alibaba.excel.exception.ExcelAnalysisStopException;
import com.alibaba.excel.exception.ExcelGenerateException;
import com.alibaba.excel.read.metadata.ReadSheet; import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.read.metadata.ReadWorkbook; import com.alibaba.excel.read.metadata.ReadWorkbook;
import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder; import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder;
@ -64,7 +63,8 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
if (poifsFileSystem.getRoot().hasEntry(Decryptor.DEFAULT_POIFS_ENTRY)) { if (poifsFileSystem.getRoot().hasEntry(Decryptor.DEFAULT_POIFS_ENTRY)) {
InputStream decryptedStream = null; InputStream decryptedStream = null;
try { try {
decryptedStream = DocumentFactoryHelper.getDecryptedStream(poifsFileSystem.getRoot(), null); decryptedStream =
DocumentFactoryHelper.getDecryptedStream(poifsFileSystem.getRoot().getFileSystem(), null);
excelExecutor = new XlsxSaxAnalyser(analysisContext, decryptedStream); excelExecutor = new XlsxSaxAnalyser(analysisContext, decryptedStream);
return; return;
} finally { } finally {

12
src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java

@ -8,6 +8,8 @@ import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_VALUE_TAG;
import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_VALUE_TYPE_TAG; import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_VALUE_TYPE_TAG;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Map; import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
@ -34,8 +36,7 @@ import com.alibaba.excel.util.StringUtils;
*/ */
public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder { public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder {
private final AnalysisContext analysisContext; private final AnalysisContext analysisContext;
private String currentTag; private Deque<String> currentTagDeque = new LinkedList<String>();
private String currentCellIndex;
private int curCol; private int curCol;
private Map<Integer, CellData> curRowContent = new TreeMap<Integer, CellData>(); private Map<Integer, CellData> curRowContent = new TreeMap<Integer, CellData>();
private CellData currentCellData; private CellData currentCellData;
@ -62,11 +63,10 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
@Override @Override
public void startHandle(String name, Attributes attributes) { public void startHandle(String name, Attributes attributes) {
currentTag = name; currentTagDeque.push(name);
// start a cell // start a cell
if (CELL_TAG.equals(name)) { if (CELL_TAG.equals(name)) {
currentCellIndex = attributes.getValue(ExcelXmlConstants.POSITION); curCol = PositionUtils.getCol(attributes.getValue(ExcelXmlConstants.POSITION));
curCol = PositionUtils.getCol(currentCellIndex);
// t="s" ,it's means String // t="s" ,it's means String
// t="str" ,it's means String,but does not need to be read in the 'sharedStrings.xml' // t="str" ,it's means String,but does not need to be read in the 'sharedStrings.xml'
@ -101,6 +101,7 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
@Override @Override
public void endHandle(String name) { public void endHandle(String name) {
currentTagDeque.pop();
if (CELL_VALUE_TAG.equals(name)) { if (CELL_VALUE_TAG.equals(name)) {
// Have to go "sharedStrings.xml" and get it // Have to go "sharedStrings.xml" and get it
if (currentCellData.getType() == CellDataTypeEnum.STRING) { if (currentCellData.getType() == CellDataTypeEnum.STRING) {
@ -134,6 +135,7 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
if (StringUtils.isEmpty(currentCellValue)) { if (StringUtils.isEmpty(currentCellValue)) {
return; return;
} }
String currentTag = currentTagDeque.peek();
if (currentTag == null) { if (currentTag == null) {
return; return;
} }

2
src/main/java/com/alibaba/excel/converters/booleanconverter/BooleanNumberConverter.java

@ -27,7 +27,7 @@ public class BooleanNumberConverter implements Converter<Boolean> {
@Override @Override
public Boolean convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, public Boolean convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) { GlobalConfiguration globalConfiguration) {
if (BigDecimal.ONE.equals(cellData.getNumberValue())) { if (BigDecimal.ONE.compareTo(cellData.getNumberValue()) == 0) {
return Boolean.TRUE; return Boolean.TRUE;
} }
return Boolean.FALSE; return Boolean.FALSE;

1
src/main/java/com/alibaba/excel/converters/string/StringNumberConverter.java

@ -2,7 +2,6 @@ package com.alibaba.excel.converters.string;
import java.math.BigDecimal; import java.math.BigDecimal;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.DateUtil;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;

9
src/main/java/com/alibaba/excel/support/ExcelTypeEnum.java

@ -1,6 +1,8 @@
package com.alibaba.excel.support; package com.alibaba.excel.support;
import java.io.BufferedInputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -31,7 +33,12 @@ public enum ExcelTypeEnum {
try { try {
FileMagic fileMagic; FileMagic fileMagic;
if (file != null) { if (file != null) {
fileMagic = FileMagic.valueOf(file); BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
try {
fileMagic = FileMagic.valueOf(bufferedInputStream);
} finally {
bufferedInputStream.close();
}
if (!FileMagic.OLE2.equals(fileMagic) && !FileMagic.OOXML.equals(fileMagic)) { if (!FileMagic.OLE2.equals(fileMagic) && !FileMagic.OOXML.equals(fileMagic)) {
String fileName = file.getName(); String fileName = file.getName();
if (fileName.endsWith(XLSX.getValue())) { if (fileName.endsWith(XLSX.getValue())) {

6
src/main/java/com/alibaba/excel/write/style/column/LongestMatchColumnWidthStyleStrategy.java

@ -21,7 +21,7 @@ public class LongestMatchColumnWidthStyleStrategy extends AbstractColumnWidthSty
private static final int MAX_COLUMN_WIDTH = 256; private static final int MAX_COLUMN_WIDTH = 256;
Map<Integer, Map<Integer, Integer>> cache = new HashMap<Integer, Map<Integer, Integer>>(8); private static final Map<Integer, Map<Integer, Integer>> CACHE = new HashMap<Integer, Map<Integer, Integer>>(8);
@Override @Override
protected void setColumnWidth(WriteSheetHolder writeSheetHolder, CellData cellData, Cell cell, Head head, protected void setColumnWidth(WriteSheetHolder writeSheetHolder, CellData cellData, Cell cell, Head head,
@ -29,10 +29,10 @@ public class LongestMatchColumnWidthStyleStrategy extends AbstractColumnWidthSty
if (!isHead && cellData == null) { if (!isHead && cellData == null) {
return; return;
} }
Map<Integer, Integer> maxColumnWidthMap = cache.get(writeSheetHolder.getSheetNo()); Map<Integer, Integer> maxColumnWidthMap = CACHE.get(writeSheetHolder.getSheetNo());
if (maxColumnWidthMap == null) { if (maxColumnWidthMap == null) {
maxColumnWidthMap = new HashMap<Integer, Integer>(16); maxColumnWidthMap = new HashMap<Integer, Integer>(16);
cache.put(writeSheetHolder.getSheetNo(), maxColumnWidthMap); CACHE.put(writeSheetHolder.getSheetNo(), maxColumnWidthMap);
} }
Integer columnWidth = dataLength(cellData, cell, isHead); Integer columnWidth = dataLength(cellData, cell, isHead);
if (columnWidth < 0) { if (columnWidth < 0) {

3
src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java

@ -65,7 +65,8 @@ public class ReadAllConverterDataListener extends AnalysisEventListener<ReadAllC
Assert.assertEquals(data.getStringString(), "测试"); Assert.assertEquals(data.getStringString(), "测试");
Assert.assertEquals(data.getStringError(), "#VALUE!"); Assert.assertEquals(data.getStringError(), "#VALUE!");
Assert.assertEquals(data.getStringNumberDate(), "2020-01-01 01:01:01"); Assert.assertEquals(data.getStringNumberDate(), "2020-01-01 01:01:01");
Assert.assertEquals(data.getStringFormulaNumber(), "2.0"); double doubleStringFormulaNumber = new BigDecimal(data.getStringFormulaNumber()).doubleValue();
Assert.assertEquals(doubleStringFormulaNumber, 2.0, 0.0);
Assert.assertEquals(data.getStringFormulaString(), "1测试"); Assert.assertEquals(data.getStringFormulaString(), "1测试");
LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0)));
} }

2
src/test/java/com/alibaba/easyexcel/test/temp/simple/HgTest.java

@ -24,7 +24,7 @@ public class HgTest {
@Test @Test
public void hh() throws IOException { public void hh() throws IOException {
List<Object> list = List<Object> list =
EasyExcel.read(new FileInputStream("D:\\test\\test.xlsx")).headRowNumber(0).sheet().doReadSync(); EasyExcel.read(new FileInputStream("D:\\test\\折扣2007.xls")).headRowNumber(0).sheet().doReadSync();
for (Object data : list) { for (Object data : list) {
LOGGER.info("返回数据:{}", JSON.toJSONString(data)); LOGGER.info("返回数据:{}", JSON.toJSONString(data));
} }

5
update.md

@ -1,3 +1,8 @@
# 2.0.0
* 修复当cell为空可能会抛出空指针的bug
* 修复电话等长数字可能出现科学计数法的问题 [Issue #583](https://github.com/alibaba/easyexcel/issues/583)
* 升级为正式版
# 2.0.0-beta6 # 2.0.0-beta6
* 修复空行读取空指针异常 * 修复空行读取空指针异常
* 修复写入指定头为List<List<String>>,但是数据用List<Class>导致的空指针 * 修复写入指定头为List<List<String>>,但是数据用List<Class>导致的空指针

Loading…
Cancel
Save