From 1026a18bdcd8cc9e9e42476dd9f26c668dbd6800 Mon Sep 17 00:00:00 2001 From: zhuangjiaju Date: Thu, 1 Aug 2019 18:59:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=AF=BB=E5=86=99=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/alibaba/excel/EasyExcelFactory.java | 78 ++++++----- .../excel/analysis/v07/XlsxSaxAnalyser.java | 46 ++++--- .../excel/context/AnalysisContextImpl.java | 2 +- .../bigdecimal/BigDecimalStringConverter.java | 5 +- .../byteconverter/ByteStringConverter.java | 6 +- .../DoubleStringConverter.java | 6 +- .../floatconverter/FloatStringConverter.java | 6 +- .../integer/IntegerStringConverter.java | 6 +- .../longconverter/LongStringConverter.java | 6 +- .../shortconverter/ShortStringConverter.java | 6 +- .../metadata/property/ExcelHeadProperty.java | 36 +++-- .../listener/ModelBuildEventListener.java | 17 ++- .../excel/read/metadata/ReadSheet.java | 7 +- .../metadata/holder/AbstractReadHolder.java | 31 +++-- .../read/metadata/holder/ReadSheetHolder.java | 1 + .../com/alibaba/excel/util/NumberUtils.java | 126 +++++++++++++++++- .../alibaba/excel/write/ExcelBuilderImpl.java | 9 +- .../excel/write/metadata/WriteSheet.java | 9 +- .../row/SimpleRowHeightStyleStrategy.java | 6 +- .../test/core/annotation/AnnotationData.java | 18 ++- .../annotation/AnnotationDataListener.java | 41 ++++++ .../core/annotation/AnnotationDataTest.java | 64 +++------ .../AnnotationIndexAndNameData.java | 20 +++ .../AnnotationIndexAndNameDataListener.java | 36 +++++ .../AnnotationIndexAndNameDataTest.java | 55 ++++++++ .../test/core/head/NoHeadData07Test.java | 16 --- .../easyexcel/test/core/order/OrderData.java | 29 ---- .../test/core/order/OrderData07Test.java | 34 ----- .../SimpleDataSheetNameListener.java} | 14 +- .../test/core/simple/SimpleDataTest.java | 6 + src/test/resources/simple/simple07.xlsx | Bin 0 -> 10048 bytes 31 files changed, 502 insertions(+), 240 deletions(-) create mode 100644 src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataListener.java create mode 100644 src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationIndexAndNameData.java create mode 100644 src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationIndexAndNameDataListener.java create mode 100644 src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationIndexAndNameDataTest.java delete mode 100644 src/test/java/com/alibaba/easyexcel/test/core/order/OrderData.java delete mode 100644 src/test/java/com/alibaba/easyexcel/test/core/order/OrderData07Test.java rename src/test/java/com/alibaba/easyexcel/test/core/{annotation/SimpleDataListener.java => simple/SimpleDataSheetNameListener.java} (55%) create mode 100644 src/test/resources/simple/simple07.xlsx diff --git a/src/main/java/com/alibaba/excel/EasyExcelFactory.java b/src/main/java/com/alibaba/excel/EasyExcelFactory.java index 16a3d3b8..4d732198 100644 --- a/src/main/java/com/alibaba/excel/EasyExcelFactory.java +++ b/src/main/java/com/alibaba/excel/EasyExcelFactory.java @@ -6,6 +6,8 @@ import java.io.OutputStream; import java.net.URI; import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.TreeMap; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; @@ -35,7 +37,7 @@ import com.alibaba.excel.write.builder.ExcelWriterTableBuilder; *

Sample2

* * - * + * * @author jipengfei */ public class EasyExcelFactory { @@ -62,7 +64,15 @@ public class EasyExcelFactory { @Override public void doAfterAllAnalysed(AnalysisContext context) {} }, false).read(sheet); - return rows; + if (rows.size() == 0 || !(rows.get(0) instanceof Map)) { + return rows; + } + List listRow = new ArrayList(); + for (Object obj : rows) { + Map oneRow = (Map)obj; + listRow.add(new ArrayList(new TreeMap(oneRow).values())); + } + return listRow; } /** @@ -172,7 +182,7 @@ public class EasyExcelFactory { /** * Build excel the write - * + * * @return */ public static ExcelWriterBuilder write() { @@ -181,7 +191,7 @@ public class EasyExcelFactory { /** * Build excel the write - * + * * @param file * @return */ @@ -191,7 +201,7 @@ public class EasyExcelFactory { /** * Build excel the write - * + * * @param file * @param head * @return @@ -207,7 +217,7 @@ public class EasyExcelFactory { /** * Build excel the write - * + * * @param pathName * @return */ @@ -217,7 +227,7 @@ public class EasyExcelFactory { /** * Build excel the write - * + * * @param pathName * @param head * @return @@ -233,7 +243,7 @@ public class EasyExcelFactory { /** * Build excel the write - * + * * @param outputStream * @return */ @@ -243,7 +253,7 @@ public class EasyExcelFactory { /** * Build excel the write - * + * * @param outputStream * @param head * @return @@ -259,7 +269,7 @@ public class EasyExcelFactory { /** * Build excel the write - * + * * @param uri * @return */ @@ -269,7 +279,7 @@ public class EasyExcelFactory { /** * Build excel the write - * + * * @param uri * @param head * @return @@ -285,7 +295,7 @@ public class EasyExcelFactory { /** * Build excel the 'writerSheet' - * + * * @return */ public static ExcelWriterSheetBuilder writerSheet() { @@ -294,7 +304,7 @@ public class EasyExcelFactory { /** * Build excel the 'writerSheet' - * + * * @param sheetNo * @return */ @@ -304,7 +314,7 @@ public class EasyExcelFactory { /** * Build excel the 'writerSheet' - * + * * @param sheetName * @return */ @@ -314,7 +324,7 @@ public class EasyExcelFactory { /** * Build excel the 'writerSheet' - * + * * @param sheetNo * @param sheetName * @return @@ -332,7 +342,7 @@ public class EasyExcelFactory { /** * Build excel the 'writerTable' - * + * * @return */ public static ExcelWriterTableBuilder writerTable() { @@ -341,7 +351,7 @@ public class EasyExcelFactory { /** * Build excel the 'writerTable' - * + * * @param tableNo * @return */ @@ -355,7 +365,7 @@ public class EasyExcelFactory { /** * Build excel the read - * + * * @return */ public static ExcelReaderBuilder read() { @@ -364,7 +374,7 @@ public class EasyExcelFactory { /** * Build excel the read - * + * * @param file * @return */ @@ -374,7 +384,7 @@ public class EasyExcelFactory { /** * Build excel the read - * + * * @param file * @param readListener * @return @@ -385,7 +395,7 @@ public class EasyExcelFactory { /** * Build excel the read - * + * * @param file * @param head * @param readListener @@ -405,7 +415,7 @@ public class EasyExcelFactory { /** * Build excel the read - * + * * @param pathName * @return */ @@ -415,7 +425,7 @@ public class EasyExcelFactory { /** * Build excel the read - * + * * @param pathName * @param readListener * @return @@ -426,7 +436,7 @@ public class EasyExcelFactory { /** * Build excel the read - * + * * @param pathName * @param head * @param readListener @@ -446,7 +456,7 @@ public class EasyExcelFactory { /** * Build excel the read - * + * * @param inputStream * @return */ @@ -456,7 +466,7 @@ public class EasyExcelFactory { /** * Build excel the read - * + * * @param inputStream * @param readListener * @return @@ -467,7 +477,7 @@ public class EasyExcelFactory { /** * Build excel the read - * + * * @param inputStream * @param head * @param readListener @@ -487,7 +497,7 @@ public class EasyExcelFactory { /** * Build excel the read - * + * * @param uri * @return */ @@ -497,7 +507,7 @@ public class EasyExcelFactory { /** * Build excel the read - * + * * @param uri * @param readListener * @return @@ -508,7 +518,7 @@ public class EasyExcelFactory { /** * Build excel the read - * + * * @param uri * @param head * @param readListener @@ -528,7 +538,7 @@ public class EasyExcelFactory { /** * Build excel the 'readSheet' - * + * * @return */ public static ExcelReaderSheetBuilder readSheet() { @@ -537,7 +547,7 @@ public class EasyExcelFactory { /** * Build excel the 'readSheet' - * + * * @param sheetNo * @return */ @@ -547,7 +557,7 @@ public class EasyExcelFactory { /** * Build excel the 'readSheet' - * + * * @param sheetName * @return */ @@ -557,7 +567,7 @@ public class EasyExcelFactory { /** * Build excel the 'readSheet' - * + * * @param sheetNo * @param sheetName * @return diff --git a/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java b/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java index a4802760..bfed46ed 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java @@ -54,27 +54,11 @@ public class XlsxSaxAnalyser implements ExcelExecutor { ReadWorkbookHolder readWorkbookHolder = analysisContext.readWorkbookHolder(); OPCPackage pkg = readOpcPackage(readWorkbookHolder); - PackagePart sharedStringsTablePackagePart = pkg.getPartsByContentType(XSSFRelation.SHARED_STRINGS.getContentType()).get(0); - if (readWorkbookHolder.getReadCache() == null) { - long size = sharedStringsTablePackagePart.getSize(); - if (size < 0) { - size = sharedStringsTablePackagePart.getInputStream().available(); - } - if (size < USE_MAP_CACHE_SIZE) { - if (LOGGER.isDebugEnabled()) { - LOGGER.info("Use map cache.size:{}", size); - } - readWorkbookHolder.setReadCache(new MapCache()); - } else { - if (LOGGER.isDebugEnabled()) { - LOGGER.info("Use ehcache.size:{}", size); - } - readWorkbookHolder.setReadCache(new Ehcache()); - } - } - readWorkbookHolder.getReadCache().init(analysisContext); + + // Specify default cache + defaultReadCache(readWorkbookHolder, sharedStringsTablePackagePart); // Analysis sharedStringsTable.xml analysisSharedStringsTable(sharedStringsTablePackagePart.getInputStream(), readWorkbookHolder); @@ -98,6 +82,30 @@ public class XlsxSaxAnalyser implements ExcelExecutor { } } + private void defaultReadCache(ReadWorkbookHolder readWorkbookHolder, PackagePart sharedStringsTablePackagePart) + throws IOException { + if (readWorkbookHolder.getReadCache() != null) { + readWorkbookHolder.getReadCache().init(analysisContext); + return; + } + long size = sharedStringsTablePackagePart.getSize(); + if (size < 0) { + size = sharedStringsTablePackagePart.getInputStream().available(); + } + if (size < USE_MAP_CACHE_SIZE) { + if (LOGGER.isDebugEnabled()) { + LOGGER.info("Use map cache.size:{}", size); + } + readWorkbookHolder.setReadCache(new MapCache()); + } else { + if (LOGGER.isDebugEnabled()) { + LOGGER.info("Use ehcache.size:{}", size); + } + readWorkbookHolder.setReadCache(new Ehcache()); + } + readWorkbookHolder.getReadCache().init(analysisContext); + } + private void analysisUse1904WindowDate(XSSFReader xssfReader, ReadWorkbookHolder readWorkbookHolder) throws Exception { if (readWorkbookHolder.globalConfiguration().getUse1904windowing() != null) { diff --git a/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java b/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java index d92d7731..96f8eda2 100644 --- a/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java +++ b/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java @@ -103,7 +103,7 @@ public class AnalysisContextImpl implements AnalysisContext { sheetName = sheetName.trim(); } if (sheetName.equals(readSheetHolder.getSheetName())) { - readSheetHolder.setSheetNo(readSheetHolder.getSheetNo()); + readSheetHolder.setSheetNo(readSheetExcel.getSheetNo()); return; } } diff --git a/src/main/java/com/alibaba/excel/converters/bigdecimal/BigDecimalStringConverter.java b/src/main/java/com/alibaba/excel/converters/bigdecimal/BigDecimalStringConverter.java index babdf7fd..df6868b3 100644 --- a/src/main/java/com/alibaba/excel/converters/bigdecimal/BigDecimalStringConverter.java +++ b/src/main/java/com/alibaba/excel/converters/bigdecimal/BigDecimalStringConverter.java @@ -1,6 +1,7 @@ package com.alibaba.excel.converters.bigdecimal; import java.math.BigDecimal; +import java.text.ParseException; import com.alibaba.excel.converters.Converter; import com.alibaba.excel.enums.CellDataTypeEnum; @@ -28,8 +29,8 @@ public class BigDecimalStringConverter implements Converter { @Override public BigDecimal convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, - GlobalConfiguration globalConfiguration) { - return new BigDecimal(cellData.getStringValue()); + GlobalConfiguration globalConfiguration) throws ParseException { + return NumberUtils.parseBigDecimal(cellData.getStringValue(), contentProperty); } @Override diff --git a/src/main/java/com/alibaba/excel/converters/byteconverter/ByteStringConverter.java b/src/main/java/com/alibaba/excel/converters/byteconverter/ByteStringConverter.java index b9c2a922..b4996b5e 100644 --- a/src/main/java/com/alibaba/excel/converters/byteconverter/ByteStringConverter.java +++ b/src/main/java/com/alibaba/excel/converters/byteconverter/ByteStringConverter.java @@ -1,5 +1,7 @@ package com.alibaba.excel.converters.byteconverter; +import java.text.ParseException; + import com.alibaba.excel.converters.Converter; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.metadata.CellData; @@ -26,8 +28,8 @@ public class ByteStringConverter implements Converter { @Override public Byte convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, - GlobalConfiguration globalConfiguration) { - return Byte.valueOf(cellData.getStringValue()); + GlobalConfiguration globalConfiguration) throws ParseException { + return NumberUtils.parseByte(cellData.getStringValue(), contentProperty); } @Override diff --git a/src/main/java/com/alibaba/excel/converters/doubleconverter/DoubleStringConverter.java b/src/main/java/com/alibaba/excel/converters/doubleconverter/DoubleStringConverter.java index 76b6e474..5cf1482f 100644 --- a/src/main/java/com/alibaba/excel/converters/doubleconverter/DoubleStringConverter.java +++ b/src/main/java/com/alibaba/excel/converters/doubleconverter/DoubleStringConverter.java @@ -1,5 +1,7 @@ package com.alibaba.excel.converters.doubleconverter; +import java.text.ParseException; + import com.alibaba.excel.converters.Converter; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.metadata.CellData; @@ -26,8 +28,8 @@ public class DoubleStringConverter implements Converter { @Override public Double convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, - GlobalConfiguration globalConfiguration) { - return Double.valueOf(cellData.getStringValue()); + GlobalConfiguration globalConfiguration) throws ParseException { + return NumberUtils.parseDouble(cellData.getStringValue(), contentProperty); } @Override diff --git a/src/main/java/com/alibaba/excel/converters/floatconverter/FloatStringConverter.java b/src/main/java/com/alibaba/excel/converters/floatconverter/FloatStringConverter.java index fe5bf6a6..c4e2ed92 100644 --- a/src/main/java/com/alibaba/excel/converters/floatconverter/FloatStringConverter.java +++ b/src/main/java/com/alibaba/excel/converters/floatconverter/FloatStringConverter.java @@ -1,5 +1,7 @@ package com.alibaba.excel.converters.floatconverter; +import java.text.ParseException; + import com.alibaba.excel.converters.Converter; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.metadata.CellData; @@ -26,8 +28,8 @@ public class FloatStringConverter implements Converter { @Override public Float convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, - GlobalConfiguration globalConfiguration) { - return Float.valueOf(cellData.getStringValue()); + GlobalConfiguration globalConfiguration) throws ParseException { + return NumberUtils.parseFloat(cellData.getStringValue(), contentProperty); } @Override diff --git a/src/main/java/com/alibaba/excel/converters/integer/IntegerStringConverter.java b/src/main/java/com/alibaba/excel/converters/integer/IntegerStringConverter.java index 6b2dd7b4..c2394e74 100644 --- a/src/main/java/com/alibaba/excel/converters/integer/IntegerStringConverter.java +++ b/src/main/java/com/alibaba/excel/converters/integer/IntegerStringConverter.java @@ -1,5 +1,7 @@ package com.alibaba.excel.converters.integer; +import java.text.ParseException; + import com.alibaba.excel.converters.Converter; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.metadata.CellData; @@ -26,8 +28,8 @@ public class IntegerStringConverter implements Converter { @Override public Integer convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, - GlobalConfiguration globalConfiguration) { - return Integer.valueOf(cellData.getStringValue()); + GlobalConfiguration globalConfiguration) throws ParseException { + return NumberUtils.parseInteger(cellData.getStringValue(), contentProperty); } @Override diff --git a/src/main/java/com/alibaba/excel/converters/longconverter/LongStringConverter.java b/src/main/java/com/alibaba/excel/converters/longconverter/LongStringConverter.java index 4954c1cb..1c9bfd58 100644 --- a/src/main/java/com/alibaba/excel/converters/longconverter/LongStringConverter.java +++ b/src/main/java/com/alibaba/excel/converters/longconverter/LongStringConverter.java @@ -1,5 +1,7 @@ package com.alibaba.excel.converters.longconverter; +import java.text.ParseException; + import com.alibaba.excel.converters.Converter; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.metadata.CellData; @@ -26,8 +28,8 @@ public class LongStringConverter implements Converter { @Override public Long convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, - GlobalConfiguration globalConfiguration) { - return Long.valueOf(cellData.getStringValue()); + GlobalConfiguration globalConfiguration) throws ParseException { + return NumberUtils.parseLong(cellData.getStringValue(), contentProperty); } @Override diff --git a/src/main/java/com/alibaba/excel/converters/shortconverter/ShortStringConverter.java b/src/main/java/com/alibaba/excel/converters/shortconverter/ShortStringConverter.java index c5aa255f..2a272fad 100644 --- a/src/main/java/com/alibaba/excel/converters/shortconverter/ShortStringConverter.java +++ b/src/main/java/com/alibaba/excel/converters/shortconverter/ShortStringConverter.java @@ -1,5 +1,7 @@ package com.alibaba.excel.converters.shortconverter; +import java.text.ParseException; + import com.alibaba.excel.converters.Converter; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.metadata.CellData; @@ -26,8 +28,8 @@ public class ShortStringConverter implements Converter { @Override public Short convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, - GlobalConfiguration globalConfiguration) { - return Short.valueOf(cellData.getStringValue()); + GlobalConfiguration globalConfiguration) throws ParseException { + return NumberUtils.parseShort(cellData.getStringValue(), contentProperty); } @Override diff --git a/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java b/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java index e939150e..cdbdeed1 100644 --- a/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java +++ b/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java @@ -3,6 +3,7 @@ package com.alibaba.excel.metadata.property; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.TreeMap; @@ -51,11 +52,16 @@ public class ExcelHeadProperty { * Configuration column information */ private Map contentPropertyMap; + /** + * Fields ignored + */ + private Map ignoreMap; public ExcelHeadProperty(Class headClazz, List> head, Boolean convertAllFiled) { this.headClazz = headClazz; headMap = new TreeMap(); contentPropertyMap = new TreeMap(); + ignoreMap = new HashMap(16); headKind = HeadKindEnum.NONE; headRowNumber = 0; if (head != null && !head.isEmpty()) { @@ -118,10 +124,12 @@ public class ExcelHeadProperty { for (Field field : fieldList) { ExcelIgnore excelIgnore = field.getAnnotation(ExcelIgnore.class); if (excelIgnore != null) { + ignoreMap.put(field.getName(), field); continue; } ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class); if (excelProperty == null && convertAllFiled != null && !convertAllFiled) { + ignoreMap.put(field.getName(), field); continue; } if (excelProperty == null || excelProperty.index() < 0) { @@ -146,8 +154,7 @@ public class ExcelHeadProperty { index++; } for (Map.Entry entry : customFiledMap.entrySet()) { - initOneColumnProperty(index, entry.getValue(), Boolean.FALSE); - index++; + initOneColumnProperty(entry.getKey(), entry.getValue(), Boolean.TRUE); } headKind = HeadKindEnum.CLASS; } @@ -155,20 +162,16 @@ public class ExcelHeadProperty { private void initOneColumnProperty(int index, Field field, Boolean forceIndex) { ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class); List tmpHeadList = new ArrayList(); - Boolean forceName = Boolean.TRUE; - if (excelProperty != null) { - tmpHeadList = Arrays.asList(excelProperty.value()); - } else { - forceName = Boolean.FALSE; - tmpHeadList.add(field.getName()); - } - if (tmpHeadList.isEmpty() || StringUtils.isEmpty(tmpHeadList.get(0))) { - forceName = Boolean.FALSE; + boolean notForceName = excelProperty == null || excelProperty.value().length <= 0 + || (excelProperty.value().length == 1 && StringUtils.isEmpty((excelProperty.value())[0])); + if (notForceName) { tmpHeadList.add(field.getName()); + } else { + tmpHeadList = Arrays.asList(excelProperty.value()); } - Head head = new Head(index, field.getName(), tmpHeadList, forceIndex, forceName); + Head head = new Head(index, field.getName(), tmpHeadList, forceIndex, !notForceName); ExcelContentProperty excelContentProperty = new ExcelContentProperty(); - if (excelProperty != null && excelProperty.converter() != null) { + if (excelProperty != null) { Class convertClazz = excelProperty.converter(); if (convertClazz != AutoConverter.class) { try { @@ -233,4 +236,11 @@ public class ExcelHeadProperty { this.contentPropertyMap = contentPropertyMap; } + public Map getIgnoreMap() { + return ignoreMap; + } + + public void setIgnoreMap(Map ignoreMap) { + this.ignoreMap = ignoreMap; + } } diff --git a/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java b/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java index 80deec44..4ff1c690 100644 --- a/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java +++ b/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java @@ -1,8 +1,6 @@ package com.alibaba.excel.read.listener; -import java.util.ArrayList; import java.util.HashMap; -import java.util.List; import java.util.Map; import com.alibaba.excel.context.AnalysisContext; @@ -39,12 +37,17 @@ public class ModelBuildEventListener extends AbstractIgnoreExceptionReadListener } private Object buildStringList(Map cellDataMap, ReadHolder currentReadHolder) { - List list = new ArrayList(); - for (CellData cellData : cellDataMap.values()) { - list.add((String)convertValue(cellData, String.class, null, currentReadHolder.converterMap(), + Map map = new HashMap(cellDataMap.size() * 4 / 3 + 1); + for (Map.Entry entry : cellDataMap.entrySet()) { + CellData cellData = entry.getValue(); + if (cellData.getType() == CellDataTypeEnum.EMPTY) { + map.put(entry.getKey(), null); + continue; + } + map.put(entry.getKey(), (String)convertValue(cellData, String.class, null, currentReadHolder.converterMap(), currentReadHolder.globalConfiguration())); } - return list; + return map; } private Object buildUserModel(Map cellDataMap, ReadHolder currentReadHolder) { @@ -61,7 +64,7 @@ public class ModelBuildEventListener extends AbstractIgnoreExceptionReadListener Map contentPropertyMap = excelReadHeadProperty.getContentPropertyMap(); for (Map.Entry entry : headMap.entrySet()) { Integer index = entry.getKey(); - if (index >= cellDataMap.size()) { + if (!cellDataMap.containsKey(index)) { continue; } CellData cellData = cellDataMap.get(index); diff --git a/src/main/java/com/alibaba/excel/read/metadata/ReadSheet.java b/src/main/java/com/alibaba/excel/read/metadata/ReadSheet.java index b9d5112c..f3a3a7b3 100644 --- a/src/main/java/com/alibaba/excel/read/metadata/ReadSheet.java +++ b/src/main/java/com/alibaba/excel/read/metadata/ReadSheet.java @@ -2,7 +2,7 @@ package com.alibaba.excel.read.metadata; /** * Read sheet - * + * * @author jipengfei */ public class ReadSheet extends ReadBasicParameter { @@ -41,4 +41,9 @@ public class ReadSheet extends ReadBasicParameter { public void setSheetName(String sheetName) { this.sheetName = sheetName; } + + @Override + public String toString() { + return "ReadSheet{" + "sheetNo=" + sheetNo + ", sheetName='" + sheetName + '\'' + "} " + super.toString(); + } } diff --git a/src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java b/src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java index b0391a62..a84f6471 100644 --- a/src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java +++ b/src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java @@ -9,6 +9,7 @@ import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.ConverterKeyBuild; import com.alibaba.excel.converters.DefaultConverterLoader; +import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.HeadKindEnum; import com.alibaba.excel.enums.HolderEnum; import com.alibaba.excel.event.AnalysisEventListener; @@ -144,7 +145,7 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH if (!HeadKindEnum.CLASS.equals(analysisContext.currentReadHolder().excelReadHeadProperty().getHeadKind())) { return; } - List dataList = (List)buildStringList(cellDataMap, analysisContext.currentReadHolder()); + Map dataMap = buildStringMap(cellDataMap, analysisContext.currentReadHolder()); ExcelReadHeadProperty excelHeadPropertyData = analysisContext.readSheetHolder().excelReadHeadProperty(); Map headMapData = excelHeadPropertyData.getHeadMap(); Map contentPropertyMapData = excelHeadPropertyData.getContentPropertyMap(); @@ -159,8 +160,10 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH continue; } String headName = headData.getHeadNameList().get(0); - for (int i = 0; i < dataList.size(); i++) { - String headString = dataList.get(i); + + for (Map.Entry stringEntry : dataMap.entrySet()) { + String headString = stringEntry.getValue(); + Integer stringKey = stringEntry.getKey(); if (StringUtils.isEmpty(headString)) { continue; } @@ -168,9 +171,9 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH headString = headString.trim(); } if (headName.equals(headString)) { - headData.setColumnIndex(i); - tmpHeadMap.put(i, headData); - tmpContentPropertyMap.put(i, contentPropertyMapData.get(entry.getKey())); + headData.setColumnIndex(stringKey); + tmpHeadMap.put(stringKey, headData); + tmpContentPropertyMap.put(stringKey, contentPropertyMapData.get(entry.getKey())); break; } } @@ -179,9 +182,14 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH excelHeadPropertyData.setContentPropertyMap(tmpContentPropertyMap); } - private Object buildStringList(Map cellDataMa, ReadHolder readHolder) { - List list = new ArrayList(); - for (CellData cellData : cellDataMa.values()) { + private Map buildStringMap(Map cellDataMap, ReadHolder readHolder) { + Map stringMap = new HashMap(cellDataMap.size() * 4 / 3 + 1); + for (Map.Entry entry : cellDataMap.entrySet()) { + CellData cellData = entry.getValue(); + if (cellData.getType() == CellDataTypeEnum.EMPTY) { + stringMap.put(entry.getKey(), null); + continue; + } Converter converter = readHolder.converterMap().get(ConverterKeyBuild.buildKey(String.class, cellData.getType())); if (converter == null) { @@ -189,12 +197,13 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH "Converter not found, convert " + cellData.getType() + " to String"); } try { - list.add((String)(converter.convertToJavaData(cellData, null, readHolder.globalConfiguration()))); + stringMap.put(entry.getKey(), + (String)(converter.convertToJavaData(cellData, null, readHolder.globalConfiguration()))); } catch (Exception e) { throw new ExcelDataConvertException("Convert data " + cellData + " to String error ", e); } } - return list; + return stringMap; } public List getReadListenerList() { diff --git a/src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java b/src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java index 236e42fb..5895b20a 100644 --- a/src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java +++ b/src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java @@ -36,6 +36,7 @@ public class ReadSheetHolder extends AbstractReadHolder { this.readSheet = readSheet; this.parentReadWorkbookHolder = readWorkbookHolder; this.sheetNo = readSheet.getSheetNo(); + this.sheetName=readSheet.getSheetName(); } public ReadSheet getReadSheet() { diff --git a/src/main/java/com/alibaba/excel/util/NumberUtils.java b/src/main/java/com/alibaba/excel/util/NumberUtils.java index 678da150..257c3b1a 100644 --- a/src/main/java/com/alibaba/excel/util/NumberUtils.java +++ b/src/main/java/com/alibaba/excel/util/NumberUtils.java @@ -1,14 +1,16 @@ package com.alibaba.excel.util; +import java.math.BigDecimal; import java.math.RoundingMode; import java.text.DecimalFormat; +import java.text.ParseException; import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.property.ExcelContentProperty; /** * Number utils - * + * * @author zhuangjiaju */ public class NumberUtils { @@ -35,7 +37,7 @@ public class NumberUtils { /** * format - * + * * @param num * @param contentProperty * @return @@ -43,4 +45,124 @@ public class NumberUtils { public static CellData formatToCellData(Number num, ExcelContentProperty contentProperty) { return new CellData(format(num, contentProperty)); } + + /** + * parse + * + * @param string + * @param contentProperty + * @return + */ + public static Short parseShort(String string, ExcelContentProperty contentProperty) throws ParseException { + if (!hasFormat(contentProperty)) { + return Short.valueOf(string); + } + return parse(string, contentProperty).shortValue(); + } + + /** + * parse + * + * @param string + * @param contentProperty + * @return + */ + public static Long parseLong(String string, ExcelContentProperty contentProperty) throws ParseException { + if (!hasFormat(contentProperty)) { + return Long.valueOf(string); + } + return parse(string, contentProperty).longValue(); + } + + /** + * parse + * + * @param string + * @param contentProperty + * @return + */ + public static Integer parseInteger(String string, ExcelContentProperty contentProperty) throws ParseException { + if (!hasFormat(contentProperty)) { + return Integer.valueOf(string); + } + return parse(string, contentProperty).intValue(); + } + + /** + * parse + * + * @param string + * @param contentProperty + * @return + */ + public static Float parseFloat(String string, ExcelContentProperty contentProperty) throws ParseException { + if (!hasFormat(contentProperty)) { + return Float.valueOf(string); + } + return parse(string, contentProperty).floatValue(); + } + + /** + * parse + * + * @param string + * @param contentProperty + * @return + */ + public static BigDecimal parseBigDecimal(String string, ExcelContentProperty contentProperty) + throws ParseException { + if (!hasFormat(contentProperty)) { + return new BigDecimal(string); + } + return BigDecimal.valueOf(parse(string, contentProperty).doubleValue()); + } + + /** + * parse + * + * @param string + * @param contentProperty + * @return + */ + public static Byte parseByte(String string, ExcelContentProperty contentProperty) throws ParseException { + if (!hasFormat(contentProperty)) { + return Byte.valueOf(string); + } + return parse(string, contentProperty).byteValue(); + } + + /** + * parse + * + * @param string + * @param contentProperty + * @return + */ + public static Double parseDouble(String string, ExcelContentProperty contentProperty) throws ParseException { + if (!hasFormat(contentProperty)) { + return Double.valueOf(string); + } + return parse(string, contentProperty).doubleValue(); + } + + private static boolean hasFormat(ExcelContentProperty contentProperty) { + return contentProperty != null && contentProperty.getNumberFormatProperty() != null + && !StringUtils.isEmpty(contentProperty.getNumberFormatProperty().getFormat()); + } + + /** + * parse + * + * @param string + * @param contentProperty + * @return + * @throws ParseException + */ + private static Number parse(String string, ExcelContentProperty contentProperty) throws ParseException { + String format = contentProperty.getNumberFormatProperty().getFormat(); + RoundingMode roundingMode = contentProperty.getNumberFormatProperty().getRoundingMode(); + DecimalFormat decimalFormat = new DecimalFormat(format); + decimalFormat.setRoundingMode(roundingMode); + return decimalFormat.parse(string); + } } diff --git a/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java b/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java index fa028e52..e4181f95 100644 --- a/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java +++ b/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java @@ -1,5 +1,6 @@ package com.alibaba.excel.write; +import java.lang.reflect.Field; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -90,7 +91,9 @@ public class ExcelBuilderImpl implements ExcelBuilder { @Override public void finish() { - context.finish(); + if (context != null) { + context.finish(); + } } @Override @@ -206,9 +209,11 @@ public class ExcelBuilderImpl implements ExcelBuilder { if (cellIndex != 0) { cellIndex++; } + Map ignoreMap = context.currentWriteHolder().excelWriteHeadProperty().getIgnoreMap(); Set> entrySet = beanMap.entrySet(); for (Map.Entry entry : entrySet) { - if (entry.getValue() == null || beanMapHandledSet.contains(entry.getKey())) { + if (entry.getValue() == null || beanMapHandledSet.contains(entry.getKey()) + || ignoreMap.containsKey(entry.getKey())) { continue; } beforeCellCreate(row, null, relativeRowIndex); diff --git a/src/main/java/com/alibaba/excel/write/metadata/WriteSheet.java b/src/main/java/com/alibaba/excel/write/metadata/WriteSheet.java index 8a7dcc31..fa5c1baf 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/WriteSheet.java +++ b/src/main/java/com/alibaba/excel/write/metadata/WriteSheet.java @@ -9,7 +9,7 @@ import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy; /** * Write sheet - * + * * @author jipengfei */ public class WriteSheet extends WriteBasicParameter { @@ -23,7 +23,7 @@ public class WriteSheet extends WriteBasicParameter { private String sheetName; /** * column with - * + * * @deprecated please use {@link SimpleColumnWidthStyleStrategy} */ @Deprecated @@ -66,4 +66,9 @@ public class WriteSheet extends WriteBasicParameter { public void setTableStyle(TableStyle tableStyle) { this.tableStyle = tableStyle; } + + @Override + public String toString() { + return "WriteSheet{" + "sheetNo=" + sheetNo + ", sheetName='" + sheetName + '\'' + "} " + super.toString(); + } } diff --git a/src/main/java/com/alibaba/excel/write/style/row/SimpleRowHeightStyleStrategy.java b/src/main/java/com/alibaba/excel/write/style/row/SimpleRowHeightStyleStrategy.java index a35a785d..fee1c605 100644 --- a/src/main/java/com/alibaba/excel/write/style/row/SimpleRowHeightStyleStrategy.java +++ b/src/main/java/com/alibaba/excel/write/style/row/SimpleRowHeightStyleStrategy.java @@ -4,7 +4,7 @@ import org.apache.poi.ss.usermodel.Row; /** * Set the head column high and content column high - * + * * @author zhuangjiaju */ public class SimpleRowHeightStyleStrategy extends AbstractRowHeightStyleStrategy { @@ -19,14 +19,14 @@ public class SimpleRowHeightStyleStrategy extends AbstractRowHeightStyleStrategy @Override protected void setHeadColumnHeight(Row row, int relativeRowIndex) { if (headRowHeight != null) { - row.setHeight(headRowHeight); + row.setHeightInPoints(headRowHeight); } } @Override protected void setContentColumnHeight(Row row, int relativeRowIndex) { if (contentRowHeight != null) { - row.setHeight(contentRowHeight); + row.setHeightInPoints(contentRowHeight); } } diff --git a/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationData.java b/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationData.java index 73b92904..d10f87e0 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationData.java @@ -2,7 +2,14 @@ package com.alibaba.easyexcel.test.core.annotation; import java.util.Date; +import com.alibaba.excel.annotation.ExcelIgnore; import com.alibaba.excel.annotation.ExcelProperty; +import com.alibaba.excel.annotation.format.DateTimeFormat; +import com.alibaba.excel.annotation.format.NumberFormat; +import com.alibaba.excel.annotation.write.style.ColumnWidth; +import com.alibaba.excel.annotation.write.style.ContentRowHeight; +import com.alibaba.excel.annotation.write.style.HeadRowHeight; +import com.alibaba.excel.converters.doubleconverter.DoubleStringConverter; import lombok.Data; @@ -10,7 +17,16 @@ import lombok.Data; * @author zhuangjiaju */ @Data +@ColumnWidth(30 * 256) +@HeadRowHeight(15) +@ContentRowHeight(20) public class AnnotationData { @ExcelProperty("日期") - private Date name; + @DateTimeFormat("yyyy年MM月dd日HH时mm分ss秒") + private Date date; + @ExcelProperty(value = "数字", converter = DoubleStringConverter.class) + @NumberFormat("#.##%") + private Double number; + @ExcelIgnore + private String ignore; } diff --git a/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataListener.java b/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataListener.java new file mode 100644 index 00000000..bc9349c8 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataListener.java @@ -0,0 +1,41 @@ +package com.alibaba.easyexcel.test.core.annotation; + +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; + +/** + * @author zhuangjiaju + */ +public class AnnotationDataListener extends AnalysisEventListener { + private static final Logger LOGGER = LoggerFactory.getLogger(AnnotationDataListener.class); + List list = new ArrayList(); + + @Override + public void invoke(AnnotationData data, AnalysisContext context) { + list.add(data); + } + + @Override + public void doAfterAllAnalysed(AnalysisContext context) { + Assert.assertEquals(list.size(), 1); + AnnotationData data = list.get(0); + try { + Assert.assertEquals(data.getDate(), DateUtils.parseDate("2020-01-01 01:01:01")); + } catch (ParseException e) { + throw new ExcelCommonException("Test Exception", e); + } + Assert.assertEquals(data.getNumber(), 99.99, 0.00); + LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); + } +} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataTest.java index 81667f84..3061dcc3 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataTest.java @@ -4,13 +4,12 @@ import java.io.File; import java.util.ArrayList; import java.util.List; -import org.junit.Assert; +import org.junit.BeforeClass; import org.junit.Test; -import com.alibaba.easyexcel.test.core.simple.SimpleData; -import com.alibaba.easyexcel.test.core.simple.SimpleDataListener; import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcelFactory; +import com.alibaba.excel.util.DateUtils; /** * Annotation data test @@ -19,56 +18,37 @@ import com.alibaba.excel.EasyExcelFactory; */ public class AnnotationDataTest { - private File file07 = TestFileUtil.createNewFile("simple07.xlsx"); - private File file03 = TestFileUtil.createNewFile("simple03.xls"); + private static File file07; + private static File file03; - - - @Test - public void readAndWrite07() { - EasyExcelFactory.write(file07, com.alibaba.easyexcel.test.core.simple.SimpleData.class).sheet().doWrite(data()) - .finish(); - EasyExcelFactory.read(file07, com.alibaba.easyexcel.test.core.simple.SimpleData.class, - new com.alibaba.easyexcel.test.core.simple.SimpleDataListener()).sheet().doRead().finish(); + @BeforeClass + public static void init() { + file07 = TestFileUtil.createNewFile("annotation07.xlsx"); + file03 = TestFileUtil.createNewFile("annotation03.xls"); } @Test - public void synchronousRead07() { - // Synchronous read file - List list = EasyExcelFactory.read(file07).head(com.alibaba.easyexcel.test.core.simple.SimpleData.class) - .sheet().doReadSync(); - Assert.assertEquals(list.size(), 10); - Assert.assertTrue(list.get(0) instanceof com.alibaba.easyexcel.test.core.simple.SimpleData); - Assert.assertEquals(((com.alibaba.easyexcel.test.core.simple.SimpleData)list.get(0)).getName(), "姓名0"); + public void T01ReadAndWrite07() throws Exception { + readAndWrite(file07); } @Test - public void readAndWrite03() { - EasyExcelFactory.write(file03, com.alibaba.easyexcel.test.core.simple.SimpleData.class).sheet().doWrite(data()) - .finish(); - EasyExcelFactory.read(file03, com.alibaba.easyexcel.test.core.simple.SimpleData.class, new SimpleDataListener()) - .sheet().doRead().finish(); + public void T02ReadAndWrite03() throws Exception { + readAndWrite(file03); } - @Test - - public void synchronousRead03() { - // Synchronous read file - List list = EasyExcelFactory.read(file03).head(com.alibaba.easyexcel.test.core.simple.SimpleData.class) - .sheet().doReadSync(); - Assert.assertEquals(list.size(), 10); - Assert.assertTrue(list.get(0) instanceof com.alibaba.easyexcel.test.core.simple.SimpleData); - Assert.assertEquals(((com.alibaba.easyexcel.test.core.simple.SimpleData)list.get(0)).getName(), "姓名0"); + private void readAndWrite(File file) throws Exception { + EasyExcelFactory.write(file, AnnotationData.class).sheet().doWrite(data()).finish(); + EasyExcelFactory.read(file, AnnotationData.class, new AnnotationDataListener()).sheet().doRead().finish(); } - private List data() { - List list = - new ArrayList(); - for (int i = 0; i < 10; i++) { - com.alibaba.easyexcel.test.core.simple.SimpleData simpleData = new SimpleData(); - simpleData.setName("姓名" + i); - list.add(simpleData); - } + private List data() throws Exception { + List list = new ArrayList(); + AnnotationData data = new AnnotationData(); + data.setDate(DateUtils.parseDate("2020-01-01 01:01:01")); + data.setNumber(99.99); + data.setIgnore("忽略"); + list.add(data); return list; } } diff --git a/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationIndexAndNameData.java b/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationIndexAndNameData.java new file mode 100644 index 00000000..ec74439e --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationIndexAndNameData.java @@ -0,0 +1,20 @@ +package com.alibaba.easyexcel.test.core.annotation; + +import com.alibaba.excel.annotation.ExcelProperty; + +import lombok.Data; + +/** + * @author zhuangjiaju + */ +@Data +public class AnnotationIndexAndNameData { + @ExcelProperty(value = "第四个", index = 4) + private String index4; + @ExcelProperty(value = "第二个") + private String index2; + @ExcelProperty(index = 0) + private String index0; + @ExcelProperty(value = "第一个", index = 1) + private String index1; +} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationIndexAndNameDataListener.java b/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationIndexAndNameDataListener.java new file mode 100644 index 00000000..0dcf3c9a --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationIndexAndNameDataListener.java @@ -0,0 +1,36 @@ +package com.alibaba.easyexcel.test.core.annotation; + +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.fastjson.JSON; + +/** + * @author zhuangjiaju + */ +public class AnnotationIndexAndNameDataListener extends AnalysisEventListener { + private static final Logger LOGGER = LoggerFactory.getLogger(AnnotationIndexAndNameDataListener.class); + List list = new ArrayList(); + + @Override + public void invoke(AnnotationIndexAndNameData data, AnalysisContext context) { + list.add(data); + } + + @Override + public void doAfterAllAnalysed(AnalysisContext context) { + Assert.assertEquals(list.size(), 1); + AnnotationIndexAndNameData data = list.get(0); + Assert.assertEquals(data.getIndex0(), "第0个"); + Assert.assertEquals(data.getIndex1(), "第1个"); + Assert.assertEquals(data.getIndex2(), "第2个"); + Assert.assertEquals(data.getIndex4(), "第4个"); + LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); + } +} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationIndexAndNameDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationIndexAndNameDataTest.java new file mode 100644 index 00000000..e5614d6a --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationIndexAndNameDataTest.java @@ -0,0 +1,55 @@ +package com.alibaba.easyexcel.test.core.annotation; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +import org.junit.BeforeClass; +import org.junit.Test; + +import com.alibaba.easyexcel.test.util.TestFileUtil; +import com.alibaba.excel.EasyExcelFactory; + +/** + * Annotation data test + * + * @author zhuangjiaju + */ +public class AnnotationIndexAndNameDataTest { + + private static File file07; + private static File file03; + + @BeforeClass + public static void init() { + file07 = TestFileUtil.createNewFile("annotationIndexAndName07.xlsx"); + file03 = TestFileUtil.createNewFile("annotationIndexAndName03.xls"); + } + + @Test + public void T01ReadAndWrite07() { + readAndWrite(file07); + } + + @Test + public void T02ReadAndWrite03() { + readAndWrite(file03); + } + + private void readAndWrite(File file) { + EasyExcelFactory.write(file, AnnotationIndexAndNameData.class).sheet().doWrite(data()).finish(); + EasyExcelFactory.read(file, AnnotationIndexAndNameData.class, new AnnotationIndexAndNameDataListener()).sheet() + .doRead().finish(); + } + + private List data() { + List list = new ArrayList(); + AnnotationIndexAndNameData data = new AnnotationIndexAndNameData(); + data.setIndex0("第0个"); + data.setIndex1("第1个"); + data.setIndex2("第2个"); + data.setIndex4("第4个"); + list.add(data); + return list; + } +} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadData07Test.java b/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadData07Test.java index 1eae01f9..bab3927f 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadData07Test.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadData07Test.java @@ -1,12 +1,7 @@ package com.alibaba.easyexcel.test.core.head; -import java.util.ArrayList; -import java.util.List; - import org.junit.Test; -import com.alibaba.easyexcel.test.core.order.OrderData; - /** * Order data test * @@ -22,15 +17,4 @@ public class NoHeadData07Test { // writer.write(createData(10000 * 100), sheet); // writer.finish(); } - - private List createData(int count) { - List list = new ArrayList(); - for (int i = 0; i < count; i++) { - OrderData orderData = new OrderData(); - orderData.setIndex1("排序1:" + i); - orderData.setIndex10("排序10:" + i); - list.add(orderData); - } - return list; - } } diff --git a/src/test/java/com/alibaba/easyexcel/test/core/order/OrderData.java b/src/test/java/com/alibaba/easyexcel/test/core/order/OrderData.java deleted file mode 100644 index 657c74ba..00000000 --- a/src/test/java/com/alibaba/easyexcel/test/core/order/OrderData.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.alibaba.easyexcel.test.core.order; - -import com.alibaba.excel.annotation.ExcelProperty; - -/** - * @author zhuangjiaju - */ -public class OrderData { - @ExcelProperty(value = "第一个", index = 1) - private String index1; - @ExcelProperty(value = "第10个", index = 10) - private String index10; - - public String getIndex1() { - return index1; - } - - public void setIndex1(String index1) { - this.index1 = index1; - } - - public String getIndex10() { - return index10; - } - - public void setIndex10(String index10) { - this.index10 = index10; - } -} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/order/OrderData07Test.java b/src/test/java/com/alibaba/easyexcel/test/core/order/OrderData07Test.java deleted file mode 100644 index 134242a7..00000000 --- a/src/test/java/com/alibaba/easyexcel/test/core/order/OrderData07Test.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.alibaba.easyexcel.test.core.order; - -import java.util.ArrayList; -import java.util.List; - -import org.junit.Test; - -/** - * Order data test - * - * @author zhuangjiaju - */ -public class OrderData07Test { - - @Test - public void simple() { - // ExcelWriter writer = EasyExcelFactory.writerBuilder().outputFile(TestFileUtil.createNewWriteFile("order07.xlsx")) - // .head(OrderData.class).build(); - // Sheet sheet = EasyExcelFactory.writerSheetBuilder().sheetNo(0).sheetName("order").build(); - // writer.write(createData(10000 * 100), sheet); - // writer.finish(); - } - - private List createData(int count) { - List list = new ArrayList(); - for (int i = 0; i < count; i++) { - OrderData orderData = new OrderData(); - orderData.setIndex1("排序1:" + i); - orderData.setIndex10("排序10:" + i); - list.add(orderData); - } - return list; - } -} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/annotation/SimpleDataListener.java b/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataSheetNameListener.java similarity index 55% rename from src/test/java/com/alibaba/easyexcel/test/core/annotation/SimpleDataListener.java rename to src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataSheetNameListener.java index 85ecd1c9..d3a444de 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/annotation/SimpleDataListener.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataSheetNameListener.java @@ -1,4 +1,4 @@ -package com.alibaba.easyexcel.test.core.annotation; +package com.alibaba.easyexcel.test.core.simple; import java.util.ArrayList; import java.util.List; @@ -7,7 +7,6 @@ import org.junit.Assert; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.alibaba.easyexcel.test.core.simple.SimpleData; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.fastjson.JSON; @@ -15,8 +14,8 @@ import com.alibaba.fastjson.JSON; /** * @author zhuangjiaju */ -public class SimpleDataListener extends AnalysisEventListener { - private static final Logger LOGGER = LoggerFactory.getLogger(SimpleDataListener.class); +public class SimpleDataSheetNameListener extends AnalysisEventListener { + private static final Logger LOGGER = LoggerFactory.getLogger(SimpleDataSheetNameListener.class); List list = new ArrayList(); @Override @@ -26,11 +25,8 @@ public class SimpleDataListener extends AnalysisEventListener { @Override public void doAfterAllAnalysed(AnalysisContext context) { - Assert.assertEquals(list.size(), 10); - Assert.assertEquals(list.get(0).getName(), "姓名0"); - Assert.assertEquals((int)(context.readSheetHolder().getSheetNo()), 0); - Assert.assertEquals( - context.readSheetHolder().getExcelReadHeadProperty().getHeadMap().get(0).getHeadNameList().get(0), "姓名"); + Assert.assertEquals(list.size(), 1); + Assert.assertEquals(list.get(0).getName(), "张三"); LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); } } diff --git a/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataTest.java index 11704832..f66b55b0 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataTest.java @@ -55,6 +55,12 @@ public class SimpleDataTest { synchronousRead(file03); } + @Test + public void T05SheetNameRead07() { + EasyExcelFactory.read(TestFileUtil.readFile("simple" + File.separator + "simple07.xlsx"), SimpleData.class, + new SimpleDataSheetNameListener()).sheet("simple").doRead().finish(); + } + private void synchronousRead(File file) { // Synchronous read file List list = EasyExcelFactory.read(file).head(SimpleData.class).sheet().doReadSync(); diff --git a/src/test/resources/simple/simple07.xlsx b/src/test/resources/simple/simple07.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..f01c4460b032536ed1a21a8dc9c3f30964c3afcd GIT binary patch literal 10048 zcma)C1yr3|uEyQ16nCe^y|`QP;!@n*-QC^2xLa{|cP&!fwFQd3LpyVO%iNju_F6}P zBzx~9|Cf+NMgkNJ2IzSON`K{j{`~iX27J-8)RnQZw6c9G10X{K`~vDH8FHB!3J)+4 zkQE3J5YpetbgZo2IGLNJ$Bc@D(xdrb`*JD$2uW9Qz&nw@PM zPHagI%aCvvo0pp!0kPWI3J=7X7lW8)$4zLffImx>kM^8miG?E`wl0q?Am2(cA3crp zo3bw{Js!e)uu^C<=6Z7$a_*)2Kc$+)$)dw6hp zwzudL{n#&!eVA>V>+j7|3;O6-1uu#-xj?FJ#Nfp`s}8XQ%C!&b2nWr&u~H6XG_$)L zlT+RbTt9zUPCDaJc9KoBj!$xarx=3&r?Q1_JuamI$`%JG8~N|b*0r?Jf7W(*bgOh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