Browse Source

修复多个头的情况下会读取数据为空

2.1.x
Jiaju Zhuang 5 years ago
parent
commit
5b6957952b
  1. 9
      pom.xml
  2. 15
      src/main/java/com/alibaba/excel/context/WriteContextImpl.java
  3. 6
      src/main/java/com/alibaba/excel/metadata/CellData.java
  4. 5
      src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java
  5. 7
      src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java
  6. 6
      src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java
  7. 7
      src/main/java/com/alibaba/excel/write/style/column/LongestMatchColumnWidthStyleStrategy.java
  8. 12
      src/test/java/com/alibaba/easyexcel/test/temp/simple/Wirte.java
  9. 5
      update.md

9
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.1.0-beta3</version> <version>2.1.0-beta4</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>easyexcel</name> <name>easyexcel</name>
@ -132,11 +132,11 @@
<build> <build>
<plugins> <plugins>
<!-- 用pmd校验阿里的p3c规范 --> <!-- code style -->
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId> <artifactId>maven-pmd-plugin</artifactId>
<version>3.12.0</version> <version>3.8</version>
<configuration> <configuration>
<printFailingErrors>true</printFailingErrors> <printFailingErrors>true</printFailingErrors>
<verbose>true</verbose> <verbose>true</verbose>
@ -157,7 +157,6 @@
</excludes> </excludes>
</configuration> </configuration>
<executions> <executions>
<!-- 绑定pmd:check到verify生命周期 -->
<execution> <execution>
<id>pmd-check-verify</id> <id>pmd-check-verify</id>
<phase>validate</phase> <phase>validate</phase>
@ -170,7 +169,7 @@
<dependency> <dependency>
<groupId>com.alibaba.p3c</groupId> <groupId>com.alibaba.p3c</groupId>
<artifactId>p3c-pmd</artifactId> <artifactId>p3c-pmd</artifactId>
<version>2.0.0</version> <version>1.3.6</version>
</dependency> </dependency>
</dependencies> </dependencies>
</plugin> </plugin>

15
src/main/java/com/alibaba/excel/context/WriteContextImpl.java

@ -1,27 +1,17 @@
package com.alibaba.excel.context; package com.alibaba.excel.context;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.util.FileUtils;
import com.alibaba.excel.util.StringUtils;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.security.GeneralSecurityException;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey; import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess; import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.poifs.crypt.Decryptor;
import org.apache.poi.poifs.crypt.EncryptionInfo; import org.apache.poi.poifs.crypt.EncryptionInfo;
import org.apache.poi.poifs.crypt.EncryptionMode; import org.apache.poi.poifs.crypt.EncryptionMode;
import org.apache.poi.poifs.crypt.Encryptor; import org.apache.poi.poifs.crypt.Encryptor;
import org.apache.poi.poifs.filesystem.DocumentOutputStream;
import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
@ -36,6 +26,9 @@ import com.alibaba.excel.enums.WriteTypeEnum;
import com.alibaba.excel.exception.ExcelGenerateException; import com.alibaba.excel.exception.ExcelGenerateException;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.util.FileUtils;
import com.alibaba.excel.util.StringUtils;
import com.alibaba.excel.util.WorkBookUtil; import com.alibaba.excel.util.WorkBookUtil;
import com.alibaba.excel.util.WriteHandlerUtils; import com.alibaba.excel.util.WriteHandlerUtils;
import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.excel.write.metadata.WriteSheet;
@ -179,7 +172,7 @@ public class WriteContextImpl implements WriteContext {
Row row = WorkBookUtil.createRow(writeSheetHolder.getSheet(), i); Row row = WorkBookUtil.createRow(writeSheetHolder.getSheet(), i);
WriteHandlerUtils.afterRowCreate(this, row, relativeRowIndex, Boolean.TRUE); WriteHandlerUtils.afterRowCreate(this, row, relativeRowIndex, Boolean.TRUE);
addOneRowOfHeadDataToExcel(row, excelWriteHeadProperty.getHeadMap(), relativeRowIndex); addOneRowOfHeadDataToExcel(row, excelWriteHeadProperty.getHeadMap(), relativeRowIndex);
WriteHandlerUtils.afterRowDispose(this, row, relativeRowIndex, Boolean.FALSE); WriteHandlerUtils.afterRowDispose(this, row, relativeRowIndex, Boolean.TRUE);
} }
} }

6
src/main/java/com/alibaba/excel/metadata/CellData.java

@ -202,6 +202,9 @@ public class CellData<T> {
* Ensure that the object does not appear null * Ensure that the object does not appear null
*/ */
public void checkEmpty() { public void checkEmpty() {
if (type == null) {
type = CellDataTypeEnum.EMPTY;
}
switch (type) { switch (type) {
case STRING: case STRING:
case ERROR: case ERROR:
@ -225,6 +228,9 @@ public class CellData<T> {
@Override @Override
public String toString() { public String toString() {
if (type == null) {
return "empty";
}
switch (type) { switch (type) {
case NUMBER: case NUMBER:
return numberValue.toString(); return numberValue.toString();

5
src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java

@ -158,7 +158,6 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH
if (currentheadRowNumber == rowIndex + 1) { if (currentheadRowNumber == rowIndex + 1) {
buildHead(analysisContext, cellDataMap); buildHead(analysisContext, cellDataMap);
} }
// Now is header // Now is header
for (ReadListener readListener : analysisContext.currentReadHolder().readListenerList()) { for (ReadListener readListener : analysisContext.currentReadHolder().readListenerList()) {
try { try {
@ -206,8 +205,8 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH
tmpContentPropertyMap.put(entry.getKey(), contentPropertyMapData.get(entry.getKey())); tmpContentPropertyMap.put(entry.getKey(), contentPropertyMapData.get(entry.getKey()));
continue; continue;
} }
String headName = headData.getHeadNameList().get(0); List<String> headNameList = headData.getHeadNameList();
String headName = headNameList.get(headNameList.size() - 1);
for (Map.Entry<Integer, String> stringEntry : dataMap.entrySet()) { for (Map.Entry<Integer, String> stringEntry : dataMap.entrySet()) {
String headString = stringEntry.getValue(); String headString = stringEntry.getValue();
Integer stringKey = stringEntry.getKey(); Integer stringKey = stringEntry.getKey();

7
src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java

@ -31,7 +31,7 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
protected CellData converterAndSet(WriteHolder currentWriteHolder, Class clazz, Cell cell, Object value, protected CellData converterAndSet(WriteHolder currentWriteHolder, Class clazz, Cell cell, Object value,
ExcelContentProperty excelContentProperty) { ExcelContentProperty excelContentProperty) {
if (value == null) { if (value == null) {
return new CellData(); return new CellData(CellDataTypeEnum.EMPTY);
} }
if (value instanceof String && currentWriteHolder.globalConfiguration().getAutoTrim()) { if (value instanceof String && currentWriteHolder.globalConfiguration().getAutoTrim()) {
value = ((String)value).trim(); value = ((String)value).trim();
@ -40,6 +40,9 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
if (cellData.getFormula() != null && cellData.getFormula()) { if (cellData.getFormula() != null && cellData.getFormula()) {
cell.setCellFormula(cellData.getFormulaValue()); cell.setCellFormula(cellData.getFormulaValue());
} }
if (cellData.getType() == null) {
cellData.setType(CellDataTypeEnum.EMPTY);
}
switch (cellData.getType()) { switch (cellData.getType()) {
case STRING: case STRING:
cell.setCellValue(cellData.getStringValue()); cell.setCellValue(cellData.getStringValue());
@ -64,7 +67,7 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
protected CellData convert(WriteHolder currentWriteHolder, Class clazz, Cell cell, Object value, protected CellData convert(WriteHolder currentWriteHolder, Class clazz, Cell cell, Object value,
ExcelContentProperty excelContentProperty) { ExcelContentProperty excelContentProperty) {
if (value == null) { if (value == null) {
return new CellData(); return new CellData(CellDataTypeEnum.EMPTY);
} }
// This means that the user has defined the data. // This means that the user has defined the data.
if (value instanceof CellData) { if (value instanceof CellData) {

6
src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java

@ -14,6 +14,7 @@ import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Sheet;
import com.alibaba.excel.context.WriteContext; import com.alibaba.excel.context.WriteContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.enums.WriteDirectionEnum; import com.alibaba.excel.enums.WriteDirectionEnum;
import com.alibaba.excel.enums.WriteTemplateAnalysisCellTypeEnum; import com.alibaba.excel.enums.WriteTemplateAnalysisCellTypeEnum;
import com.alibaba.excel.exception.ExcelGenerateException; import com.alibaba.excel.exception.ExcelGenerateException;
@ -168,7 +169,9 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
CellData cellData = convert(writeSheetHolder, value == null ? null : value.getClass(), cell, value, CellData cellData = convert(writeSheetHolder, value == null ? null : value.getClass(), cell, value,
fieldNameContentPropertyMap.get(variable)); fieldNameContentPropertyMap.get(variable));
cellDataList.add(cellData); cellDataList.add(cellData);
switch (cellData.getType()) { CellDataTypeEnum type = cellData.getType();
if (type != null) {
switch (type) {
case STRING: case STRING:
cellValueBuild.append(cellData.getStringValue()); cellValueBuild.append(cellData.getStringValue());
break; break;
@ -182,6 +185,7 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
break; break;
} }
} }
}
cellValueBuild.append(analysisCell.getPrepareDataList().get(index)); cellValueBuild.append(analysisCell.getPrepareDataList().get(index));
cell.setCellValue(cellValueBuild.toString()); cell.setCellValue(cellValueBuild.toString());
WriteHandlerUtils.afterCellDispose(writeContext, cellDataList, cell, null, null, Boolean.FALSE); WriteHandlerUtils.afterCellDispose(writeContext, cellDataList, cell, null, null, Boolean.FALSE);

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

@ -6,6 +6,7 @@ import java.util.Map;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.util.CollectionUtils; import com.alibaba.excel.util.CollectionUtils;
@ -56,7 +57,11 @@ public class LongestMatchColumnWidthStyleStrategy extends AbstractColumnWidthSty
return cell.getStringCellValue().getBytes().length; return cell.getStringCellValue().getBytes().length;
} }
CellData cellData = cellDataList.get(0); CellData cellData = cellDataList.get(0);
switch (cellData.getType()) { CellDataTypeEnum type = cellData.getType();
if (type == null) {
return -1;
}
switch (type) {
case STRING: case STRING:
return cellData.getStringValue().getBytes().length; return cellData.getStringValue().getBytes().length;
case BOOLEAN: case BOOLEAN:

12
src/test/java/com/alibaba/easyexcel/test/temp/simple/Wirte.java

@ -1,7 +1,5 @@
package com.alibaba.easyexcel.test.temp.simple; package com.alibaba.easyexcel.test.temp.simple;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -13,14 +11,9 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.alibaba.easyexcel.test.core.large.LargeData; import com.alibaba.easyexcel.test.core.large.LargeData;
import com.alibaba.easyexcel.test.core.simple.SimpleData;
import com.alibaba.easyexcel.test.demo.write.DemoData; import com.alibaba.easyexcel.test.demo.write.DemoData;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.style.row.SimpleRowHeightStyleStrategy;
import com.alibaba.fastjson.JSON;
import net.sf.cglib.beans.BeanMap; import net.sf.cglib.beans.BeanMap;
@ -50,8 +43,7 @@ public class Wirte {
String fileName = TestFileUtil.getPath() + "t22" + System.currentTimeMillis() + ".xlsx"; String fileName = TestFileUtil.getPath() + "t22" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去读,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 // 这里 需要指定写用哪个class去读,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
// 如果这里想使用03 则 传入excelType参数即可 // 如果这里想使用03 则 传入excelType参数即可
EasyExcel.write(fileName, DemoData.class) EasyExcel.write(fileName, DemoData.class).sheet("模板").doWrite(data());
.registerWriteHandler(new SimpleRowHeightStyleStrategy((short)150, (short)120)).sheet("模板").doWrite(data());
} }
private List<List<String>> head() { private List<List<String>> head() {
@ -74,7 +66,7 @@ public class Wirte {
DemoData data = new DemoData(); DemoData data = new DemoData();
data.setString("字符串" + i); data.setString("字符串" + i);
data.setDate(new Date()); data.setDate(new Date());
data.setDoubleData(0.56); data.setDoubleData(null);
list.add(data); list.add(data);
} }
return list; return list;

5
update.md

@ -1,3 +1,8 @@
# 2.1.0-beta4
* 修改最长匹配策略会空指针的bug [Issue #747](https://github.com/alibaba/easyexcel/issues/747)
* 修改afterRowDispose错误 [Issue #751](https://github.com/alibaba/easyexcel/issues/751)
* 修复多个头的情况下会读取数据为空
# 2.1.0-beta3 # 2.1.0-beta3
* 支持强行指定在内存处理,以支持备注、RichTextString等的写入 * 支持强行指定在内存处理,以支持备注、RichTextString等的写入
* 修复关闭流失败,可能会不删除临时文件的问题 * 修复关闭流失败,可能会不删除临时文件的问题

Loading…
Cancel
Save