diff --git a/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java b/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java index 748e3256..340260c8 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java +++ b/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java @@ -4,6 +4,8 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Map; +import java.util.TreeMap; import org.apache.poi.hssf.eventusermodel.EventWorkbookBuilder; import org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener; @@ -62,7 +64,7 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelExecutor { */ private EventWorkbookBuilder.SheetRecordCollectingListener workbookBuildingListener; private FormatTrackingHSSFListener formatListener; - private List records; + private Map records; private List sheets = new ArrayList(); private HSSFWorkbook stubWorkbook; private List recordHandlers = new ArrayList(); @@ -70,7 +72,7 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelExecutor { public XlsSaxAnalyser(AnalysisContext context) throws IOException { this.analysisContext = context; - this.records = new ArrayList(); + this.records = new TreeMap(); ReadWorkbookHolder readWorkbookHolder = analysisContext.readWorkbookHolder(); if (readWorkbookHolder.getFile() != null) { this.fs = new POIFSFileSystem(readWorkbookHolder.getFile()); @@ -114,7 +116,7 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelExecutor { private void init() { lastRowNumber = 0; lastColumnNumber = 0; - records = new ArrayList(); + records = new TreeMap(); sheets = new ArrayList(); buildXlsRecordHandlers(); } @@ -131,7 +133,7 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelExecutor { thisColumn = handler.getColumn(); cellData = handler.getCellData(); if (cellData != null) { - records.add(cellData); + records.put(thisColumn, cellData); } break; } diff --git a/src/main/java/com/alibaba/excel/analysis/v03/handlers/BlankOrErrorRecordHandler.java b/src/main/java/com/alibaba/excel/analysis/v03/handlers/BlankOrErrorRecordHandler.java index c858e37e..f64d4372 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/handlers/BlankOrErrorRecordHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v03/handlers/BlankOrErrorRecordHandler.java @@ -31,7 +31,7 @@ public class BlankOrErrorRecordHandler extends AbstractXlsRecordHandler { BoolErrRecord ber = (BoolErrRecord)record; this.row = ber.getRow(); this.column = ber.getColumn(); - this.cellData = new CellData(CellDataTypeEnum.EMPTY); + this.cellData = new CellData(ber.getBooleanValue()); } } diff --git a/src/main/java/com/alibaba/excel/analysis/v07/XlsxRowResultHolder.java b/src/main/java/com/alibaba/excel/analysis/v07/XlsxRowResultHolder.java index 90c1ea98..953d45e0 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/XlsxRowResultHolder.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/XlsxRowResultHolder.java @@ -1,6 +1,6 @@ package com.alibaba.excel.analysis.v07; -import java.util.List; +import java.util.Map; import com.alibaba.excel.metadata.CellData; @@ -27,12 +27,5 @@ public interface XlsxRowResultHolder { * * @return */ - List getCurRowContent(); - - /** - * get column size - * - * @return - */ - int getColumnSize(); + Map getCurRowContent(); } diff --git a/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java index d64f315a..3f376b4d 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java @@ -6,8 +6,8 @@ import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_TAG; import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_VALUE_TAG; import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_VALUE_TYPE_TAG; -import java.util.ArrayList; -import java.util.List; +import java.util.Map; +import java.util.TreeMap; import org.apache.poi.xssf.usermodel.XSSFRichTextString; import org.xml.sax.Attributes; @@ -34,7 +34,7 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder private String currentCellIndex; private int curRow; private int curCol; - private List curRowContent = new ArrayList(); + private Map curRowContent = new TreeMap(); private CellData currentCellData; public DefaultCellHandler(AnalysisContext analysisContext) { @@ -43,7 +43,7 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder @Override public void clearResult() { - curRowContent.clear(); + curRowContent = new TreeMap(); } @Override @@ -67,6 +67,7 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder curCol = PositionUtils.getCol(currentCellIndex); // t="s" ,it's means String + // t="str" ,it's means String,but does not need to be read in the 'sharedStrings.xml' // t="inlineStr" ,it's means String // t="b" ,it's means Boolean // t="e" ,it's means Error @@ -92,8 +93,10 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder stringValue = stringValue.trim(); } currentCellData.setStringValue(stringValue); + } else if (currentCellData.getType() == CellDataTypeEnum.DIRECT_STRING) { + currentCellData.setType(CellDataTypeEnum.STRING); } - curRowContent.set(curCol, currentCellData); + curRowContent.put(curCol, currentCellData); } // This is a special form of string if (CELL_INLINE_STRING_VALUE_TAG.equals(name)) { @@ -103,7 +106,7 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder stringValue = stringValue.trim(); } currentCellData.setStringValue(stringValue); - curRowContent.set(curCol, currentCellData); + curRowContent.put(curCol, currentCellData); } } @@ -121,6 +124,7 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder } CellDataTypeEnum oldType = currentCellData.getType(); switch (oldType) { + case DIRECT_STRING: case STRING: case ERROR: currentCellData.setStringValue(currentCellValue); @@ -139,13 +143,8 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder } @Override - public List getCurRowContent() { - return this.curRowContent; - } - - @Override - public int getColumnSize() { - return this.curCol; + public Map getCurRowContent() { + return curRowContent; } } diff --git a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java index db7c0803..2b1f7d7c 100644 --- a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java +++ b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java @@ -352,6 +352,10 @@ public class WriteContextImpl implements WriteContext { if (writeWorkbookHolder.getTemplateInputStream() != null) { writeWorkbookHolder.getTemplateInputStream().close(); } + } else { + if (writeWorkbookHolder.getFile() != null && writeWorkbookHolder.getOutputStream() != null) { + writeWorkbookHolder.getOutputStream().close(); + } } } catch (IOException e) { throw new ExcelGenerateException("Can not close IO", e); diff --git a/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java b/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java index 7a6519ea..c1155e53 100644 --- a/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java +++ b/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java @@ -14,23 +14,29 @@ import com.alibaba.excel.converters.byteconverter.ByteNumberConverter; import com.alibaba.excel.converters.byteconverter.ByteStringConverter; import com.alibaba.excel.converters.date.DateNumberConverter; import com.alibaba.excel.converters.date.DateStringConverter; +import com.alibaba.excel.converters.doubleconverter.DoubleBooleanConverter; import com.alibaba.excel.converters.doubleconverter.DoubleNumberConverter; import com.alibaba.excel.converters.doubleconverter.DoubleStringConverter; +import com.alibaba.excel.converters.floatconverter.FloatBooleanConverter; import com.alibaba.excel.converters.floatconverter.FloatNumberConverter; import com.alibaba.excel.converters.floatconverter.FloatStringConverter; +import com.alibaba.excel.converters.integer.IntegerBooleanConverter; import com.alibaba.excel.converters.integer.IntegerNumberConverter; import com.alibaba.excel.converters.integer.IntegerStringConverter; +import com.alibaba.excel.converters.longconverter.LongBooleanConverter; import com.alibaba.excel.converters.longconverter.LongNumberConverter; import com.alibaba.excel.converters.longconverter.LongStringConverter; +import com.alibaba.excel.converters.shortconverter.ShortBooleanConverter; import com.alibaba.excel.converters.shortconverter.ShortNumberConverter; import com.alibaba.excel.converters.shortconverter.ShortStringConverter; +import com.alibaba.excel.converters.string.StringBooleanConverter; import com.alibaba.excel.converters.string.StringErrorConverter; import com.alibaba.excel.converters.string.StringNumberConverter; import com.alibaba.excel.converters.string.StringStringConverter; /** * Load default handler - * + * * @author zhuangjiaju */ public class DefaultConverterLoader { @@ -80,27 +86,27 @@ public class DefaultConverterLoader { putReadConverter(converterMap, new DateNumberConverter()); putReadConverter(converterMap, new DateStringConverter()); - putReadConverter(converterMap, new DoubleNumberConverter()); + putReadConverter(converterMap, new DoubleBooleanConverter()); putReadConverter(converterMap, new DoubleNumberConverter()); putReadConverter(converterMap, new DoubleStringConverter()); - putReadConverter(converterMap, new FloatNumberConverter()); + putReadConverter(converterMap, new FloatBooleanConverter()); putReadConverter(converterMap, new FloatNumberConverter()); putReadConverter(converterMap, new FloatStringConverter()); - putReadConverter(converterMap, new IntegerNumberConverter()); + putReadConverter(converterMap, new IntegerBooleanConverter()); putReadConverter(converterMap, new IntegerNumberConverter()); putReadConverter(converterMap, new IntegerStringConverter()); - putReadConverter(converterMap, new LongNumberConverter()); + putReadConverter(converterMap, new LongBooleanConverter()); putReadConverter(converterMap, new LongNumberConverter()); putReadConverter(converterMap, new LongStringConverter()); - putReadConverter(converterMap, new ShortNumberConverter()); + putReadConverter(converterMap, new ShortBooleanConverter()); putReadConverter(converterMap, new ShortNumberConverter()); putReadConverter(converterMap, new ShortStringConverter()); - putReadConverter(converterMap, new StringNumberConverter()); + putReadConverter(converterMap, new StringBooleanConverter()); putReadConverter(converterMap, new StringNumberConverter()); putReadConverter(converterMap, new StringStringConverter()); putReadConverter(converterMap, new StringErrorConverter()); diff --git a/src/main/java/com/alibaba/excel/converters/bigdecimal/BigDecimalBooleanConverter.java b/src/main/java/com/alibaba/excel/converters/bigdecimal/BigDecimalBooleanConverter.java index 7d35d39b..1ff6171b 100644 --- a/src/main/java/com/alibaba/excel/converters/bigdecimal/BigDecimalBooleanConverter.java +++ b/src/main/java/com/alibaba/excel/converters/bigdecimal/BigDecimalBooleanConverter.java @@ -17,7 +17,7 @@ public class BigDecimalBooleanConverter implements Converter { @Override public Class supportJavaTypeKey() { - return Byte.class; + return BigDecimal.class; } @Override diff --git a/src/main/java/com/alibaba/excel/converters/shortconverter/ShortBooleanConverter.java b/src/main/java/com/alibaba/excel/converters/shortconverter/ShortBooleanConverter.java index 65d53162..40d75079 100644 --- a/src/main/java/com/alibaba/excel/converters/shortconverter/ShortBooleanConverter.java +++ b/src/main/java/com/alibaba/excel/converters/shortconverter/ShortBooleanConverter.java @@ -17,7 +17,7 @@ public class ShortBooleanConverter implements Converter { @Override public Class supportJavaTypeKey() { - return Long.class; + return Short.class; } @Override diff --git a/src/main/java/com/alibaba/excel/enums/CellDataTypeEnum.java b/src/main/java/com/alibaba/excel/enums/CellDataTypeEnum.java index 72ce70a1..b12b0bfa 100644 --- a/src/main/java/com/alibaba/excel/enums/CellDataTypeEnum.java +++ b/src/main/java/com/alibaba/excel/enums/CellDataTypeEnum.java @@ -11,11 +11,15 @@ import com.alibaba.excel.util.StringUtils; * @author zhuangjiaju */ public enum CellDataTypeEnum { - /** * string */ STRING, + /** + * This type of data does not need to be read in the 'sharedStrings.xml', it is only used for overuse, and the data + * will be stored as a {@link #STRING} + */ + DIRECT_STRING, /** * number */ @@ -36,6 +40,7 @@ public enum CellDataTypeEnum { private static final Map TYPE_ROUTING_MAP = new HashMap(16); static { TYPE_ROUTING_MAP.put("s", STRING); + TYPE_ROUTING_MAP.put("str", DIRECT_STRING); TYPE_ROUTING_MAP.put("inlineStr", STRING); TYPE_ROUTING_MAP.put("e", ERROR); TYPE_ROUTING_MAP.put("b", BOOLEAN); diff --git a/src/main/java/com/alibaba/excel/metadata/Head.java b/src/main/java/com/alibaba/excel/metadata/Head.java index 00ba2169..3e0f912f 100644 --- a/src/main/java/com/alibaba/excel/metadata/Head.java +++ b/src/main/java/com/alibaba/excel/metadata/Head.java @@ -27,20 +27,17 @@ public class Head { * Whether index is specified */ private Boolean forceIndex; + /** + * Whether to specify a name + */ + private Boolean forceName; /** * column with */ private ColumnWidthProperty columnWidthProperty; - public Head(Integer columnIndex, String fieldName, String headName) { - this.columnIndex = columnIndex; - this.fieldName = fieldName; - headNameList = new ArrayList(); - headNameList.add(headName); - this.forceIndex = Boolean.FALSE; - } - - public Head(Integer columnIndex, String fieldName, List headNameList, Boolean forceIndex) { + public Head(Integer columnIndex, String fieldName, List headNameList, Boolean forceIndex, + Boolean forceName) { this.columnIndex = columnIndex; this.fieldName = fieldName; if (headNameList == null) { @@ -48,6 +45,7 @@ public class Head { } this.headNameList = headNameList; this.forceIndex = forceIndex; + this.forceName = forceName; } public Integer getColumnIndex() { @@ -89,4 +87,12 @@ public class Head { public void setForceIndex(Boolean forceIndex) { this.forceIndex = forceIndex; } + + public Boolean getForceName() { + return forceName; + } + + public void setForceName(Boolean forceName) { + this.forceName = forceName; + } } 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 4b8fcf4a..e939150e 100644 --- a/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java +++ b/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java @@ -60,7 +60,7 @@ public class ExcelHeadProperty { headRowNumber = 0; if (head != null && !head.isEmpty()) { for (int i = 0; i < head.size(); i++) { - headMap.put(i, new Head(i, null, head.get(i), Boolean.FALSE)); + headMap.put(i, new Head(i, null, head.get(i), Boolean.FALSE, Boolean.TRUE)); contentPropertyMap.put(i, null); } headKind = HeadKindEnum.STRING; @@ -155,15 +155,18 @@ 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; tmpHeadList.add(field.getName()); } - Head head = new Head(index, field.getName(), tmpHeadList, forceIndex); + Head head = new Head(index, field.getName(), tmpHeadList, forceIndex, forceName); ExcelContentProperty excelContentProperty = new ExcelContentProperty(); if (excelProperty != null && excelProperty.converter() != null) { Class convertClazz = excelProperty.converter(); 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 8f147025..7285808f 100644 --- a/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java +++ b/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java @@ -26,28 +26,28 @@ import net.sf.cglib.beans.BeanMap; * * @author jipengfei */ -public class ModelBuildEventListener extends AbstractIgnoreExceptionReadListener> { +public class ModelBuildEventListener extends AbstractIgnoreExceptionReadListener> { @Override - public void invoke(List data, AnalysisContext context) { + public void invoke(Map cellDataMap, AnalysisContext context) { ReadHolder currentReadHolder = context.currentReadHolder(); if (HeadKindEnum.CLASS.equals(currentReadHolder.excelReadHeadProperty().getHeadKind())) { - context.readRowHolder().setCurrentRowAnalysisResult(buildUserModel(data, currentReadHolder)); + context.readRowHolder().setCurrentRowAnalysisResult(buildUserModel(cellDataMap, currentReadHolder)); return; } - context.readRowHolder().setCurrentRowAnalysisResult(buildStringList(data, currentReadHolder)); + context.readRowHolder().setCurrentRowAnalysisResult(buildStringList(cellDataMap, currentReadHolder)); } - private Object buildStringList(List data, ReadHolder currentReadHolder) { + private Object buildStringList(Map cellDataMap, ReadHolder currentReadHolder) { List list = new ArrayList(); - for (CellData cellData : data) { + for (CellData cellData : cellDataMap.values()) { list.add((String)convertValue(cellData, String.class, null, currentReadHolder.converterMap(), currentReadHolder.globalConfiguration())); } return list; } - private Object buildUserModel(List data, ReadHolder currentReadHolder) { + private Object buildUserModel(Map cellDataMap, ReadHolder currentReadHolder) { ExcelReadHeadProperty excelReadHeadProperty = currentReadHolder.excelReadHeadProperty(); Object resultModel; try { @@ -61,10 +61,10 @@ public class ModelBuildEventListener extends AbstractIgnoreExceptionReadListener Map contentPropertyMap = excelReadHeadProperty.getContentPropertyMap(); for (Map.Entry entry : headMap.entrySet()) { Integer index = entry.getKey(); - if (index >= data.size()) { + if (index >= cellDataMap.size()) { continue; } - CellData cellData = data.get(index); + CellData cellData = cellDataMap.get(index); if (cellData.getType() == CellDataTypeEnum.EMPTY) { continue; } @@ -81,6 +81,9 @@ public class ModelBuildEventListener extends AbstractIgnoreExceptionReadListener private Object convertValue(CellData cellData, Class clazz, ExcelContentProperty contentProperty, Map converterMap, GlobalConfiguration globalConfiguration) { + if (clazz == CellData.class) { + return cellData; + } Converter converter = null; if (contentProperty != null) { converter = contentProperty.getConverter(); diff --git a/src/main/java/com/alibaba/excel/read/listener/event/AnalysisFinishEvent.java b/src/main/java/com/alibaba/excel/read/listener/event/AnalysisFinishEvent.java index c73b3286..9749cf06 100644 --- a/src/main/java/com/alibaba/excel/read/listener/event/AnalysisFinishEvent.java +++ b/src/main/java/com/alibaba/excel/read/listener/event/AnalysisFinishEvent.java @@ -1,6 +1,6 @@ package com.alibaba.excel.read.listener.event; -import java.util.List; +import java.util.Map; import com.alibaba.excel.metadata.CellData; @@ -16,5 +16,5 @@ public interface AnalysisFinishEvent { * * @return */ - List getAnalysisResult(); + Map getAnalysisResult(); } diff --git a/src/main/java/com/alibaba/excel/read/listener/event/EachRowAnalysisFinishEvent.java b/src/main/java/com/alibaba/excel/read/listener/event/EachRowAnalysisFinishEvent.java index 0a5a945e..879a59a7 100644 --- a/src/main/java/com/alibaba/excel/read/listener/event/EachRowAnalysisFinishEvent.java +++ b/src/main/java/com/alibaba/excel/read/listener/event/EachRowAnalysisFinishEvent.java @@ -1,6 +1,6 @@ package com.alibaba.excel.read.listener.event; -import java.util.List; +import java.util.Map; import com.alibaba.excel.metadata.CellData; @@ -8,13 +8,14 @@ import com.alibaba.excel.metadata.CellData; * @author jipengfei */ public class EachRowAnalysisFinishEvent implements AnalysisFinishEvent { - private List result; + private Map result; - public EachRowAnalysisFinishEvent(List content) { + public EachRowAnalysisFinishEvent(Map content) { this.result = content; } + @Override - public List getAnalysisResult() { + public Map getAnalysisResult() { return result; } } 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 48e92bbc..b0391a62 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 @@ -107,9 +107,9 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH @Override public void notifyEndOneRow(AnalysisFinishEvent event, AnalysisContext analysisContext) { - List cellDataList = event.getAnalysisResult(); + Map cellDataMap = event.getAnalysisResult(); ReadRowHolder readRowHolder = analysisContext.readRowHolder(); - readRowHolder.setCurrentRowAnalysisResult(cellDataList); + readRowHolder.setCurrentRowAnalysisResult(cellDataMap); if (readRowHolder.getRowIndex() >= analysisContext.readSheetHolder().getHeadRowNumber()) { for (ReadListener readListener : analysisContext.currentReadHolder().readListenerList()) { @@ -129,7 +129,7 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH } // Now is header if (analysisContext.readSheetHolder().getHeadRowNumber().equals(readRowHolder.getRowIndex() + 1)) { - buildHead(analysisContext, cellDataList); + buildHead(analysisContext, cellDataMap); } } @@ -140,11 +140,11 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH } } - private void buildHead(AnalysisContext analysisContext, List cellDataList) { + private void buildHead(AnalysisContext analysisContext, Map cellDataMap) { if (!HeadKindEnum.CLASS.equals(analysisContext.currentReadHolder().excelReadHeadProperty().getHeadKind())) { return; } - List dataList = (List)buildStringList(cellDataList, analysisContext.currentReadHolder()); + List dataList = (List)buildStringList(cellDataMap, analysisContext.currentReadHolder()); ExcelReadHeadProperty excelHeadPropertyData = analysisContext.readSheetHolder().excelReadHeadProperty(); Map headMapData = excelHeadPropertyData.getHeadMap(); Map contentPropertyMapData = excelHeadPropertyData.getContentPropertyMap(); @@ -153,7 +153,7 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH new HashMap(contentPropertyMapData.size() * 4 / 3 + 1); for (Map.Entry entry : headMapData.entrySet()) { Head headData = entry.getValue(); - if (headData.getForceIndex()) { + if (headData.getForceIndex() || !headData.getForceName()) { tmpHeadMap.put(entry.getKey(), headData); tmpContentPropertyMap.put(entry.getKey(), contentPropertyMapData.get(entry.getKey())); continue; @@ -179,9 +179,9 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH excelHeadPropertyData.setContentPropertyMap(tmpContentPropertyMap); } - private Object buildStringList(List data, ReadHolder readHolder) { + private Object buildStringList(Map cellDataMa, ReadHolder readHolder) { List list = new ArrayList(); - for (CellData cellData : data) { + for (CellData cellData : cellDataMa.values()) { Converter converter = readHolder.converterMap().get(ConverterKeyBuild.buildKey(String.class, cellData.getType())); if (converter == null) { diff --git a/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java b/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java index ec9ba522..fa028e52 100644 --- a/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java +++ b/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java @@ -256,25 +256,7 @@ public class ExcelBuilderImpl implements ExcelBuilder { if (value instanceof String && currentWriteHolder.globalConfiguration().getAutoTrim()) { value = ((String)value).trim(); } - Converter converter = null; - if (excelContentProperty != null) { - converter = excelContentProperty.getConverter(); - } - if (converter == null) { - converter = currentWriteHolder.converterMap().get(ConverterKeyBuild.buildKey(clazz)); - } - if (converter == null) { - throw new ExcelDataConvertException( - "Can not find 'Converter' support class " + clazz.getSimpleName() + "."); - } - CellData cellData; - try { - cellData = - converter.convertToExcelData(value, excelContentProperty, currentWriteHolder.globalConfiguration()); - } catch (Exception e) { - throw new ExcelDataConvertException("Convert data:" + value + " error,at row:" + cell.getRow().getRowNum(), - e); - } + CellData cellData = convert(currentWriteHolder, clazz, cell, value, excelContentProperty); if (cellData == null || cellData.getType() == null) { throw new ExcelDataConvertException( "Convert data:" + value + " return null,at row:" + cell.getRow().getRowNum()); @@ -297,4 +279,31 @@ public class ExcelBuilderImpl implements ExcelBuilder { + "at row:" + cell.getRow().getRowNum()); } } + + private CellData convert(WriteHolder currentWriteHolder, Class clazz, Cell cell, Object value, + ExcelContentProperty excelContentProperty) { + if (value instanceof CellData) { + return (CellData)value; + } + Converter converter = null; + if (excelContentProperty != null) { + converter = excelContentProperty.getConverter(); + } + if (converter == null) { + converter = currentWriteHolder.converterMap().get(ConverterKeyBuild.buildKey(clazz)); + } + if (converter == null) { + throw new ExcelDataConvertException( + "Can not find 'Converter' support class " + clazz.getSimpleName() + "."); + } + CellData cellData; + try { + cellData = + converter.convertToExcelData(value, excelContentProperty, currentWriteHolder.globalConfiguration()); + } catch (Exception e) { + throw new ExcelDataConvertException("Convert data:" + value + " error,at row:" + cell.getRow().getRowNum(), + e); + } + return 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/ConverterData.java index 800ede05..0ecc3437 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterData.java @@ -4,6 +4,7 @@ import java.math.BigDecimal; import java.util.Date; import com.alibaba.excel.annotation.ExcelProperty; +import com.alibaba.excel.metadata.CellData; import lombok.Data; @@ -32,4 +33,6 @@ public class ConverterData { private Float floatData; @ExcelProperty("字符串") private String string; + @ExcelProperty("自定义") + private CellData cellData; } 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 9992e61e..d688c707 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 @@ -45,6 +45,7 @@ public class ConverterDataListener extends AnalysisEventListener Assert.assertEquals(data.getDoubleData(), 1.0, 0.0); Assert.assertEquals(data.getFloatData(), (float)1.0, 0.0); Assert.assertEquals(data.getString(), "测试"); + Assert.assertEquals(data.getCellData().getStringValue(), "自定义"); LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); } } 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 be13972e..d97901bc 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 @@ -5,12 +5,14 @@ import java.math.BigDecimal; 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.util.TestFileUtil; import com.alibaba.excel.EasyExcelFactory; +import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.util.DateUtils; /** @@ -21,8 +23,14 @@ import com.alibaba.excel.util.DateUtils; @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class ConverterDataTest { - private File file07 = TestFileUtil.createNewFile("converter07.xlsx"); - private File file03 = TestFileUtil.createNewFile("converter03.xls"); + private static File file07; + private static File file03; + + @BeforeClass + public static void init() { + file07 = TestFileUtil.createNewFile("converter07.xlsx"); + file03 = TestFileUtil.createNewFile("converter03.xls"); + } @Test public void T01ReadAndWrite07() throws Exception { @@ -39,6 +47,22 @@ public class ConverterDataTest { EasyExcelFactory.read(file, ConverterData.class, new ConverterDataListener()).sheet().doRead().finish(); } + @Test + public void T03ReadAllConverter07() { + readAllConverter("converter" + File.separator + "converter07.xlsx"); + } + + @Test + public void T03ReadAllConverter03() { + readAllConverter("converter" + File.separator + "converter03.xls"); + } + + private void readAllConverter(String fileName) { + EasyExcelFactory + .read(TestFileUtil.readFile(fileName), ReadAllConverterData.class, new ReadAllConverterDataListener()) + .sheet().doRead().finish(); + } + private List data() throws Exception { List list = new ArrayList(); ConverterData converterData = new ConverterData(); @@ -52,6 +76,7 @@ public class ConverterDataTest { converterData.setDoubleData(1.0); converterData.setFloatData((float)1.0); converterData.setString("测试"); + converterData.setCellData(new CellData("自定义")); list.add(converterData); return list; } diff --git a/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterData.java b/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterData.java new file mode 100644 index 00000000..19e4b283 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterData.java @@ -0,0 +1,45 @@ +package com.alibaba.easyexcel.test.core.converter; + +import java.math.BigDecimal; +import java.util.Date; + +import lombok.Data; + +/** + * @author zhuangjiaju + */ +@Data +public class ReadAllConverterData { + private BigDecimal bigDecimalBoolean; + private BigDecimal bigDecimalNumber; + private BigDecimal bigDecimalString; + private Boolean booleanBoolean; + private Boolean booleanNumber; + private Boolean booleanString; + private Byte byteBoolean; + private Byte byteNumber; + private Byte byteString; + private Date dateNumber; + private Date dateString; + private Double doubleBoolean; + private Double doubleNumber; + private Double doubleString; + private Float floatBoolean; + private Float floatNumber; + private Float floatString; + private Integer integerBoolean; + private Integer integerNumber; + private Integer integerString; + private Long longBoolean; + private Long longNumber; + private Long longString; + private Short shortBoolean; + private Short shortNumber; + private Short shortString; + private String StringBoolean; + private String StringNumber; + private String StringString; + private String StringError; + private String StringFormulaNumber; + private String StringFormulaString; +} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java b/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java new file mode 100644 index 00000000..63352467 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java @@ -0,0 +1,46 @@ +package com.alibaba.easyexcel.test.core.converter; + +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 ReadAllConverterDataListener extends AnalysisEventListener { + private static final Logger LOGGER = LoggerFactory.getLogger(ReadAllConverterDataListener.class); + List list = new ArrayList(); + + @Override + public void invoke(ReadAllConverterData data, AnalysisContext context) { + list.add(data); + } + + @Override + public void doAfterAllAnalysed(AnalysisContext context) { + Assert.assertEquals(list.size(), 1); + ReadAllConverterData 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.getBooleanData(), Boolean.TRUE); + // Assert.assertEquals(data.getBigDecimal().doubleValue(), BigDecimal.ONE.doubleValue(), 0.0); + // Assert.assertEquals((long)data.getLongData(), 1L); + // Assert.assertEquals((long)data.getIntegerData(), 1L); + // Assert.assertEquals((long)data.getShortData(), 1L); + // Assert.assertEquals((long)data.getByteData(), 1L); + // Assert.assertEquals(data.getDoubleData(), 1.0, 0.0); + // Assert.assertEquals(data.getFloatData(), (float)1.0, 0.0); + // Assert.assertEquals(data.getString(), "测试"); + LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); + } +} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadConverterData.java b/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadConverterData.java deleted file mode 100644 index 35f53e16..00000000 --- a/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadConverterData.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.alibaba.easyexcel.test.core.converter; - -import java.math.BigDecimal; -import java.util.Date; - -import com.alibaba.excel.annotation.ExcelProperty; - -import lombok.Data; - -/** - * @author zhuangjiaju - */ -@Data -public class ReadConverterData { - @ExcelProperty("日期") - private Date date; - @ExcelProperty("日期字符串") - private String dateString; - @ExcelProperty("布尔") - private Boolean booleanData; - @ExcelProperty("布尔字符串") - private String booleanString; - @ExcelProperty("大数") - private BigDecimal bigDecimal; - @ExcelProperty("大数字符串") - private String bigDecimalString; - @ExcelProperty("长整型") - private Long longData; - @ExcelProperty("长整型字符串") - private String longString; - @ExcelProperty("整型") - private Integer integerData; - @ExcelProperty("整型字符串") - private String integerString; - @ExcelProperty("短整型") - private Short shortData; - @ExcelProperty("短整型字符串") - private String shortString; - @ExcelProperty("字节型") - private Byte byteData; - @ExcelProperty("字节型字符串") - private String byteString; - @ExcelProperty("双精度浮点型") - private Double doulbleData; - @ExcelProperty("双精度浮点型字符串") - private String doulbleString; - @ExcelProperty("浮点型") - private Float FloatData; - @ExcelProperty("浮点型字符串") - private String FloatString; - @ExcelProperty("字符串") - private String string; -} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/nohead/NoHeadData07Test.java b/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadData07Test.java similarity index 95% rename from src/test/java/com/alibaba/easyexcel/test/core/nohead/NoHeadData07Test.java rename to src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadData07Test.java index 4d0af588..1eae01f9 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/nohead/NoHeadData07Test.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadData07Test.java @@ -1,4 +1,4 @@ -package com.alibaba.easyexcel.test.core.nohead; +package com.alibaba.easyexcel.test.core.head; import java.util.ArrayList; import java.util.List; @@ -9,7 +9,7 @@ import com.alibaba.easyexcel.test.core.order.OrderData; /** * Order data test - * + * * @author zhuangjiaju */ public class NoHeadData07Test { 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 7058204c..11704832 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 @@ -5,6 +5,7 @@ import java.util.ArrayList; import java.util.List; import org.junit.Assert; +import org.junit.BeforeClass; import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runners.MethodSorters; @@ -20,11 +21,17 @@ import com.alibaba.excel.EasyExcelFactory; @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class SimpleDataTest { - private File file07 = TestFileUtil.createNewFile("simple07.xlsx"); - private File file03 = TestFileUtil.createNewFile("simple03.xls"); + private static File file07; + private static File file03; + + @BeforeClass + public static void init() { + file07 = TestFileUtil.createNewFile("simple07.xlsx"); + file03 = TestFileUtil.createNewFile("simple03.xls"); + } @Test - public void T01ReadAndWrite() { + public void T01ReadAndWrite07() { readAndWrite(file07); } @@ -48,8 +55,6 @@ public class SimpleDataTest { synchronousRead(file03); } - - private void synchronousRead(File file) { // Synchronous read file List list = EasyExcelFactory.read(file).head(SimpleData.class).sheet().doReadSync(); diff --git a/src/test/java/com/alibaba/easyexcel/test/util/TestFileUtil.java b/src/test/java/com/alibaba/easyexcel/test/util/TestFileUtil.java index 2c14e543..e43bdde7 100644 --- a/src/test/java/com/alibaba/easyexcel/test/util/TestFileUtil.java +++ b/src/test/java/com/alibaba/easyexcel/test/util/TestFileUtil.java @@ -13,6 +13,10 @@ public class TestFileUtil { return TestFileUtil.class.getResource("/").getPath(); } + public static File readFile(String pathName) { + return new File(getPath() + pathName); + } + public static File createNewFile(String pathName) { File file = new File(getPath() + pathName); if (file.exists()) { @@ -25,7 +29,4 @@ public class TestFileUtil { return file; } - public static File createNewWriteFile(String pathName) { - return createNewFile("wirte/" + pathName); - } } diff --git a/src/test/resources/converter/converter03.xls b/src/test/resources/converter/converter03.xls new file mode 100644 index 00000000..6ae32b9a Binary files /dev/null and b/src/test/resources/converter/converter03.xls differ diff --git a/src/test/resources/converter/converter07.xlsx b/src/test/resources/converter/converter07.xlsx new file mode 100644 index 00000000..a210bbc2 Binary files /dev/null and b/src/test/resources/converter/converter07.xlsx differ