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 ac2b8457..e37ea6bc 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 @@ -4,7 +4,7 @@ import java.math.BigDecimal; import com.alibaba.excel.context.xlsx.XlsxReadContext; import com.alibaba.excel.enums.CellDataTypeEnum; -import com.alibaba.excel.metadata.data.CellData; +import com.alibaba.excel.metadata.data.ReadCellData; import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder; import com.alibaba.excel.util.BooleanUtils; import com.alibaba.excel.util.StringUtils; @@ -19,7 +19,7 @@ public abstract class AbstractCellValueTagHandler extends AbstractXlsxTagHandler @Override public void endElement(XlsxReadContext xlsxReadContext, String name) { XlsxReadSheetHolder xlsxReadSheetHolder = xlsxReadContext.xlsxReadSheetHolder(); - CellData tempCellData = xlsxReadSheetHolder.getTempCellData(); + ReadCellData tempCellData = xlsxReadSheetHolder.getTempCellData(); StringBuilder tempData = xlsxReadSheetHolder.getTempData(); String tempDataString = tempData.toString(); CellDataTypeEnum oldType = tempCellData.getType(); diff --git a/src/main/java/com/alibaba/excel/metadata/data/DataFormatData.java b/src/main/java/com/alibaba/excel/metadata/data/DataFormatData.java index 3ac70781..7f1a14c8 100644 --- a/src/main/java/com/alibaba/excel/metadata/data/DataFormatData.java +++ b/src/main/java/com/alibaba/excel/metadata/data/DataFormatData.java @@ -18,4 +18,12 @@ public class DataFormatData { * format */ private String format; + + @Override + public DataFormatData clone() { + DataFormatData dataFormatData = new DataFormatData(); + dataFormatData.setIndex(getIndex()); + dataFormatData.setFormat(getFormat()); + return dataFormatData; + } } diff --git a/src/main/java/com/alibaba/excel/metadata/data/FormulaData.java b/src/main/java/com/alibaba/excel/metadata/data/FormulaData.java index cdf91055..36d0206b 100644 --- a/src/main/java/com/alibaba/excel/metadata/data/FormulaData.java +++ b/src/main/java/com/alibaba/excel/metadata/data/FormulaData.java @@ -13,4 +13,11 @@ public class FormulaData { * formula */ private String formulaValue; + + @Override + public FormulaData clone() { + FormulaData formulaData = new FormulaData(); + formulaData.setFormulaValue(getFormulaValue()); + return formulaData; + } } diff --git a/src/main/java/com/alibaba/excel/metadata/data/ReadCellData.java b/src/main/java/com/alibaba/excel/metadata/data/ReadCellData.java index 90cf9a5a..68231a19 100644 --- a/src/main/java/com/alibaba/excel/metadata/data/ReadCellData.java +++ b/src/main/java/com/alibaba/excel/metadata/data/ReadCellData.java @@ -20,7 +20,6 @@ public class ReadCellData extends CellData { */ private DataFormatData dataFormatData; - public ReadCellData(CellDataTypeEnum type) { super(); if (type == null) { @@ -104,10 +103,21 @@ public class ReadCellData extends CellData { return cellData; } - @Override - public ReadCellData clone(){ - return new ReadCellData<>(""); + public ReadCellData clone() { + ReadCellData readCellData = new ReadCellData<>(); + readCellData.setType(getType()); + readCellData.setNumberValue(getNumberValue()); + readCellData.setStringValue(getStringValue()); + readCellData.setBooleanValue(getBooleanValue()); + readCellData.setData(getData()); + if (getDataFormatData() != null) { + readCellData.setDataFormatData(getDataFormatData().clone()); + } + if (getFormulaData() != null) { + readCellData.setFormulaData(getFormulaData().clone()); + } + return readCellData; } } diff --git a/src/main/java/com/alibaba/excel/support/ExcelTypeEnum.java b/src/main/java/com/alibaba/excel/support/ExcelTypeEnum.java index cdca07fd..fd90e485 100644 --- a/src/main/java/com/alibaba/excel/support/ExcelTypeEnum.java +++ b/src/main/java/com/alibaba/excel/support/ExcelTypeEnum.java @@ -5,13 +5,13 @@ import java.io.File; import java.io.FileInputStream; import java.io.InputStream; -import org.apache.poi.poifs.filesystem.FileMagic; - import com.alibaba.excel.exception.ExcelAnalysisException; import com.alibaba.excel.exception.ExcelCommonException; import com.alibaba.excel.read.metadata.ReadWorkbook; import com.alibaba.excel.util.StringUtils; +import org.apache.poi.poifs.filesystem.FileMagic; + /** * @author jipengfei */ @@ -78,8 +78,6 @@ public enum ExcelTypeEnum { return recognitionExcelType(inputStream); } catch (ExcelCommonException e) { throw e; - } catch (ExcelAnalysisException e) { - throw e; } catch (Exception e) { throw new ExcelCommonException( "Convert excel format exception.You can try specifying the 'excelType' yourself", e); diff --git a/src/main/java/com/alibaba/excel/util/ConverterUtils.java b/src/main/java/com/alibaba/excel/util/ConverterUtils.java index 029c8196..e0e139f7 100644 --- a/src/main/java/com/alibaba/excel/util/ConverterUtils.java +++ b/src/main/java/com/alibaba/excel/util/ConverterUtils.java @@ -11,7 +11,6 @@ import com.alibaba.excel.converters.ConverterKeyBuild; import com.alibaba.excel.converters.ReadConverterContext; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.exception.ExcelDataConvertException; -import com.alibaba.excel.metadata.data.CellData; import com.alibaba.excel.metadata.data.ReadCellData; import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.read.metadata.holder.ReadSheetHolder; @@ -22,6 +21,7 @@ import com.alibaba.excel.read.metadata.holder.ReadSheetHolder; * @author Jiaju Zhuang **/ public class ConverterUtils { + public static Class defaultClassGeneric = String.class; private ConverterUtils() {} @@ -87,16 +87,8 @@ public class ConverterUtils { } else { clazz = field.getType(); } - if (clazz == CellData.class) { - Type type = field.getGenericType(); - Class classGeneric; - if (type instanceof ParameterizedType) { - ParameterizedType parameterizedType = (ParameterizedType)type; - classGeneric = (Class)parameterizedType.getActualTypeArguments()[0]; - } else { - classGeneric = String.class; - } - + if (clazz == ReadCellData.class) { + Class classGeneric = getClassGeneric(field.getGenericType()); ReadCellData cellDataReturn = cellData.clone(); cellDataReturn.setData(doConvertToJavaObject(cellData, classGeneric, contentProperty, converterMap, context, rowIndex, columnIndex)); @@ -106,6 +98,22 @@ public class ConverterUtils { columnIndex); } + private static Class getClassGeneric(Type type) { + if (!(type instanceof ParameterizedType)) { + return defaultClassGeneric; + } + ParameterizedType parameterizedType = (ParameterizedType)type; + Type[] types = parameterizedType.getActualTypeArguments(); + if (types == null || types.length == 0) { + return defaultClassGeneric; + } + Type actualType = types[0]; + if (!(actualType instanceof Class)) { + return defaultClassGeneric; + } + return (Class)actualType; + } + /** * @param cellData * @param clazz diff --git a/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataListener.java b/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataListener.java index 6971c076..cb6a1105 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataListener.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataListener.java @@ -14,21 +14,21 @@ import org.slf4j.LoggerFactory; /** * @author Jiaju Zhuang */ -public class CellDataDataListener extends AnalysisEventListener { +public class CellDataDataListener extends AnalysisEventListener { private static final Logger LOGGER = LoggerFactory.getLogger(CellDataDataListener.class); - List list = new ArrayList(); + List list = new ArrayList<>(); @Override - public void invoke(CellDataData data, AnalysisContext context) { + public void invoke(CellDataReadData data, AnalysisContext context) { list.add(data); } @Override public void doAfterAllAnalysed(AnalysisContext context) { Assert.assertEquals(list.size(), 1); - CellDataData cellDataData = list.get(0); + CellDataReadData cellDataData = list.get(0); - Assert.assertEquals(cellDataData.getDate().getStringValue(), "2020年01月01日"); + Assert.assertEquals(cellDataData.getDate().getData(), "2020年01月01日"); Assert.assertEquals((long)cellDataData.getInteger1().getData(), 2L); Assert.assertEquals((long)cellDataData.getInteger2(), 2L); Assert.assertEquals(cellDataData.getFormulaValue().getFormulaData().getFormulaValue(), "B2+C2"); diff --git a/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataTest.java index 61d2809c..db4bf80b 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataTest.java @@ -8,7 +8,6 @@ import java.util.List; import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.enums.CellDataTypeEnum; -import com.alibaba.excel.metadata.data.CellData; import com.alibaba.excel.metadata.data.FormulaData; import com.alibaba.excel.metadata.data.WriteCellData; import com.alibaba.excel.util.DateUtils; @@ -44,15 +43,15 @@ public class CellDataDataTest { } private void readAndWrite(File file) throws Exception { - EasyExcel.write(file, CellDataData.class).sheet().doWrite(data()); - EasyExcel.read(file, CellDataData.class, new CellDataDataListener()).sheet().doRead(); + EasyExcel.write(file, CellDataWriteData.class).sheet().doWrite(data()); + EasyExcel.read(file, CellDataReadData.class, new CellDataDataListener()).sheet().doRead(); } - private List data() throws Exception { - List list = new ArrayList(); - CellDataData cellDataData = new CellDataData(); + private List data() throws Exception { + List list = new ArrayList<>(); + CellDataWriteData cellDataData = new CellDataWriteData(); cellDataData.setDate(new WriteCellData<>(DateUtils.parseDate("2020-01-01 01:01:01"))); - CellData integer1 = new CellData(); + WriteCellData integer1 = new WriteCellData<>(); integer1.setType(CellDataTypeEnum.NUMBER); integer1.setNumberValue(BigDecimal.valueOf(2L)); cellDataData.setInteger1(integer1); diff --git a/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataReadData.java b/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataReadData.java new file mode 100644 index 00000000..b8322073 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataReadData.java @@ -0,0 +1,18 @@ +package com.alibaba.easyexcel.test.core.celldata; + +import com.alibaba.excel.annotation.format.DateTimeFormat; +import com.alibaba.excel.metadata.data.ReadCellData; + +import lombok.Data; + +/** + * @author Jiaju Zhuang + */ +@Data +public class CellDataReadData { + @DateTimeFormat("yyyy年MM月dd日") + private ReadCellData date; + private ReadCellData integer1; + private Integer integer2; + private ReadCellData formulaValue; +} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataData.java b/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataWriteData.java similarity index 55% rename from src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataData.java rename to src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataWriteData.java index bed0685d..69ed7bbf 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataWriteData.java @@ -3,7 +3,7 @@ package com.alibaba.easyexcel.test.core.celldata; import java.util.Date; import com.alibaba.excel.annotation.format.DateTimeFormat; -import com.alibaba.excel.metadata.data.CellData; +import com.alibaba.excel.metadata.data.WriteCellData; import lombok.Data; @@ -11,10 +11,10 @@ import lombok.Data; * @author Jiaju Zhuang */ @Data -public class CellDataData { +public class CellDataWriteData { @DateTimeFormat("yyyy年MM月dd日") - private CellData date; - private CellData integer1; + private WriteCellData date; + private WriteCellData integer1; private Integer integer2; - private CellData formulaValue; + private WriteCellData formulaValue; } diff --git a/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java b/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java index 210ca142..411700f0 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java @@ -5,32 +5,32 @@ import java.text.ParseException; import java.util.ArrayList; import java.util.List; -import org.junit.Assert; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.exception.ExcelCommonException; import com.alibaba.excel.util.DateUtils; import com.alibaba.fastjson.JSON; +import org.junit.Assert; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * @author Jiaju Zhuang */ -public class ConverterDataListener extends AnalysisEventListener { +public class ConverterDataListener extends AnalysisEventListener { private static final Logger LOGGER = LoggerFactory.getLogger(ConverterDataListener.class); - List list = new ArrayList(); + private List list = new ArrayList<>(); @Override - public void invoke(ConverterData data, AnalysisContext context) { + public void invoke(ConverterReadData data, AnalysisContext context) { list.add(data); } @Override public void doAfterAllAnalysed(AnalysisContext context) { Assert.assertEquals(list.size(), 1); - ConverterData data = list.get(0); + ConverterReadData data = list.get(0); try { Assert.assertEquals(DateUtils.parseDate("2020-01-01 01:01:01"), data.getDate()); } catch (ParseException e) { diff --git a/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataTest.java index 4725595c..45f09be1 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataTest.java @@ -48,8 +48,8 @@ public class ConverterDataTest { } private void readAndWrite(File file) throws Exception { - EasyExcel.write(file, ConverterData.class).sheet().doWrite(data()); - EasyExcel.read(file, ConverterData.class, new ConverterDataListener()).sheet().doRead(); + EasyExcel.write(file, ConverterWriteData.class).sheet().doWrite(data()); + EasyExcel.read(file, ConverterReadData.class, new ConverterDataListener()).sheet().doRead(); } @Test @@ -97,21 +97,21 @@ public class ConverterDataTest { .sheet().doRead(); } - private List data() throws Exception { - List list = new ArrayList(); - ConverterData converterData = new ConverterData(); - converterData.setDate(DateUtils.parseDate("2020-01-01 01:01:01")); - converterData.setBooleanData(Boolean.TRUE); - converterData.setBigDecimal(BigDecimal.ONE); - converterData.setLongData(1L); - converterData.setIntegerData(1); - converterData.setShortData((short)1); - converterData.setByteData((byte)1); - converterData.setDoubleData(1.0); - converterData.setFloatData((float)1.0); - converterData.setString("测试"); - converterData.setCellData(new WriteCellData<>("自定义")); - list.add(converterData); + private List data() throws Exception { + List list = new ArrayList(); + ConverterWriteData converterWriteData = new ConverterWriteData(); + converterWriteData.setDate(DateUtils.parseDate("2020-01-01 01:01:01")); + converterWriteData.setBooleanData(Boolean.TRUE); + converterWriteData.setBigDecimal(BigDecimal.ONE); + converterWriteData.setLongData(1L); + converterWriteData.setIntegerData(1); + converterWriteData.setShortData((short)1); + converterWriteData.setByteData((byte)1); + converterWriteData.setDoubleData(1.0); + converterWriteData.setFloatData((float)1.0); + converterWriteData.setString("测试"); + converterWriteData.setCellData(new WriteCellData<>("自定义")); + list.add(converterWriteData); return list; } } diff --git a/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterReadData.java b/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterReadData.java new file mode 100644 index 00000000..bf7af176 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterReadData.java @@ -0,0 +1,38 @@ +package com.alibaba.easyexcel.test.core.converter; + +import java.math.BigDecimal; +import java.util.Date; + +import com.alibaba.excel.annotation.ExcelProperty; +import com.alibaba.excel.metadata.data.ReadCellData; + +import lombok.Data; + +/** + * @author Jiaju Zhuang + */ +@Data +public class ConverterReadData { + @ExcelProperty("日期") + private Date date; + @ExcelProperty("布尔") + private Boolean booleanData; + @ExcelProperty("大数") + private BigDecimal bigDecimal; + @ExcelProperty("长整型") + private long longData; + @ExcelProperty("整型") + private Integer integerData; + @ExcelProperty("短整型") + private Short shortData; + @ExcelProperty("字节型") + private Byte byteData; + @ExcelProperty("双精度浮点型") + private double doubleData; + @ExcelProperty("浮点型") + private Float floatData; + @ExcelProperty("字符串") + private String string; + @ExcelProperty("自定义") + private ReadCellData cellData; +} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterData.java b/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterWriteData.java similarity index 96% rename from src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterData.java rename to src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterWriteData.java index 68daf41e..009fae2e 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterWriteData.java @@ -12,7 +12,7 @@ import lombok.Data; * @author Jiaju Zhuang */ @Data -public class ConverterData { +public class ConverterWriteData { @ExcelProperty("日期") private Date date; @ExcelProperty("布尔") diff --git a/src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptDataTest.java index 375f9bea..5569492e 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptDataTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptDataTest.java @@ -6,16 +6,16 @@ import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; -import org.junit.BeforeClass; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runners.MethodSorters; - import com.alibaba.easyexcel.test.core.simple.SimpleData; import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.support.ExcelTypeEnum; +import org.junit.BeforeClass; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; + /** * * @author Jiaju Zhuang @@ -30,7 +30,7 @@ public class EncryptDataTest { @BeforeClass public static void init() { - file07 = TestFileUtil.createNewFile("encrypt07.xlsx"); + file07 = TestFileUtil.createNewFile("encrypt07111.xlsx"); file03 = TestFileUtil.createNewFile("encrypt03.xls"); file07OutputStream = TestFileUtil.createNewFile("encryptOutputStream07.xlsx"); file03OutputStream = TestFileUtil.createNewFile("encryptOutputStream03.xls");