Browse Source

修复用String接收日期、数字和excel显示不一致的bug

2.1.x
Jiaju Zhuang 5 years ago
parent
commit
3555883871
  1. 4
      pom.xml
  2. 9
      src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java
  3. 6
      src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java
  4. 13
      src/main/java/com/alibaba/excel/analysis/v03/handlers/NumberRecordHandler.java
  5. 8
      src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java
  6. 193
      src/main/java/com/alibaba/excel/constant/BuiltinFormats.java
  7. 1
      src/main/java/com/alibaba/excel/context/AnalysisContext.java
  8. 9
      src/main/java/com/alibaba/excel/context/WriteContextImpl.java
  9. 2
      src/main/java/com/alibaba/excel/converters/byteconverter/ByteNumberConverter.java
  10. 2
      src/main/java/com/alibaba/excel/converters/floatconverter/FloatNumberConverter.java
  11. 2
      src/main/java/com/alibaba/excel/converters/integer/IntegerNumberConverter.java
  12. 2
      src/main/java/com/alibaba/excel/converters/shortconverter/ShortNumberConverter.java
  13. 12
      src/main/java/com/alibaba/excel/metadata/AbstractHolder.java
  14. 93
      src/main/java/com/alibaba/excel/metadata/AbstractParameterBuilder.java
  15. 4
      src/main/java/com/alibaba/excel/metadata/DataFormatter.java
  16. 47
      src/main/java/com/alibaba/excel/read/builder/AbstractExcelReaderParameterBuilder.java
  17. 103
      src/main/java/com/alibaba/excel/read/builder/ExcelReaderBuilder.java
  18. 101
      src/main/java/com/alibaba/excel/read/builder/ExcelReaderSheetBuilder.java
  19. 5
      src/main/java/com/alibaba/excel/util/DateUtils.java
  20. 5
      src/main/java/com/alibaba/excel/util/NumberDataFormatterUtils.java
  21. 14
      src/main/java/com/alibaba/excel/util/ThreadLocalCachedUtils.java
  22. 104
      src/main/java/com/alibaba/excel/write/builder/AbstractExcelWriterParameterBuilder.java
  23. 133
      src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java
  24. 136
      src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java
  25. 135
      src/main/java/com/alibaba/excel/write/builder/ExcelWriterTableBuilder.java
  26. 6
      src/test/java/com/alibaba/easyexcel/test/core/dataformat/DateFormatData.java
  27. 27
      src/test/java/com/alibaba/easyexcel/test/core/dataformat/DateFormatTest.java
  28. 2
      src/test/java/com/alibaba/easyexcel/test/temp/dataformat/DataFormatTest.java
  29. 12
      src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiWriteTest.java
  30. 15
      src/test/java/com/alibaba/easyexcel/test/temp/simple/JsonData.java
  31. 41
      src/test/java/com/alibaba/easyexcel/test/temp/simple/Wirte.java
  32. 13
      src/test/java/com/alibaba/easyexcel/test/temp/simple/WriteData.java
  33. BIN
      src/test/resources/dataformat/dataformat.xls
  34. BIN
      src/test/resources/dataformat/dataformat.xlsx
  35. 6
      update.md

4
pom.xml

@ -17,7 +17,7 @@
<properties>
<!--<gpg.skip>true</gpg.skip>-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdk.version>1.7</jdk.version>
<jdk.version>1.6</jdk.version>
</properties>
<scm>
@ -84,7 +84,7 @@
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.7.1</version>
<version>3.4.0</version>
</dependency>
<!--test-->
<dependency>

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

@ -22,7 +22,9 @@ import com.alibaba.excel.read.metadata.ReadWorkbook;
import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.util.CollectionUtils;
import com.alibaba.excel.util.DateUtils;
import com.alibaba.excel.util.FileUtils;
import com.alibaba.excel.util.NumberDataFormatterUtils;
import com.alibaba.excel.util.StringUtils;
/**
@ -175,11 +177,18 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
clearEncrypt03();
removeThreadLocalCache();
if (throwable != null) {
throw new ExcelAnalysisException("Can not close IO.", throwable);
}
}
private void removeThreadLocalCache() {
NumberDataFormatterUtils.removeThreadLocalCache();
DateUtils.removeThreadLocalCache();
}
private void clearEncrypt03() {
if (StringUtils.isEmpty(analysisContext.readWorkbookHolder().getPassword())
|| !ExcelTypeEnum.XLS.equals(analysisContext.readWorkbookHolder().getExcelType())) {

6
src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java

@ -3,10 +3,10 @@ package com.alibaba.excel.analysis.v03;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
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;
@ -191,7 +191,7 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor {
analysisContext.readRowHolder(
new ReadRowHolder(lastRowNumber, analysisContext.readSheetHolder().getGlobalConfiguration()));
analysisContext.readSheetHolder().notifyEndOneRow(new EachRowAnalysisFinishEvent(records), analysisContext);
records.clear();
records = new HashMap<Integer, CellData>();
lastColumnNumber = -1;
}
@ -208,7 +208,7 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor {
recordHandlers.add(new FormulaRecordHandler(stubWorkbook, formatListener));
recordHandlers.add(new LabelRecordHandler());
recordHandlers.add(new NoteRecordHandler());
recordHandlers.add(new NumberRecordHandler(formatListener));
recordHandlers.add(new NumberRecordHandler(analysisContext, formatListener));
recordHandlers.add(new RkRecordHandler());
recordHandlers.add(new SstRecordHandler());
recordHandlers.add(new MissingCellDummyRecordHandler());

13
src/main/java/com/alibaba/excel/analysis/v03/handlers/NumberRecordHandler.java

@ -8,6 +8,7 @@ import org.apache.poi.hssf.record.Record;
import com.alibaba.excel.analysis.v03.AbstractXlsRecordHandler;
import com.alibaba.excel.constant.BuiltinFormats;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.metadata.CellData;
/**
@ -18,7 +19,10 @@ import com.alibaba.excel.metadata.CellData;
public class NumberRecordHandler extends AbstractXlsRecordHandler {
private FormatTrackingHSSFListener formatListener;
public NumberRecordHandler(FormatTrackingHSSFListener formatListener) {
private AnalysisContext context;
public NumberRecordHandler(AnalysisContext context, FormatTrackingHSSFListener formatListener) {
this.context = context;
this.formatListener = formatListener;
}
@ -35,11 +39,8 @@ public class NumberRecordHandler extends AbstractXlsRecordHandler {
this.cellData = new CellData(BigDecimal.valueOf(numrec.getValue()));
int dataFormat = formatListener.getFormatIndex(numrec);
this.cellData.setDataFormat(dataFormat);
if (dataFormat <= BuiltinFormats.builtinFormats.length) {
this.cellData.setDataFormatString(BuiltinFormats.getBuiltinFormat(dataFormat));
} else {
this.cellData.setDataFormatString(formatListener.getFormatString(numrec));
}
this.cellData.setDataFormatString(BuiltinFormats.getBuiltinFormat(dataFormat,
formatListener.getFormatString(numrec), context.readSheetHolder().getGlobalConfiguration().getLocale()));
}
@Override

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

@ -88,11 +88,9 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
XSSFCellStyle xssfCellStyle = stylesTable.getStyleAt(dateFormatIndexInteger);
int dataFormat = xssfCellStyle.getDataFormat();
currentCellData.setDataFormat(dataFormat);
if (dataFormat <= BuiltinFormats.builtinFormats.length) {
currentCellData.setDataFormatString(BuiltinFormats.getBuiltinFormat(dataFormat));
} else {
currentCellData.setDataFormatString(xssfCellStyle.getDataFormatString());
}
currentCellData.setDataFormatString(
BuiltinFormats.getBuiltinFormat(dataFormat, xssfCellStyle.getDataFormatString(),
analysisContext.readSheetHolder().getGlobalConfiguration().getLocale()));
}
}
// cell is formula

193
src/main/java/com/alibaba/excel/constant/BuiltinFormats.java

@ -1,6 +1,6 @@
package com.alibaba.excel.constant;
import com.alibaba.excel.util.StringUtils;
import java.util.Locale;
/**
* Excel's built-in format conversion.Currently only supports Chinese.
@ -17,7 +17,179 @@ import com.alibaba.excel.util.StringUtils;
**/
public class BuiltinFormats {
public static String[] builtinFormats = {
private static final String[] BUILTIN_FORMATS_CN = {
// 0
"General",
// 1
"0",
// 2
"0.00",
// 3
"#,##0",
// 4
"#,##0.00",
// 5
"\"¥\"#,##0_);(\"¥\"#,##0)",
// 6
"\"¥\"#,##0_);[Red](\"¥\"#,##0)",
// 7
"\"¥\"#,##0.00_);(\"¥\"#,##0.00)",
// 8
"\"¥\"#,##0.00_);[Red](\"¥\"#,##0.00)",
// 9
"0%",
// 10
"0.00%",
// 11
"0.00E+00",
// 12
"# ?/?",
// 13
"# ??/??",
// 14
// The official documentation shows "m/d/yy", but the actual test is "yyyy/m/d".
"yyyy/m/d",
// 15
"d-mmm-yy",
// 16
"d-mmm",
// 17
"mmm-yy",
// 18
"h:mm AM/PM",
// 19
"h:mm:ss AM/PM",
// 20
"h:mm",
// 21
"h:mm:ss",
// 22
// The official documentation shows "m/d/yy h:mm", but the actual test is "yyyy/m/d h:mm".
"yyyy/m/d h:mm",
// 23-26 No specific correspondence found in the official documentation.
// 23
null,
// 24
null,
// 25
null,
// 26
null,
// 27
"yyyy\"年\"m\"月\"",
// 28
"m\"月\"d\"日\"",
// 29
"m\"月\"d\"日\"",
// 30
"m-d-yy",
// 31
"yyyy\"年\"m\"月\"d\"日\"",
// 32
"h\"时\"mm\"分\"",
// 33
"h\"时\"mm\"分\"ss\"秒\"",
// 34
"上午/下午h\"时\"mm\"分\"",
// 35
"上午/下午h\"时\"mm\"分\"ss\"秒\"",
// 36
"yyyy\"年\"m\"月\"",
// 37
"#,##0_);(#,##0)",
// 38
"#,##0_);[Red](#,##0)",
// 39
"#,##0.00_);(#,##0.00)",
// 40
"#,##0.00_);[Red](#,##0.00)",
// 41
"_(* #,##0_);_(* (#,##0);_(* \"-\"_);_(@_)",
// 42
"_(\"¥\"* #,##0_);_(\"¥\"* (#,##0);_(\"¥\"* \"-\"_);_(@_)",
// 43
"_(* #,##0.00_);_(* (#,##0.00);_(* \"-\"??_);_(@_)",
// 44
"_(\"¥\"* #,##0.00_);_(\"¥\"* (#,##0.00);_(\"¥\"* \"-\"??_);_(@_)",
// 45
"mm:ss",
// 46
"[h]:mm:ss",
// 47
"mm:ss.0",
// 48
"##0.0E+0",
// 49
"@",
// 50
"yyyy\"年\"m\"月\"",
// 51
"m\"月\"d\"日\"",
// 52
"yyyy\"年\"m\"月\"",
// 53
"m\"月\"d\"日\"",
// 54
"m\"月\"d\"日\"",
// 55
"上午/下午h\"时\"mm\"分\"",
// 56
"上午/下午h\"时\"mm\"分\"ss\"秒\"",
// 57
"yyyy\"年\"m\"月\"",
// 58
"m\"月\"d\"日\"",
// 59
"t0",
// 60
"t0.00",
// 61
"t#,##0",
// 62
"t#,##0.00",
// 63-66 No specific correspondence found in the official documentation.
// 63
null,
// 64
null,
// 65
null,
// 66
null,
// 67
"t0%",
// 68
"t0.00%",
// 69
"t# ?/?",
// 70
"t# ??/??",
// 71
"ว/ด/ปปปป",
// 72
"ว-ดดด-ปป",
// 73
"ว-ดดด",
// 74
"ดดด-ปป",
// 75
"ช:นน",
// 76
"ช:นน:ทท",
// 77
"ว/ด/ปปปป ช:นน",
// 78
"นน:ทท",
// 79
"[ช]:นน:ทท",
// 80
"นน:ทท.0",
// 81
"d/m/bb",
// end
};
private static final String[] BUILTIN_FORMATS_US = {
// 0
"General",
// 1
@ -189,18 +361,19 @@ public class BuiltinFormats {
// end
};
public static String getBuiltinFormat(Integer index) {
if (index == null || index < 0 || index >= builtinFormats.length) {
return null;
public static String getBuiltinFormat(Integer index, String defaultFormat, Locale locale) {
String[] builtinFormat = switchBuiltinFormats(locale);
if (index == null || index < 0 || index >= builtinFormat.length) {
return defaultFormat;
}
return builtinFormats[index];
return builtinFormat[index];
}
public static String getFormat(Integer index, String format) {
if (!StringUtils.isEmpty(format)) {
return format;
private static String[] switchBuiltinFormats(Locale locale) {
if (locale != null && Locale.US.getCountry().equals(locale.getCountry())) {
return BUILTIN_FORMATS_US;
}
return getBuiltinFormat(index);
return BUILTIN_FORMATS_CN;
}
}

1
src/main/java/com/alibaba/excel/context/AnalysisContext.java

@ -2,7 +2,6 @@ package com.alibaba.excel.context;
import java.io.InputStream;
import com.alibaba.excel.analysis.ExcelReadExecutor;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.read.metadata.ReadSheet;

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

@ -27,7 +27,9 @@ import com.alibaba.excel.exception.ExcelGenerateException;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.util.DateUtils;
import com.alibaba.excel.util.FileUtils;
import com.alibaba.excel.util.NumberDataFormatterUtils;
import com.alibaba.excel.util.StringUtils;
import com.alibaba.excel.util.WorkBookUtil;
import com.alibaba.excel.util.WriteHandlerUtils;
@ -327,6 +329,8 @@ public class WriteContextImpl implements WriteContext {
clearEncrypt03();
removeThreadLocalCache();
if (throwable != null) {
throw new ExcelGenerateException("Can not close IO.", throwable);
}
@ -336,6 +340,11 @@ public class WriteContextImpl implements WriteContext {
}
}
private void removeThreadLocalCache() {
NumberDataFormatterUtils.removeThreadLocalCache();
DateUtils.removeThreadLocalCache();
}
@Override
public Sheet getCurrentSheet() {
return writeSheetHolder.getSheet();

2
src/main/java/com/alibaba/excel/converters/byteconverter/ByteNumberConverter.java

@ -34,7 +34,7 @@ public class ByteNumberConverter implements Converter<Byte> {
@Override
public CellData convertToExcelData(Byte value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return new CellData(BigDecimal.valueOf(value));
return new CellData(new BigDecimal(Byte.toString(value)));
}
}

2
src/main/java/com/alibaba/excel/converters/floatconverter/FloatNumberConverter.java

@ -34,7 +34,7 @@ public class FloatNumberConverter implements Converter<Float> {
@Override
public CellData convertToExcelData(Float value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return new CellData(BigDecimal.valueOf(value));
return new CellData(new BigDecimal(Float.toString(value)));
}
}

2
src/main/java/com/alibaba/excel/converters/integer/IntegerNumberConverter.java

@ -34,7 +34,7 @@ public class IntegerNumberConverter implements Converter<Integer> {
@Override
public CellData convertToExcelData(Integer value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return new CellData(BigDecimal.valueOf(value));
return new CellData(new BigDecimal(Integer.toString(value)));
}
}

2
src/main/java/com/alibaba/excel/converters/shortconverter/ShortNumberConverter.java

@ -34,7 +34,7 @@ public class ShortNumberConverter implements Converter<Short> {
@Override
public CellData convertToExcelData(Short value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return new CellData(BigDecimal.valueOf(value));
return new CellData(new BigDecimal(Short.toString(value)));
}
}

12
src/main/java/com/alibaba/excel/metadata/AbstractHolder.java

@ -1,6 +1,7 @@
package com.alibaba.excel.metadata;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import com.alibaba.excel.converters.Converter;
@ -27,7 +28,6 @@ public abstract class AbstractHolder implements ConfigurationHolder {
* Some global variables
*/
private GlobalConfiguration globalConfiguration;
/**
* <p>
* Read key:
@ -58,6 +58,16 @@ public abstract class AbstractHolder implements ConfigurationHolder {
} else {
globalConfiguration.setAutoTrim(basicParameter.getAutoTrim());
}
if (basicParameter.getLocale() == null) {
if (prentAbstractHolder == null) {
globalConfiguration.setLocale(Locale.getDefault());
} else {
globalConfiguration.setLocale(prentAbstractHolder.getGlobalConfiguration().getLocale());
}
} else {
globalConfiguration.setLocale(basicParameter.getLocale());
}
}
public Boolean getNewInitialization() {

93
src/main/java/com/alibaba/excel/metadata/AbstractParameterBuilder.java

@ -0,0 +1,93 @@
package com.alibaba.excel.metadata;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import com.alibaba.excel.converters.Converter;
/**
* ExcelBuilder
*
* @author Jiaju Zhuang
*/
public abstract class AbstractParameterBuilder<T extends AbstractParameterBuilder, C extends BasicParameter> {
/**
* You can only choose one of the {@link #head(List)} and {@link #head(Class)}
*
* @param head
* @return
*/
public T head(List<List<String>> head) {
parameter().setHead(head);
return self();
}
/**
* You can only choose one of the {@link #head(List)} and {@link #head(Class)}
*
* @param clazz
* @return
*/
public T head(Class clazz) {
parameter().setClazz(clazz);
return self();
}
/**
* Custom type conversions override the default.
*
* @param converter
* @return
*/
public T registerConverter(Converter converter) {
if (parameter().getCustomConverterList() == null) {
parameter().setCustomConverterList(new ArrayList<Converter>());
}
parameter().getCustomConverterList().add(converter);
return self();
}
/**
* true if date uses 1904 windowing, or false if using 1900 date windowing.
*
* default is false
*
* @param use1904windowing
* @return
*/
public T use1904windowing(Boolean use1904windowing) {
parameter().setUse1904windowing(use1904windowing);
return self();
}
/**
* A <code>Locale</code> object represents a specific geographical, political, or cultural region. This parameter is
* used when formatting dates and numbers.
*
* @param locale
* @return
*/
public T locale(Locale locale) {
parameter().setLocale(locale);
return self();
}
/**
* Automatic trim includes sheet name and content
*
* @param autoTrim
* @return
*/
public T autoTrim(Boolean autoTrim) {
parameter().setAutoTrim(autoTrim);
return self();
}
@SuppressWarnings("unchecked")
protected T self() {
return (T)this;
}
protected abstract C parameter();
}

4
src/main/java/com/alibaba/excel/metadata/DataFormatter.java

@ -63,7 +63,8 @@ public class DataFormatter {
private static final Pattern daysAsText = Pattern.compile("([d]{3,})", Pattern.CASE_INSENSITIVE);
/** Pattern to find "AM/PM" marker */
private static final Pattern amPmPattern = Pattern.compile("(([AP])[M/P]*)|(([上下])[午/下]*)", Pattern.CASE_INSENSITIVE);
private static final Pattern amPmPattern =
Pattern.compile("(([AP])[M/P]*)|(([上下])[午/下]*)", Pattern.CASE_INSENSITIVE);
/** Pattern to find formats with condition ranges e.g. [>=100] */
private static final Pattern rangeConditionalPattern =
@ -152,7 +153,6 @@ public class DataFormatter {
public DataFormatter(Locale locale, Boolean use1904windowing) {
this.use1904windowing = use1904windowing != null ? use1904windowing : Boolean.FALSE;
this.locale = locale != null ? locale : Locale.getDefault();
this.locale = Locale.US;
this.dateSymbols = DateFormatSymbols.getInstance(this.locale);
this.decimalSymbols = DecimalFormatSymbols.getInstance(this.locale);
}

47
src/main/java/com/alibaba/excel/read/builder/AbstractExcelReaderParameterBuilder.java

@ -0,0 +1,47 @@
package com.alibaba.excel.read.builder;
import java.util.ArrayList;
import com.alibaba.excel.metadata.AbstractParameterBuilder;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.read.metadata.ReadBasicParameter;
/**
* Build ExcelBuilder
*
* @author Jiaju Zhuang
*/
public abstract class AbstractExcelReaderParameterBuilder<T extends AbstractExcelReaderParameterBuilder,
C extends ReadBasicParameter> extends AbstractParameterBuilder<T, C> {
/**
* Count the number of added heads when read sheet.
*
* <p>
* 0 - This Sheet has no head ,since the first row are the data
* <p>
* 1 - This Sheet has one row head , this is the default
* <p>
* 2 - This Sheet has two row head ,since the third row is the data
*
* @param headRowNumber
* @return
*/
public T headRowNumber(Integer headRowNumber) {
parameter().setHeadRowNumber(headRowNumber);
return self();
}
/**
* Custom type listener run after default
*
* @param readListener
* @return
*/
public T registerReadListener(ReadListener readListener) {
if (parameter().getCustomReadListenerList() == null) {
parameter().setCustomReadListenerList(new ArrayList<ReadListener>());
}
parameter().getCustomReadListenerList().add(readListener);
return self();
}
}

103
src/main/java/com/alibaba/excel/read/builder/ExcelReaderBuilder.java

@ -2,8 +2,6 @@ package com.alibaba.excel.read.builder;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.SAXParserFactory;
@ -11,9 +9,7 @@ import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.cache.ReadCache;
import com.alibaba.excel.cache.selector.ReadCacheSelector;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.read.metadata.ReadWorkbook;
import com.alibaba.excel.support.ExcelTypeEnum;
@ -22,7 +18,7 @@ import com.alibaba.excel.support.ExcelTypeEnum;
*
* @author Jiaju Zhuang
*/
public class ExcelReaderBuilder {
public class ExcelReaderBuilder extends AbstractExcelReaderParameterBuilder<ExcelReaderBuilder, ReadWorkbook> {
/**
* Workbook
*/
@ -132,98 +128,6 @@ public class ExcelReaderBuilder {
return this;
}
/**
* Count the number of added heads when read sheet.
*
* <p>
* 0 - This Sheet has no head ,since the first row are the data
* <p>
* 1 - This Sheet has one row head , this is the default
* <p>
* 2 - This Sheet has two row head ,since the third row is the data
*
* @param headRowNumber
* @return
*/
public ExcelReaderBuilder headRowNumber(Integer headRowNumber) {
readWorkbook.setHeadRowNumber(headRowNumber);
return this;
}
/**
* You can only choose one of the {@link ExcelReaderBuilder#head(List)} and {@link ExcelReaderBuilder#head(Class)}
*
* @param head
* @return
*/
public ExcelReaderBuilder head(List<List<String>> head) {
readWorkbook.setHead(head);
return this;
}
/**
* You can only choose one of the {@link ExcelReaderBuilder#head(List)} and {@link ExcelReaderBuilder#head(Class)}
*
* @param clazz
* @return
*/
public ExcelReaderBuilder head(Class clazz) {
readWorkbook.setClazz(clazz);
return this;
}
/**
* Custom type conversions override the default.
*
* @param converter
* @return
*/
public ExcelReaderBuilder registerConverter(Converter converter) {
if (readWorkbook.getCustomConverterList() == null) {
readWorkbook.setCustomConverterList(new ArrayList<Converter>());
}
readWorkbook.getCustomConverterList().add(converter);
return this;
}
/**
* Custom type listener run after default
*
* @param readListener
* @return
*/
public ExcelReaderBuilder registerReadListener(ReadListener readListener) {
if (readWorkbook.getCustomReadListenerList() == null) {
readWorkbook.setCustomReadListenerList(new ArrayList<ReadListener>());
}
readWorkbook.getCustomReadListenerList().add(readListener);
return this;
}
/**
* true if date uses 1904 windowing, or false if using 1900 date windowing.
*
* default is false
*
* @param use1904windowing
* @return
*/
public ExcelReaderBuilder use1904windowing(Boolean use1904windowing) {
readWorkbook.setUse1904windowing(use1904windowing);
return this;
}
/**
* Automatic trim includes sheet name and content
*
* @param autoTrim
* @return
*/
public ExcelReaderBuilder autoTrim(Boolean autoTrim) {
readWorkbook.setAutoTrim(autoTrim);
return this;
}
/**
* Whether the encryption
*
@ -285,4 +189,9 @@ public class ExcelReaderBuilder {
}
return excelReaderSheetBuilder;
}
@Override
protected ReadWorkbook parameter() {
return readWorkbook;
}
}

101
src/main/java/com/alibaba/excel/read/builder/ExcelReaderSheetBuilder.java

@ -1,14 +1,11 @@
package com.alibaba.excel.read.builder;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.event.SyncReadListener;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.exception.ExcelGenerateException;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.read.metadata.ReadSheet;
/**
@ -16,7 +13,7 @@ import com.alibaba.excel.read.metadata.ReadSheet;
*
* @author Jiaju Zhuang
*/
public class ExcelReaderSheetBuilder {
public class ExcelReaderSheetBuilder extends AbstractExcelReaderParameterBuilder<ExcelReaderSheetBuilder, ReadSheet> {
private ExcelReader excelReader;
/**
* Sheet
@ -54,98 +51,6 @@ public class ExcelReaderSheetBuilder {
return this;
}
/**
* Count the number of added heads when read sheet.
*
* <p>
* 0 - This Sheet has no head ,since the first row are the data
* <p>
* 1 - This Sheet has one row head , this is the default
* <p>
* 2 - This Sheet has two row head ,since the third row is the data
*
* @param headRowNumber
* @return
*/
public ExcelReaderSheetBuilder headRowNumber(Integer headRowNumber) {
readSheet.setHeadRowNumber(headRowNumber);
return this;
}
/**
* You can only choose one of the {@link ExcelReaderBuilder#head(List)} and {@link ExcelReaderBuilder#head(Class)}
*
* @param head
* @return
*/
public ExcelReaderSheetBuilder head(List<List<String>> head) {
readSheet.setHead(head);
return this;
}
/**
* You can only choose one of the {@link ExcelReaderBuilder#head(List)} and {@link ExcelReaderBuilder#head(Class)}
*
* @param clazz
* @return
*/
public ExcelReaderSheetBuilder head(Class clazz) {
readSheet.setClazz(clazz);
return this;
}
/**
* Custom type conversions override the default.
*
* @param converter
* @return
*/
public ExcelReaderSheetBuilder registerConverter(Converter converter) {
if (readSheet.getCustomConverterList() == null) {
readSheet.setCustomConverterList(new ArrayList<Converter>());
}
readSheet.getCustomConverterList().add(converter);
return this;
}
/**
* Custom type listener run after default
*
* @param readListener
* @return
*/
public ExcelReaderSheetBuilder registerReadListener(ReadListener readListener) {
if (readSheet.getCustomReadListenerList() == null) {
readSheet.setCustomReadListenerList(new ArrayList<ReadListener>());
}
readSheet.getCustomReadListenerList().add(readListener);
return this;
}
/**
* true if date uses 1904 windowing, or false if using 1900 date windowing.
*
* default is false
*
* @param use1904windowing
* @return
*/
public ExcelReaderSheetBuilder use1904windowing(Boolean use1904windowing) {
readSheet.setUse1904windowing(use1904windowing);
return this;
}
/**
* Automatic trim includes sheet name and content
*
* @param autoTrim
* @return
*/
public ExcelReaderSheetBuilder autoTrim(Boolean autoTrim) {
readSheet.setAutoTrim(autoTrim);
return this;
}
public ReadSheet build() {
return readSheet;
}
@ -177,4 +82,8 @@ public class ExcelReaderSheetBuilder {
return (List<T>)syncReadListener.getList();
}
@Override
protected ReadSheet parameter() {
return readSheet;
}
}

5
src/main/java/com/alibaba/excel/util/DateUtils.java

@ -13,7 +13,7 @@ import java.util.regex.Pattern;
*
* @author Jiaju Zhuang
**/
public class DateUtils implements ThreadLocalCachedUtils {
public class DateUtils {
/**
* Is a cache of dates
*/
@ -302,8 +302,7 @@ public class DateUtils implements ThreadLocalCachedUtils {
return false;
}
@Override
public void removeThreadLocalCache() {
public static void removeThreadLocalCache() {
DATE_THREAD_LOCAL.remove();
DATE_FORMAT_THREAD_LOCAL.remove();
}

5
src/main/java/com/alibaba/excel/util/NumberDataFormatterUtils.java

@ -8,7 +8,7 @@ import com.alibaba.excel.metadata.GlobalConfiguration;
*
* @author Jiaju Zhuang
**/
public class NumberDataFormatterUtils implements ThreadLocalCachedUtils {
public class NumberDataFormatterUtils {
/**
* Cache DataFormatter.
*/
@ -40,8 +40,7 @@ public class NumberDataFormatterUtils implements ThreadLocalCachedUtils {
}
@Override
public void removeThreadLocalCache() {
public static void removeThreadLocalCache() {
DATA_FORMATTER_THREAD_LOCAL.remove();
}
}

14
src/main/java/com/alibaba/excel/util/ThreadLocalCachedUtils.java

@ -1,14 +0,0 @@
package com.alibaba.excel.util;
/**
* Thread local cache in the current tool class.
*
* @author Jiaju Zhuang
**/
public interface ThreadLocalCachedUtils {
/**
* Remove remove thread local cached.
*/
void removeThreadLocalCache();
}

104
src/main/java/com/alibaba/excel/write/builder/AbstractExcelWriterParameterBuilder.java

@ -0,0 +1,104 @@
package com.alibaba.excel.write.builder;
import java.util.ArrayList;
import java.util.Collection;
import com.alibaba.excel.metadata.AbstractParameterBuilder;
import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.metadata.WriteBasicParameter;
/**
* Build ExcelBuilder
*
* @author Jiaju Zhuang
*/
public abstract class AbstractExcelWriterParameterBuilder<T extends AbstractExcelWriterParameterBuilder,
C extends WriteBasicParameter> extends AbstractParameterBuilder<T, C> {
/**
* Writes the head relative to the existing contents of the sheet. Indexes are zero-based.
*
* @param relativeHeadRowIndex
* @return
*/
public T relativeHeadRowIndex(Integer relativeHeadRowIndex) {
parameter().setRelativeHeadRowIndex(relativeHeadRowIndex);
return self();
}
/**
* Need Head
*/
public T needHead(Boolean needHead) {
parameter().setNeedHead(needHead);
return self();
}
/**
* Custom write handler
*
* @param writeHandler
* @return
*/
public T registerWriteHandler(WriteHandler writeHandler) {
if (parameter().getCustomWriteHandlerList() == null) {
parameter().setCustomWriteHandlerList(new ArrayList<WriteHandler>());
}
parameter().getCustomWriteHandlerList().add(writeHandler);
return self();
}
/**
* Use the default style.Default is true.
*
* @param useDefaultStyle
* @return
*/
public T useDefaultStyle(Boolean useDefaultStyle) {
parameter().setUseDefaultStyle(useDefaultStyle);
return self();
}
/**
* Whether to automatically merge headers.Default is true.
*
* @param automaticMergeHead
* @return
*/
public T automaticMergeHead(Boolean automaticMergeHead) {
parameter().setAutomaticMergeHead(automaticMergeHead);
return self();
}
/**
* Ignore the custom columns.
*/
public T excludeColumnIndexes(Collection<Integer> excludeColumnIndexes) {
parameter().setExcludeColumnIndexes(excludeColumnIndexes);
return self();
}
/**
* Ignore the custom columns.
*/
public T excludeColumnFiledNames(Collection<String> excludeColumnFiledNames) {
parameter().setExcludeColumnFiledNames(excludeColumnFiledNames);
return self();
}
/**
* Only output the custom columns.
*/
public T includeColumnIndexes(Collection<Integer> includeColumnIndexes) {
parameter().setIncludeColumnIndexes(includeColumnIndexes);
return self();
}
/**
* Only output the custom columns.
*/
public T includeColumnFiledNames(Collection<String> includeColumnFiledNames) {
parameter().setIncludeColumnFiledNames(includeColumnFiledNames);
return self();
}
}

133
src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java

@ -3,12 +3,8 @@ package com.alibaba.excel.write.builder;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.metadata.WriteWorkbook;
@ -18,7 +14,7 @@ import com.alibaba.excel.write.metadata.WriteWorkbook;
*
* @author Jiaju Zhuang
*/
public class ExcelWriterBuilder {
public class ExcelWriterBuilder extends AbstractExcelWriterParameterBuilder<ExcelWriterBuilder, WriteWorkbook> {
/**
* Workbook
*/
@ -28,47 +24,6 @@ public class ExcelWriterBuilder {
this.writeWorkbook = new WriteWorkbook();
}
/**
* Writes the head relative to the existing contents of the sheet. Indexes are zero-based.
*
* @param relativeHeadRowIndex
* @return
*/
public ExcelWriterBuilder relativeHeadRowIndex(Integer relativeHeadRowIndex) {
writeWorkbook.setRelativeHeadRowIndex(relativeHeadRowIndex);
return this;
}
/**
* You can only choose one of the {@link ExcelWriterBuilder#head(List)} and {@link ExcelWriterBuilder#head(Class)}
*
* @param head
* @return
*/
public ExcelWriterBuilder head(List<List<String>> head) {
writeWorkbook.setHead(head);
return this;
}
/**
* You can only choose one of the {@link ExcelWriterBuilder#head(List)} and {@link ExcelWriterBuilder#head(Class)}
*
* @param clazz
* @return
*/
public ExcelWriterBuilder head(Class clazz) {
writeWorkbook.setClazz(clazz);
return this;
}
/**
* Need Head
*/
public ExcelWriterBuilder needHead(Boolean needHead) {
writeWorkbook.setNeedHead(needHead);
return this;
}
/**
* Default true
*
@ -80,28 +35,6 @@ public class ExcelWriterBuilder {
return this;
}
/**
* Use the default style.Default is true.
*
* @param useDefaultStyle
* @return
*/
public ExcelWriterBuilder useDefaultStyle(Boolean useDefaultStyle) {
writeWorkbook.setUseDefaultStyle(useDefaultStyle);
return this;
}
/**
* Whether to automatically merge headers.Default is true.
*
* @param automaticMergeHead
* @return
*/
public ExcelWriterBuilder automaticMergeHead(Boolean automaticMergeHead) {
writeWorkbook.setAutomaticMergeHead(automaticMergeHead);
return this;
}
/**
* Whether the encryption.
* <p>
@ -125,38 +58,6 @@ public class ExcelWriterBuilder {
return this;
}
/**
* Ignore the custom columns.
*/
public ExcelWriterBuilder excludeColumnIndexes(Collection<Integer> excludeColumnIndexes) {
writeWorkbook.setExcludeColumnIndexes(excludeColumnIndexes);
return this;
}
/**
* Ignore the custom columns.
*/
public ExcelWriterBuilder excludeColumnFiledNames(Collection<String> excludeColumnFiledNames) {
writeWorkbook.setExcludeColumnFiledNames(excludeColumnFiledNames);
return this;
}
/**
* Only output the custom columns.
*/
public ExcelWriterBuilder includeColumnIndexes(Collection<Integer> includeColumnIndexes) {
writeWorkbook.setIncludeColumnIndexes(includeColumnIndexes);
return this;
}
/**
* Only output the custom columns.
*/
public ExcelWriterBuilder includeColumnFiledNames(Collection<String> includeColumnFiledNames) {
writeWorkbook.setIncludeColumnFiledNames(includeColumnFiledNames);
return this;
}
/**
* Excel is also written in the event of an exception being thrown.The default false.
*/
@ -181,34 +82,6 @@ public class ExcelWriterBuilder {
return this;
}
/**
* Custom type conversions override the default.
*
* @param converter
* @return
*/
public ExcelWriterBuilder registerConverter(Converter converter) {
if (writeWorkbook.getCustomConverterList() == null) {
writeWorkbook.setCustomConverterList(new ArrayList<Converter>());
}
writeWorkbook.getCustomConverterList().add(converter);
return this;
}
/**
* Custom write handler
*
* @param writeHandler
* @return
*/
public ExcelWriterBuilder registerWriteHandler(WriteHandler writeHandler) {
if (writeWorkbook.getCustomWriteHandlerList() == null) {
writeWorkbook.setCustomWriteHandlerList(new ArrayList<WriteHandler>());
}
writeWorkbook.getCustomWriteHandlerList().add(writeHandler);
return this;
}
public ExcelWriterBuilder excelType(ExcelTypeEnum excelType) {
writeWorkbook.setExcelType(excelType);
return this;
@ -281,4 +154,8 @@ public class ExcelWriterBuilder {
return excelWriterSheetBuilder;
}
@Override
protected WriteWorkbook parameter() {
return writeWorkbook;
}
}

136
src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java

@ -1,13 +1,9 @@
package com.alibaba.excel.write.builder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.exception.ExcelGenerateException;
import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
@ -16,7 +12,7 @@ import com.alibaba.excel.write.metadata.fill.FillConfig;
*
* @author Jiaju Zhuang
*/
public class ExcelWriterSheetBuilder {
public class ExcelWriterSheetBuilder extends AbstractExcelWriterParameterBuilder<ExcelWriterSheetBuilder, WriteSheet> {
private ExcelWriter excelWriter;
/**
* Sheet
@ -32,99 +28,6 @@ public class ExcelWriterSheetBuilder {
this.excelWriter = excelWriter;
}
/**
* Writes the head relative to the existing contents of the sheet. Indexes are zero-based.
*
* @param relativeHeadRowIndex
* @return
*/
public ExcelWriterSheetBuilder relativeHeadRowIndex(Integer relativeHeadRowIndex) {
writeSheet.setRelativeHeadRowIndex(relativeHeadRowIndex);
return this;
}
/**
* You can only choose one of the {@link ExcelWriterSheetBuilder#head(List)} and
* {@link ExcelWriterSheetBuilder#head(Class)}
*
* @param head
* @return
*/
public ExcelWriterSheetBuilder head(List<List<String>> head) {
writeSheet.setHead(head);
return this;
}
/**
* You can only choose one of the {@link ExcelWriterSheetBuilder#head(List)} and
* {@link ExcelWriterSheetBuilder#head(Class)}
*
* @param clazz
* @return
*/
public ExcelWriterSheetBuilder head(Class clazz) {
writeSheet.setClazz(clazz);
return this;
}
/**
* Need Head
*/
public ExcelWriterSheetBuilder needHead(Boolean needHead) {
writeSheet.setNeedHead(needHead);
return this;
}
/**
* Use the default style.Default is true.
*
* @param useDefaultStyle
* @return
*/
public ExcelWriterSheetBuilder useDefaultStyle(Boolean useDefaultStyle) {
writeSheet.setUseDefaultStyle(useDefaultStyle);
return this;
}
/**
* Whether to automatically merge headers.Default is true.
*
* @param automaticMergeHead
* @return
*/
public ExcelWriterSheetBuilder automaticMergeHead(Boolean automaticMergeHead) {
writeSheet.setAutomaticMergeHead(automaticMergeHead);
return this;
}
/**
* Custom type conversions override the default.
*
* @param converter
* @return
*/
public ExcelWriterSheetBuilder registerConverter(Converter converter) {
if (writeSheet.getCustomConverterList() == null) {
writeSheet.setCustomConverterList(new ArrayList<Converter>());
}
writeSheet.getCustomConverterList().add(converter);
return this;
}
/**
* Custom write handler
*
* @param writeHandler
* @return
*/
public ExcelWriterSheetBuilder registerWriteHandler(WriteHandler writeHandler) {
if (writeSheet.getCustomWriteHandlerList() == null) {
writeSheet.setCustomWriteHandlerList(new ArrayList<WriteHandler>());
}
writeSheet.getCustomWriteHandlerList().add(writeHandler);
return this;
}
/**
* Starting from 0
*
@ -147,38 +50,6 @@ public class ExcelWriterSheetBuilder {
return this;
}
/**
* Ignore the custom columns.
*/
public ExcelWriterSheetBuilder excludeColumnIndexes(Collection<Integer> excludeColumnIndexes) {
writeSheet.setExcludeColumnIndexes(excludeColumnIndexes);
return this;
}
/**
* Ignore the custom columns.
*/
public ExcelWriterSheetBuilder excludeColumnFiledNames(Collection<String> excludeColumnFiledNames) {
writeSheet.setExcludeColumnFiledNames(excludeColumnFiledNames);
return this;
}
/**
* Only output the custom columns.
*/
public ExcelWriterSheetBuilder includeColumnIndexes(Collection<Integer> includeColumnIndexes) {
writeSheet.setIncludeColumnIndexes(includeColumnIndexes);
return this;
}
/**
* Only output the custom columns.
*/
public ExcelWriterSheetBuilder includeColumnFiledNames(Collection<String> includeColumnFiledNames) {
writeSheet.setIncludeColumnFiledNames(includeColumnFiledNames);
return this;
}
public WriteSheet build() {
return writeSheet;
}
@ -215,4 +86,9 @@ public class ExcelWriterSheetBuilder {
return excelWriterTableBuilder;
}
@Override
protected WriteSheet parameter() {
return writeSheet;
}
}

135
src/main/java/com/alibaba/excel/write/builder/ExcelWriterTableBuilder.java

@ -1,13 +1,9 @@
package com.alibaba.excel.write.builder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.exception.ExcelGenerateException;
import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;
@ -16,7 +12,7 @@ import com.alibaba.excel.write.metadata.WriteTable;
*
* @author Jiaju Zhuang
*/
public class ExcelWriterTableBuilder {
public class ExcelWriterTableBuilder extends AbstractExcelWriterParameterBuilder<ExcelWriterTableBuilder, WriteTable> {
private ExcelWriter excelWriter;
@ -36,99 +32,6 @@ public class ExcelWriterTableBuilder {
this.writeTable = new WriteTable();
}
/**
* Writes the head relative to the existing contents of the sheet. Indexes are zero-based.
*
* @param relativeHeadRowIndex
* @return
*/
public ExcelWriterTableBuilder relativeHeadRowIndex(Integer relativeHeadRowIndex) {
writeTable.setRelativeHeadRowIndex(relativeHeadRowIndex);
return this;
}
/**
* You can only choose one of the {@link ExcelWriterTableBuilder#head(List)} and
* {@link ExcelWriterTableBuilder#head(Class)}
*
* @param head
* @return
*/
public ExcelWriterTableBuilder head(List<List<String>> head) {
writeTable.setHead(head);
return this;
}
/**
* You can only choose one of the {@link ExcelWriterTableBuilder#head(List)} and
* {@link ExcelWriterTableBuilder#head(Class)}
*
* @param clazz
* @return
*/
public ExcelWriterTableBuilder head(Class clazz) {
writeTable.setClazz(clazz);
return this;
}
/**
* Need Head
*/
public ExcelWriterTableBuilder needHead(Boolean needHead) {
writeTable.setNeedHead(needHead);
return this;
}
/**
* Use the default style.Default is true.
*
* @param useDefaultStyle
* @return
*/
public ExcelWriterTableBuilder useDefaultStyle(Boolean useDefaultStyle) {
writeTable.setUseDefaultStyle(useDefaultStyle);
return this;
}
/**
* Whether to automatically merge headers.Default is true.
*
* @param automaticMergeHead
* @return
*/
public ExcelWriterTableBuilder automaticMergeHead(Boolean automaticMergeHead) {
writeTable.setAutomaticMergeHead(automaticMergeHead);
return this;
}
/**
* Custom type conversions override the default.
*
* @param converter
* @return
*/
public ExcelWriterTableBuilder registerConverter(Converter converter) {
if (writeTable.getCustomConverterList() == null) {
writeTable.setCustomConverterList(new ArrayList<Converter>());
}
writeTable.getCustomConverterList().add(converter);
return this;
}
/**
* Custom write handler
*
* @param writeHandler
* @return
*/
public ExcelWriterTableBuilder registerWriteHandler(WriteHandler writeHandler) {
if (writeTable.getCustomWriteHandlerList() == null) {
writeTable.setCustomWriteHandlerList(new ArrayList<WriteHandler>());
}
writeTable.getCustomWriteHandlerList().add(writeHandler);
return this;
}
/**
* Starting from 0
*
@ -140,38 +43,6 @@ public class ExcelWriterTableBuilder {
return this;
}
/**
* Ignore the custom columns.
*/
public ExcelWriterTableBuilder excludeColumnIndexes(Collection<Integer> excludeColumnIndexes) {
writeTable.setExcludeColumnIndexes(excludeColumnIndexes);
return this;
}
/**
* Ignore the custom columns.
*/
public ExcelWriterTableBuilder excludeColumnFiledNames(Collection<String> excludeColumnFiledNames) {
writeTable.setExcludeColumnFiledNames(excludeColumnFiledNames);
return this;
}
/**
* Only output the custom columns.
*/
public ExcelWriterTableBuilder includeColumnIndexes(Collection<Integer> includeColumnIndexes) {
writeTable.setIncludeColumnIndexes(includeColumnIndexes);
return this;
}
/**
* Only output the custom columns.
*/
public ExcelWriterTableBuilder includeColumnFiledNames(Collection<String> includeColumnFiledNames) {
writeSheet.setIncludeColumnFiledNames(includeColumnFiledNames);
return this;
}
public WriteTable build() {
return writeTable;
}
@ -184,4 +55,8 @@ public class ExcelWriterTableBuilder {
excelWriter.finish();
}
@Override
protected WriteTable parameter() {
return writeTable;
}
}

6
src/test/java/com/alibaba/easyexcel/test/core/dataformat/DateFormatData.java

@ -8,7 +8,9 @@ import lombok.Data;
@Data
public class DateFormatData {
private String date;
private String dateString;
private String dateStringCn;
private String dateStringUs;
private String number;
private String numberString;
private String numberStringCn;
private String numberStringUs;
}

27
src/test/java/com/alibaba/easyexcel/test/core/dataformat/DateFormatTest.java

@ -2,7 +2,9 @@ package com.alibaba.easyexcel.test.core.dataformat;
import java.io.File;
import java.util.List;
import java.util.Locale;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
@ -10,7 +12,6 @@ import org.slf4j.LoggerFactory;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
/**
*
@ -30,21 +31,31 @@ public class DateFormatTest {
@Test
public void t01Read07() {
read(file07);
readCn(file07);
readUs(file07);
}
@Test
public void t02Read03() {
read(file03);
readCn(file03);
readUs(file03);
}
private void read(File file) {
List<DateFormatData> list = EasyExcel.read(file, DateFormatData.class, null).sheet().doReadSync();
private void readCn(File file) {
List<DateFormatData> list =
EasyExcel.read(file, DateFormatData.class, null).locale(Locale.CHINA).sheet().doReadSync();
for (DateFormatData data : list) {
if (!data.getDate().equals(data.getDateString())) {
LOGGER.info("返回:{}", JSON.toJSONString(data));
}
Assert.assertEquals(data.getDate(), data.getDateStringCn());
Assert.assertEquals(data.getNumber(), data.getNumberStringCn());
}
}
private void readUs(File file) {
List<DateFormatData> list =
EasyExcel.read(file, DateFormatData.class, null).locale(Locale.US).sheet().doReadSync();
for (DateFormatData data : list) {
Assert.assertEquals(data.getDate(), data.getDateStringUs());
Assert.assertEquals(data.getNumber(), data.getNumberStringUs());
}
}
}

2
src/test/java/com/alibaba/easyexcel/test/temp/dataformat/DataFormatTest.java

@ -146,7 +146,7 @@ public class DataFormatTest {
@Test
public void test3556() throws IOException, InvalidFormatException {
String file = "D://test/dataformat.xlsx";
String file = "D://test/dataformat1.xlsx";
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(file);
Sheet xssfSheet = xssfWorkbook.getSheetAt(0);
DataFormatter d = new DataFormatter(Locale.CHINA);

12
src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiWriteTest.java

@ -2,6 +2,7 @@ package com.alibaba.easyexcel.test.temp.poi;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.regex.Pattern;
import org.apache.poi.xssf.streaming.SXSSFCell;
@ -35,9 +36,20 @@ public class PoiWriteTest {
cell1.setCellValue(999999999999999L);
SXSSFCell cell2 = row.createCell(1);
cell2.setCellValue(1000000000000001L);
SXSSFCell cell32 = row.createCell(2);
cell32.setCellValue(300.35f);
sxxsFWorkbook.write(fileOutputStream);
}
@Test
public void write01() throws IOException {
float ff = 300.35f;
BigDecimal bd = new BigDecimal(Float.toString(ff));
System.out.println(bd.doubleValue());
System.out.println(bd.floatValue());
}
@Test
public void write() throws IOException {
FileOutputStream fileOutputStream =

15
src/test/java/com/alibaba/easyexcel/test/temp/simple/JsonData.java

@ -0,0 +1,15 @@
package com.alibaba.easyexcel.test.temp.simple;
import lombok.Data;
/**
* TODO
*
* @author Jiaju Zhuang
**/
@Data
public class JsonData {
private String SS1;
private String sS2;
private String ss3;
}

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

@ -14,6 +14,7 @@ import com.alibaba.easyexcel.test.core.large.LargeData;
import com.alibaba.easyexcel.test.demo.write.DemoData;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import net.sf.cglib.beans.BeanMap;
@ -43,7 +44,35 @@ public class Wirte {
String fileName = TestFileUtil.getPath() + "t22" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去读,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
// 如果这里想使用03 则 传入excelType参数即可
EasyExcel.write(fileName, DemoData.class).sheet("模板").doWrite(data());
EasyExcel.write(fileName, DemoData.class).relativeHeadRowIndex(10).sheet("模板").doWrite(data());
}
@Test
public void simpleWrite2() {
// 写法1
String fileName = TestFileUtil.getPath() + "t22" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去读,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
// 如果这里想使用03 则 传入excelType参数即可
EasyExcel.write(fileName, WriteData.class).sheet("模板").doWrite(data1());
}
@Test
public void json() {
JsonData jsonData = new JsonData();
jsonData.setSS1("11");
jsonData.setSS2("22");
jsonData.setSs3("33");
System.out.println(JSON.toJSONString(jsonData));
}
@Test
public void json3() {
String json = "{\"SS1\":\"11\",\"sS2\":\"22\",\"ss3\":\"33\"}";
JsonData jsonData = JSON.parseObject(json, JsonData.class);
System.out.println(JSON.toJSONString(jsonData));
}
private List<List<String>> head() {
@ -72,4 +101,14 @@ public class Wirte {
return list;
}
private List<WriteData> data1() {
List<WriteData> list = new ArrayList<WriteData>();
for (int i = 0; i < 10; i++) {
WriteData data = new WriteData();
data.setF(300.35f);
list.add(data);
}
return list;
}
}

13
src/test/java/com/alibaba/easyexcel/test/temp/simple/WriteData.java

@ -0,0 +1,13 @@
package com.alibaba.easyexcel.test.temp.simple;
import lombok.Data;
/**
* write data
*
* @author Jiaju Zhuang
**/
@Data
public class WriteData {
private float f;
}

BIN
src/test/resources/dataformat/dataformat.xls

Binary file not shown.

BIN
src/test/resources/dataformat/dataformat.xlsx

Binary file not shown.

6
update.md

@ -1,3 +1,9 @@
# 2.2.0-beta1
* 修复用String接收日期、数字和excel显示不一致的bug(不是完美修复,但是大部分情况已经兼容)
* 降低Ehcache版本 3.7.1(jkd7) -> 3.4.0(jdk6)
* 修复xls 用Map接收时多次接收会是同一个对象的bug
* 修复浮点型数据导入到excel 会丢失精度的bug
# 2.1.3
* 每个java进程单独创建一个缓存目录 [Issue #813](https://github.com/alibaba/easyexcel/issues/813)
* 统一修改合并为unsafe,提高大量数据导出的合并的效率

Loading…
Cancel
Save