3. 直接读即可
*/
@PostMapping("upload")
@ResponseBody
public String upload(MultipartFile file) throws IOException {
- EasyExcel.read(file.getInputStream(), UploadData.class, new UploadDataListener()).sheet().doRead();
+ EasyExcel.read(file.getInputStream(), UploadData.class, new UploadDataListener(uploadDAO)).sheet().doRead();
return "success";
}
```
diff --git a/img/readme/quickstart/fill/complexFill.png b/img/readme/quickstart/fill/complexFill.png
deleted file mode 100644
index d37bb0f9..00000000
Binary files a/img/readme/quickstart/fill/complexFill.png and /dev/null differ
diff --git a/img/readme/quickstart/fill/complexFillTemplate.png b/img/readme/quickstart/fill/complexFillTemplate.png
deleted file mode 100644
index 14e26b95..00000000
Binary files a/img/readme/quickstart/fill/complexFillTemplate.png and /dev/null differ
diff --git a/img/readme/quickstart/fill/complexFillWithTable.png b/img/readme/quickstart/fill/complexFillWithTable.png
deleted file mode 100644
index c1075201..00000000
Binary files a/img/readme/quickstart/fill/complexFillWithTable.png and /dev/null differ
diff --git a/img/readme/quickstart/fill/complexFillWithTableTemplate.png b/img/readme/quickstart/fill/complexFillWithTableTemplate.png
deleted file mode 100644
index 902cd0da..00000000
Binary files a/img/readme/quickstart/fill/complexFillWithTableTemplate.png and /dev/null differ
diff --git a/img/readme/quickstart/fill/horizontalFill.png b/img/readme/quickstart/fill/horizontalFill.png
deleted file mode 100644
index 10f90cd7..00000000
Binary files a/img/readme/quickstart/fill/horizontalFill.png and /dev/null differ
diff --git a/img/readme/quickstart/fill/horizontalFillTemplate.png b/img/readme/quickstart/fill/horizontalFillTemplate.png
deleted file mode 100644
index 462b86eb..00000000
Binary files a/img/readme/quickstart/fill/horizontalFillTemplate.png and /dev/null differ
diff --git a/img/readme/quickstart/fill/listFill.png b/img/readme/quickstart/fill/listFill.png
deleted file mode 100644
index 3baca5a8..00000000
Binary files a/img/readme/quickstart/fill/listFill.png and /dev/null differ
diff --git a/img/readme/quickstart/fill/listFillTemplate.png b/img/readme/quickstart/fill/listFillTemplate.png
deleted file mode 100644
index 010c8c16..00000000
Binary files a/img/readme/quickstart/fill/listFillTemplate.png and /dev/null differ
diff --git a/img/readme/quickstart/fill/simpleFill.png b/img/readme/quickstart/fill/simpleFill.png
deleted file mode 100644
index 4f575628..00000000
Binary files a/img/readme/quickstart/fill/simpleFill.png and /dev/null differ
diff --git a/img/readme/quickstart/fill/simpleFillTemplate.png b/img/readme/quickstart/fill/simpleFillTemplate.png
deleted file mode 100644
index 97a9119d..00000000
Binary files a/img/readme/quickstart/fill/simpleFillTemplate.png and /dev/null differ
diff --git a/img/readme/quickstart/read/demo.png b/img/readme/quickstart/read/demo.png
deleted file mode 100644
index 8bd14139..00000000
Binary files a/img/readme/quickstart/read/demo.png and /dev/null differ
diff --git a/img/readme/quickstart/write/complexHeadWrite.png b/img/readme/quickstart/write/complexHeadWrite.png
deleted file mode 100644
index 995ffc3c..00000000
Binary files a/img/readme/quickstart/write/complexHeadWrite.png and /dev/null differ
diff --git a/img/readme/quickstart/write/converterWrite.png b/img/readme/quickstart/write/converterWrite.png
deleted file mode 100644
index 5c9f2898..00000000
Binary files a/img/readme/quickstart/write/converterWrite.png and /dev/null differ
diff --git a/img/readme/quickstart/write/customHandlerWrite.png b/img/readme/quickstart/write/customHandlerWrite.png
deleted file mode 100644
index 41916c3f..00000000
Binary files a/img/readme/quickstart/write/customHandlerWrite.png and /dev/null differ
diff --git a/img/readme/quickstart/write/dynamicHeadWrite.png b/img/readme/quickstart/write/dynamicHeadWrite.png
deleted file mode 100644
index 3b25ad72..00000000
Binary files a/img/readme/quickstart/write/dynamicHeadWrite.png and /dev/null differ
diff --git a/img/readme/quickstart/write/imageWrite.png b/img/readme/quickstart/write/imageWrite.png
deleted file mode 100644
index c6a0c67a..00000000
Binary files a/img/readme/quickstart/write/imageWrite.png and /dev/null differ
diff --git a/img/readme/quickstart/write/indexWrite.png b/img/readme/quickstart/write/indexWrite.png
deleted file mode 100644
index 8dbec177..00000000
Binary files a/img/readme/quickstart/write/indexWrite.png and /dev/null differ
diff --git a/img/readme/quickstart/write/longestMatchColumnWidthWrite.png b/img/readme/quickstart/write/longestMatchColumnWidthWrite.png
deleted file mode 100644
index c8a06049..00000000
Binary files a/img/readme/quickstart/write/longestMatchColumnWidthWrite.png and /dev/null differ
diff --git a/img/readme/quickstart/write/mergeWrite.png b/img/readme/quickstart/write/mergeWrite.png
deleted file mode 100644
index b631e59b..00000000
Binary files a/img/readme/quickstart/write/mergeWrite.png and /dev/null differ
diff --git a/img/readme/quickstart/write/repeatedWrite.png b/img/readme/quickstart/write/repeatedWrite.png
deleted file mode 100644
index fb204a4b..00000000
Binary files a/img/readme/quickstart/write/repeatedWrite.png and /dev/null differ
diff --git a/img/readme/quickstart/write/simpleWrite.png b/img/readme/quickstart/write/simpleWrite.png
deleted file mode 100644
index e5924b19..00000000
Binary files a/img/readme/quickstart/write/simpleWrite.png and /dev/null differ
diff --git a/img/readme/quickstart/write/styleWrite.png b/img/readme/quickstart/write/styleWrite.png
deleted file mode 100644
index 356c3a7f..00000000
Binary files a/img/readme/quickstart/write/styleWrite.png and /dev/null differ
diff --git a/img/readme/quickstart/write/tableWrite.png b/img/readme/quickstart/write/tableWrite.png
deleted file mode 100644
index 0006a9ea..00000000
Binary files a/img/readme/quickstart/write/tableWrite.png and /dev/null differ
diff --git a/img/readme/quickstart/write/templateWrite.png b/img/readme/quickstart/write/templateWrite.png
deleted file mode 100644
index 75f7cb16..00000000
Binary files a/img/readme/quickstart/write/templateWrite.png and /dev/null differ
diff --git a/img/readme/quickstart/write/widthAndHeightWrite.png b/img/readme/quickstart/write/widthAndHeightWrite.png
deleted file mode 100644
index d59d8972..00000000
Binary files a/img/readme/quickstart/write/widthAndHeightWrite.png and /dev/null differ
diff --git a/img/readme/wechat.png b/img/readme/wechat.png
deleted file mode 100644
index b8057604..00000000
Binary files a/img/readme/wechat.png and /dev/null differ
diff --git a/pom.xml b/pom.xml
index 68fe674a..cb5bb97b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -4,7 +4,7 @@
4.0.0com.alibabaeasyexcel
- 2.1.0-beta1
+ 2.1.4jareasyexcel
@@ -17,7 +17,7 @@
UTF-8
- 1.7
+ 1.6
@@ -66,6 +66,11 @@
poi-ooxml3.17
+
+ org.apache.poi
+ poi-ooxml-schemas
+ 3.17
+ cglibcglib
@@ -79,7 +84,7 @@
org.ehcacheehcache
- 3.7.1
+ 3.4.0
@@ -132,11 +137,11 @@
-
+
org.apache.maven.pluginsmaven-pmd-plugin
- 3.12.0
+ 3.8truetrue
@@ -157,7 +162,6 @@
-
pmd-check-verifyvalidate
@@ -170,7 +174,7 @@
com.alibaba.p3cp3c-pmd
- 2.0.0
+ 1.3.6
diff --git a/src/main/java/com/alibaba/excel/ExcelReader.java b/src/main/java/com/alibaba/excel/ExcelReader.java
index 0887e325..07a373b7 100644
--- a/src/main/java/com/alibaba/excel/ExcelReader.java
+++ b/src/main/java/com/alibaba/excel/ExcelReader.java
@@ -14,7 +14,6 @@ import com.alibaba.excel.analysis.ExcelReadExecutor;
import com.alibaba.excel.cache.MapCache;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
-import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.parameter.AnalysisParam;
import com.alibaba.excel.read.listener.ReadListener;
@@ -35,8 +34,6 @@ public class ExcelReader {
*/
private ExcelAnalyser excelAnalyser;
- private boolean finished = false;
-
/**
* Create new reader
*
@@ -160,7 +157,6 @@ public class ExcelReader {
* Parse all sheet content by default
*/
public void readAll() {
- checkFinished();
excelAnalyser.analysis(null, Boolean.TRUE);
}
@@ -181,7 +177,6 @@ public class ExcelReader {
* @return
*/
public ExcelReader read(List readSheetList) {
- checkFinished();
excelAnalyser.analysis(readSheetList, Boolean.FALSE);
return this;
}
@@ -231,7 +226,6 @@ public class ExcelReader {
* @return
*/
public AnalysisContext analysisContext() {
- checkFinished();
return excelAnalyser.analysisContext();
}
@@ -241,7 +235,6 @@ public class ExcelReader {
* @return
*/
public ExcelReadExecutor excelExecutor() {
- checkFinished();
return excelAnalyser.excelExecutor();
}
@@ -281,10 +274,6 @@ public class ExcelReader {
* Complete the entire read file.Release the cache and close stream.
*/
public void finish() {
- if (finished) {
- return;
- }
- finished = true;
excelAnalyser.finish();
}
@@ -294,19 +283,11 @@ public class ExcelReader {
*/
@Override
protected void finalize() {
- if (finished) {
- return;
- }
try {
- excelAnalyser.finish();
+ finish();
} catch (Throwable e) {
LOGGER.warn("Destroy object failed", e);
}
}
- private void checkFinished() {
- if (finished) {
- throw new ExcelAnalysisException("Can not use a finished reader.");
- }
- }
}
diff --git a/src/main/java/com/alibaba/excel/ExcelWriter.java b/src/main/java/com/alibaba/excel/ExcelWriter.java
index 65a99fe5..a0b4795b 100644
--- a/src/main/java/com/alibaba/excel/ExcelWriter.java
+++ b/src/main/java/com/alibaba/excel/ExcelWriter.java
@@ -5,6 +5,9 @@ import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
import com.alibaba.excel.context.WriteContext;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.metadata.Table;
@@ -31,6 +34,8 @@ import com.alibaba.excel.write.metadata.fill.FillConfig;
* @author jipengfei
*/
public class ExcelWriter {
+ private static final Logger LOGGER = LoggerFactory.getLogger(ExcelWriter.class);
+
private ExcelBuilder excelBuilder;
/**
@@ -320,7 +325,20 @@ public class ExcelWriter {
* Close IO
*/
public void finish() {
- excelBuilder.finish();
+ excelBuilder.finish(false);
+ }
+
+ /**
+ * Prevents calls to {@link #finish} from freeing the cache
+ *
+ */
+ @Override
+ protected void finalize() {
+ try {
+ finish();
+ } catch (Throwable e) {
+ LOGGER.warn("Destroy object failed", e);
+ }
}
/**
diff --git a/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java b/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java
index 6e1f435d..feec16b6 100644
--- a/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java
+++ b/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java
@@ -1,13 +1,10 @@
package com.alibaba.excel.analysis;
-import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.crypt.Decryptor;
-import org.apache.poi.poifs.crypt.EncryptionInfo;
import org.apache.poi.poifs.filesystem.DocumentFactoryHelper;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.IOUtils;
@@ -25,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;
/**
@@ -37,6 +36,10 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
private AnalysisContext analysisContext;
private ExcelReadExecutor excelReadExecutor;
+ /**
+ * Prevent multiple shutdowns
+ */
+ private boolean finished = false;
public ExcelAnalyserImpl(ReadWorkbook readWorkbook) {
try {
@@ -124,31 +127,37 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
@Override
public void finish() {
+ if (finished) {
+ return;
+ }
+ finished = true;
if (analysisContext == null || analysisContext.readWorkbookHolder() == null) {
return;
}
ReadWorkbookHolder readWorkbookHolder = analysisContext.readWorkbookHolder();
+ Throwable throwable = null;
+
try {
if (readWorkbookHolder.getReadCache() != null) {
readWorkbookHolder.getReadCache().destroy();
}
} catch (Throwable t) {
- throwCanNotCloseIo(t);
+ throwable = t;
}
try {
if (readWorkbookHolder.getOpcPackage() != null) {
readWorkbookHolder.getOpcPackage().revert();
}
} catch (Throwable t) {
- throwCanNotCloseIo(t);
+ throwable = t;
}
try {
if (readWorkbookHolder.getPoifsFileSystem() != null) {
readWorkbookHolder.getPoifsFileSystem().close();
}
} catch (Throwable t) {
- throwCanNotCloseIo(t);
+ throwable = t;
}
try {
if (analysisContext.readWorkbookHolder().getAutoCloseStream()
@@ -156,17 +165,28 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
readWorkbookHolder.getInputStream().close();
}
} catch (Throwable t) {
- throwCanNotCloseIo(t);
+ throwable = t;
}
try {
if (readWorkbookHolder.getTempFile() != null) {
FileUtils.delete(readWorkbookHolder.getTempFile());
}
} catch (Throwable t) {
- throwCanNotCloseIo(t);
+ throwable = t;
}
clearEncrypt03();
+
+ removeThreadLocalCache();
+
+ if (throwable != null) {
+ throw new ExcelAnalysisException("Can not close IO.", throwable);
+ }
+ }
+
+ private void removeThreadLocalCache() {
+ NumberDataFormatterUtils.removeThreadLocalCache();
+ DateUtils.removeThreadLocalCache();
}
private void clearEncrypt03() {
@@ -177,10 +197,6 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
Biff8EncryptionKey.setCurrentUserPassword(null);
}
- private void throwCanNotCloseIo(Throwable t) {
- throw new ExcelAnalysisException("Can not close IO", t);
- }
-
@Override
public ExcelReadExecutor excelExecutor() {
return excelReadExecutor;
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 e5df4bfa..5ef640e9 100644
--- a/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java
+++ b/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java
@@ -4,9 +4,9 @@ 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 com.alibaba.excel.util.StringUtils;
import org.apache.poi.hssf.eventusermodel.EventWorkbookBuilder;
@@ -33,6 +33,7 @@ import com.alibaba.excel.analysis.ExcelReadExecutor;
import com.alibaba.excel.analysis.v03.handlers.BlankOrErrorRecordHandler;
import com.alibaba.excel.analysis.v03.handlers.BofRecordHandler;
import com.alibaba.excel.analysis.v03.handlers.FormulaRecordHandler;
+import com.alibaba.excel.analysis.v03.handlers.IndexRecordHandler;
import com.alibaba.excel.analysis.v03.handlers.LabelRecordHandler;
import com.alibaba.excel.analysis.v03.handlers.MissingCellDummyRecordHandler;
import com.alibaba.excel.analysis.v03.handlers.NoteRecordHandler;
@@ -86,7 +87,7 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor {
public XlsSaxAnalyser(AnalysisContext context, POIFSFileSystem poifsFileSystem) {
this.analysisContext = context;
- this.records = new TreeMap();
+ this.records = new LinkedHashMap();
this.poifsFileSystem = poifsFileSystem;
try {
this.poiWorkbook = WorkbookFactory.create(poifsFileSystem);
@@ -134,7 +135,7 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor {
private void init() {
lastRowNumber = 0;
lastColumnNumber = 0;
- records = new TreeMap();
+ records = new LinkedHashMap();
buildXlsRecordHandlers();
}
@@ -227,7 +228,7 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor {
analysisContext.readRowHolder().setRowComments(rowComments);
}
analysisContext.readSheetHolder().notifyEndOneRow(new EachRowAnalysisFinishEvent(records), analysisContext);
- records.clear();
+ records = new HashMap();
lastColumnNumber = -1;
}
@@ -244,10 +245,11 @@ 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());
+ recordHandlers.add(new IndexRecordHandler(analysisContext));
Collections.sort(recordHandlers);
}
diff --git a/src/main/java/com/alibaba/excel/analysis/v03/handlers/BofRecordHandler.java b/src/main/java/com/alibaba/excel/analysis/v03/handlers/BofRecordHandler.java
index 3a862942..083edbc7 100644
--- a/src/main/java/com/alibaba/excel/analysis/v03/handlers/BofRecordHandler.java
+++ b/src/main/java/com/alibaba/excel/analysis/v03/handlers/BofRecordHandler.java
@@ -73,7 +73,7 @@ public class BofRecordHandler extends AbstractXlsRecordHandler {
readSheet = SheetUtils.match(readSheet, readSheetList, readAll,
context.readWorkbookHolder().getGlobalConfiguration());
if (readSheet != null) {
- if (readSheet.getSheetNo() != 0) {
+ if (readSheet.getSheetNo() != 0 && context.readSheetHolder() != null) {
// Prompt for the end of the previous form read
context.readSheetHolder().notifyAfterAllAnalysed(context);
}
diff --git a/src/main/java/com/alibaba/excel/analysis/v03/handlers/IndexRecordHandler.java b/src/main/java/com/alibaba/excel/analysis/v03/handlers/IndexRecordHandler.java
new file mode 100644
index 00000000..6837ebd6
--- /dev/null
+++ b/src/main/java/com/alibaba/excel/analysis/v03/handlers/IndexRecordHandler.java
@@ -0,0 +1,42 @@
+package com.alibaba.excel.analysis.v03.handlers;
+
+import org.apache.poi.hssf.record.IndexRecord;
+import org.apache.poi.hssf.record.Record;
+
+import com.alibaba.excel.analysis.v03.AbstractXlsRecordHandler;
+import com.alibaba.excel.context.AnalysisContext;
+
+/**
+ * Record handler
+ *
+ * @author Jiaju Zhuang
+ */
+public class IndexRecordHandler extends AbstractXlsRecordHandler {
+
+ private AnalysisContext context;
+
+ public IndexRecordHandler(AnalysisContext context) {
+ this.context = context;
+ }
+
+ @Override
+ public boolean support(Record record) {
+ return record instanceof IndexRecord;
+ }
+
+ @Override
+ public void init() {}
+
+ @Override
+ public void processRecord(Record record) {
+ if (context.readSheetHolder() == null) {
+ return;
+ }
+ context.readSheetHolder().setApproximateTotalRowNumber(((IndexRecord)record).getLastRowAdd1());
+ }
+
+ @Override
+ public int getOrder() {
+ return 1;
+ }
+}
diff --git a/src/main/java/com/alibaba/excel/analysis/v03/handlers/NumberRecordHandler.java b/src/main/java/com/alibaba/excel/analysis/v03/handlers/NumberRecordHandler.java
index 75c3128a..af024882 100644
--- a/src/main/java/com/alibaba/excel/analysis/v03/handlers/NumberRecordHandler.java
+++ b/src/main/java/com/alibaba/excel/analysis/v03/handlers/NumberRecordHandler.java
@@ -7,6 +7,8 @@ import org.apache.poi.hssf.record.NumberRecord;
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;
/**
@@ -17,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;
}
@@ -32,8 +37,10 @@ public class NumberRecordHandler extends AbstractXlsRecordHandler {
this.row = numrec.getRow();
this.column = numrec.getColumn();
this.cellData = new CellData(BigDecimal.valueOf(numrec.getValue()));
- this.cellData.setDataFormat(formatListener.getFormatIndex(numrec));
- this.cellData.setDataFormatString(formatListener.getFormatString(numrec));
+ int dataFormat = formatListener.getFormatIndex(numrec);
+ this.cellData.setDataFormat(dataFormat);
+ this.cellData.setDataFormatString(BuiltinFormats.getBuiltinFormat(dataFormat,
+ formatListener.getFormatString(numrec), context.readSheetHolder().getGlobalConfiguration().getLocale()));
}
@Override
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 591932e3..374dc45b 100644
--- a/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java
+++ b/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java
@@ -15,7 +15,6 @@ import javax.xml.parsers.SAXParserFactory;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.openxml4j.opc.PackagePart;
-import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.model.CommentsTable;
import org.apache.poi.xssf.model.StylesTable;
@@ -164,7 +163,13 @@ public class XlsxSaxAnalyser implements ExcelReadExecutor {
private void parseXmlSource(InputStream inputStream, ContentHandler handler) {
InputSource inputSource = new InputSource(inputStream);
try {
- SAXParserFactory saxFactory = SAXParserFactory.newInstance();
+ SAXParserFactory saxFactory;
+ String xlsxSAXParserFactoryName = analysisContext.readWorkbookHolder().getXlsxSAXParserFactoryName();
+ if (StringUtils.isEmpty(xlsxSAXParserFactoryName)) {
+ saxFactory = SAXParserFactory.newInstance();
+ } else {
+ saxFactory = SAXParserFactory.newInstance(xlsxSAXParserFactoryName, null);
+ }
saxFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
saxFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
saxFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
diff --git a/src/main/java/com/alibaba/excel/analysis/v07/handlers/CountRowCellHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/handlers/CountRowCellHandler.java
index c92ec6ca..c838aec6 100644
--- a/src/main/java/com/alibaba/excel/analysis/v07/handlers/CountRowCellHandler.java
+++ b/src/main/java/com/alibaba/excel/analysis/v07/handlers/CountRowCellHandler.java
@@ -10,7 +10,7 @@ import com.alibaba.excel.context.AnalysisContext;
/**
* Cell Handler
- *
+ *
* @author jipengfei
*/
public class CountRowCellHandler implements XlsxCellHandler {
@@ -31,7 +31,7 @@ public class CountRowCellHandler implements XlsxCellHandler {
String d = attributes.getValue(DIMENSION_REF);
String totalStr = d.substring(d.indexOf(":") + 1, d.length());
String c = totalStr.toUpperCase().replaceAll("[A-Z]", "");
- analysisContext.readSheetHolder().setTotal(Integer.parseInt(c));
+ analysisContext.readSheetHolder().setApproximateTotalRowNumber(Integer.parseInt(c));
}
@Override
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 2dc0fdb9..f4534fb7 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
@@ -9,11 +9,10 @@ import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_VALUE_TYPE_TAG;
import java.math.BigDecimal;
import java.util.Deque;
+import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
-import java.util.TreeMap;
-import org.apache.poi.ss.usermodel.BuiltinFormats;
import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
@@ -21,6 +20,7 @@ import org.xml.sax.Attributes;
import com.alibaba.excel.analysis.v07.XlsxCellHandler;
import com.alibaba.excel.analysis.v07.XlsxRowResultHolder;
+import com.alibaba.excel.constant.BuiltinFormats;
import com.alibaba.excel.constant.ExcelXmlConstants;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
@@ -37,7 +37,7 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
private final AnalysisContext analysisContext;
private Deque currentTagDeque = new LinkedList();
private int curCol;
- private Map curRowContent = new TreeMap();
+ private Map curRowContent = new LinkedHashMap();
private CellData currentCellData;
private StringBuilder dataStringBuilder;
private StringBuilder formulaStringBuilder;
@@ -54,7 +54,7 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
@Override
public void clearResult() {
- curRowContent = new TreeMap();
+ curRowContent = new LinkedHashMap();
}
@Override
@@ -87,13 +87,10 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
int dateFormatIndexInteger = Integer.parseInt(dateFormatIndex);
XSSFCellStyle xssfCellStyle = stylesTable.getStyleAt(dateFormatIndexInteger);
int dataFormat = xssfCellStyle.getDataFormat();
- String dataFormatString = xssfCellStyle.getDataFormatString();
currentCellData.setDataFormat(dataFormat);
- if (dataFormatString == null) {
- currentCellData.setDataFormatString(BuiltinFormats.getBuiltinFormat(dataFormat));
- } else {
- currentCellData.setDataFormatString(dataFormatString);
- }
+ currentCellData.setDataFormatString(
+ BuiltinFormats.getBuiltinFormat(dataFormat, xssfCellStyle.getDataFormatString(),
+ analysisContext.readSheetHolder().getGlobalConfiguration().getLocale()));
}
}
// cell is formula
diff --git a/src/main/java/com/alibaba/excel/constant/BuiltinFormats.java b/src/main/java/com/alibaba/excel/constant/BuiltinFormats.java
new file mode 100644
index 00000000..1dec2c68
--- /dev/null
+++ b/src/main/java/com/alibaba/excel/constant/BuiltinFormats.java
@@ -0,0 +1,379 @@
+package com.alibaba.excel.constant;
+
+import java.util.Locale;
+
+/**
+ * Excel's built-in format conversion.Currently only supports Chinese.
+ *
+ *
+ * If it is not Chinese, it is recommended to directly modify the builtinFormats, which will better support
+ * internationalization in the future.
+ *
+ *
* 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() {
diff --git a/src/main/java/com/alibaba/excel/metadata/AbstractParameterBuilder.java b/src/main/java/com/alibaba/excel/metadata/AbstractParameterBuilder.java
new file mode 100644
index 00000000..58400465
--- /dev/null
+++ b/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 {
+ /**
+ * You can only choose one of the {@link #head(List)} and {@link #head(Class)}
+ *
+ * @param head
+ * @return
+ */
+ public T head(List> 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());
+ }
+ 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 Locale 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();
+}
diff --git a/src/main/java/com/alibaba/excel/metadata/BasicParameter.java b/src/main/java/com/alibaba/excel/metadata/BasicParameter.java
index 40bffe63..d00c93a8 100644
--- a/src/main/java/com/alibaba/excel/metadata/BasicParameter.java
+++ b/src/main/java/com/alibaba/excel/metadata/BasicParameter.java
@@ -1,6 +1,7 @@
package com.alibaba.excel.metadata;
import java.util.List;
+import java.util.Locale;
import com.alibaba.excel.converters.Converter;
@@ -34,6 +35,11 @@ public class BasicParameter {
* @return
*/
private Boolean use1904windowing;
+ /**
+ * A Locale object represents a specific geographical, political, or cultural region. This parameter is
+ * used when formatting dates and numbers.
+ */
+ private Locale locale;
public List> getHead() {
return head;
@@ -75,4 +81,11 @@ public class BasicParameter {
this.use1904windowing = use1904windowing;
}
+ public Locale getLocale() {
+ return locale;
+ }
+
+ public void setLocale(Locale locale) {
+ this.locale = locale;
+ }
}
diff --git a/src/main/java/com/alibaba/excel/metadata/CellData.java b/src/main/java/com/alibaba/excel/metadata/CellData.java
index a3a40c61..7cd5496e 100644
--- a/src/main/java/com/alibaba/excel/metadata/CellData.java
+++ b/src/main/java/com/alibaba/excel/metadata/CellData.java
@@ -202,6 +202,9 @@ public class CellData {
* Ensure that the object does not appear null
*/
public void checkEmpty() {
+ if (type == null) {
+ type = CellDataTypeEnum.EMPTY;
+ }
switch (type) {
case STRING:
case ERROR:
@@ -225,16 +228,22 @@ public class CellData {
@Override
public String toString() {
+ if (type == null) {
+ return StringUtils.EMPTY;
+ }
switch (type) {
case NUMBER:
return numberValue.toString();
case BOOLEAN:
return booleanValue.toString();
+ case DIRECT_STRING:
case STRING:
case ERROR:
return stringValue;
+ case IMAGE:
+ return "image[" + imageValue.length + "]";
default:
- return "empty";
+ return StringUtils.EMPTY;
}
}
diff --git a/src/main/java/com/alibaba/excel/metadata/DataFormatter.java b/src/main/java/com/alibaba/excel/metadata/DataFormatter.java
new file mode 100644
index 00000000..7467cd26
--- /dev/null
+++ b/src/main/java/com/alibaba/excel/metadata/DataFormatter.java
@@ -0,0 +1,786 @@
+/*
+ * ==================================================================== Licensed to the Apache Software Foundation (ASF)
+ * under one or more contributor license agreements. See the NOTICE file distributed with this work for additional
+ * information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the
+ * License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
+ * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ *
+ * 2012 - Alfresco Software, Ltd. Alfresco Software has modified source of this file The details of changes as svn diff
+ * can be found in svn at location root/projects/3rd-party/src
+ * ====================================================================
+ */
+package com.alibaba.excel.metadata;
+
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+import java.text.DateFormatSymbols;
+import java.text.DecimalFormat;
+import java.text.DecimalFormatSymbols;
+import java.text.FieldPosition;
+import java.text.Format;
+import java.text.ParsePosition;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.poi.ss.usermodel.DateUtil;
+import org.apache.poi.ss.usermodel.ExcelGeneralNumberFormat;
+import org.apache.poi.ss.usermodel.ExcelStyleDateFormatter;
+import org.apache.poi.ss.usermodel.FractionFormat;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.alibaba.excel.util.DateUtils;
+
+/**
+ * Written with reference to {@link org.apache.poi.ss.usermodel.DataFormatter}.Made some optimizations for date
+ * conversion.
+ *
+ * This is a non-thread-safe class.
+ *
+ * @author Jiaju Zhuang
+ */
+public class DataFormatter {
+ /** For logging any problems we find */
+ private static final Logger LOGGER = LoggerFactory.getLogger(DataFormatter.class);
+ private static final String defaultFractionWholePartFormat = "#";
+ private static final String defaultFractionFractionPartFormat = "#/##";
+ /** Pattern to find a number format: "0" or "#" */
+ private static final Pattern numPattern = Pattern.compile("[0#]+");
+
+ /** Pattern to find days of week as text "ddd...." */
+ 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);
+
+ /** Pattern to find formats with condition ranges e.g. [>=100] */
+ private static final Pattern rangeConditionalPattern =
+ Pattern.compile(".*\\[\\s*(>|>=|<|<=|=)\\s*[0-9]*\\.*[0-9].*");
+
+ /**
+ * A regex to find locale patterns like [$$-1009] and [$?-452]. Note that we don't currently process these into
+ * locales
+ */
+ private static final Pattern localePatternGroup = Pattern.compile("(\\[\\$[^-\\]]*-[0-9A-Z]+])");
+
+ /**
+ * A regex to match the colour formattings rules. Allowed colours are: Black, Blue, Cyan, Green, Magenta, Red,
+ * White, Yellow, "Color n" (1<=n<=56)
+ */
+ private static final Pattern colorPattern = Pattern.compile(
+ "(\\[BLACK])|(\\[BLUE])|(\\[CYAN])|(\\[GREEN])|" + "(\\[MAGENTA])|(\\[RED])|(\\[WHITE])|(\\[YELLOW])|"
+ + "(\\[COLOR\\s*\\d])|(\\[COLOR\\s*[0-5]\\d])|(\\[DBNum(1|2|3)])|(\\[\\$-\\d{0,3}])",
+ Pattern.CASE_INSENSITIVE);
+
+ /**
+ * A regex to identify a fraction pattern. This requires that replaceAll("\\?", "#") has already been called
+ */
+ private static final Pattern fractionPattern = Pattern.compile("(?:([#\\d]+)\\s+)?(#+)\\s*/\\s*([#\\d]+)");
+
+ /**
+ * A regex to strip junk out of fraction formats
+ */
+ private static final Pattern fractionStripper = Pattern.compile("(\"[^\"]*\")|([^ ?#\\d/]+)");
+
+ /**
+ * A regex to detect if an alternate grouping character is used in a numeric format
+ */
+ private static final Pattern alternateGrouping = Pattern.compile("([#0]([^.#0])[#0]{3})");
+
+ /**
+ * Cells formatted with a date or time format and which contain invalid date or time values show 255 pound signs
+ * ("#").
+ */
+ private static final String invalidDateTimeString;
+ static {
+ StringBuilder buf = new StringBuilder();
+ for (int i = 0; i < 255; i++)
+ buf.append('#');
+ invalidDateTimeString = buf.toString();
+ }
+
+ /**
+ * The decimal symbols of the locale used for formatting values.
+ */
+ private DecimalFormatSymbols decimalSymbols;
+
+ /**
+ * The date symbols of the locale used for formatting values.
+ */
+ private DateFormatSymbols dateSymbols;
+ /** A default format to use when a number pattern cannot be parsed. */
+ private Format defaultNumFormat;
+ /**
+ * A map to cache formats. Map formats
+ */
+ private final Map formats = new HashMap();
+
+ /** stores the locale valid it the last formatting call */
+ private Locale locale;
+ /**
+ * true if date uses 1904 windowing, or false if using 1900 date windowing.
+ *
+ * default is false
+ *
+ * @return
+ */
+ private Boolean use1904windowing;
+
+ /**
+ * Creates a formatter using the {@link Locale#getDefault() default locale}.
+ */
+ public DataFormatter() {
+ this(null, null);
+ }
+
+ /**
+ * Creates a formatter using the given locale.
+ *
+ */
+ public DataFormatter(Locale locale, Boolean use1904windowing) {
+ this.use1904windowing = use1904windowing != null ? use1904windowing : Boolean.FALSE;
+ this.locale = locale != null ? locale : Locale.getDefault();
+ this.dateSymbols = DateFormatSymbols.getInstance(this.locale);
+ this.decimalSymbols = DecimalFormatSymbols.getInstance(this.locale);
+ }
+
+ private Format getFormat(Integer dataFormat, String dataFormatString) {
+ // See if we already have it cached
+ Format format = formats.get(dataFormatString);
+ if (format != null) {
+ return format;
+ }
+ // Is it one of the special built in types, General or @?
+ if ("General".equalsIgnoreCase(dataFormatString) || "@".equals(dataFormatString)) {
+ format = getDefaultFormat();
+ addFormat(dataFormatString, format);
+ return format;
+ }
+
+ // Build a formatter, and cache it
+ format = createFormat(dataFormat, dataFormatString);
+ addFormat(dataFormatString, format);
+ return format;
+ }
+
+ private Format createFormat(Integer dataFormat, String dataFormatString) {
+ String formatStr = dataFormatString;
+
+ Format format = checkSpecialConverter(formatStr);
+ if (format != null) {
+ return format;
+ }
+
+ // Remove colour formatting if present
+ Matcher colourM = colorPattern.matcher(formatStr);
+ while (colourM.find()) {
+ String colour = colourM.group();
+
+ // Paranoid replacement...
+ int at = formatStr.indexOf(colour);
+ if (at == -1)
+ break;
+ String nFormatStr = formatStr.substring(0, at) + formatStr.substring(at + colour.length());
+ if (nFormatStr.equals(formatStr))
+ break;
+
+ // Try again in case there's multiple
+ formatStr = nFormatStr;
+ colourM = colorPattern.matcher(formatStr);
+ }
+
+ // Strip off the locale information, we use an instance-wide locale for everything
+ Matcher m = localePatternGroup.matcher(formatStr);
+ while (m.find()) {
+ String match = m.group();
+ String symbol = match.substring(match.indexOf('$') + 1, match.indexOf('-'));
+ if (symbol.indexOf('$') > -1) {
+ symbol = symbol.substring(0, symbol.indexOf('$')) + '\\' + symbol.substring(symbol.indexOf('$'));
+ }
+ formatStr = m.replaceAll(symbol);
+ m = localePatternGroup.matcher(formatStr);
+ }
+
+ // Check for special cases
+ if (formatStr == null || formatStr.trim().length() == 0) {
+ return getDefaultFormat();
+ }
+
+ if ("General".equalsIgnoreCase(formatStr) || "@".equals(formatStr)) {
+ return getDefaultFormat();
+ }
+
+ if (DateUtils.isADateFormat(dataFormat, formatStr)) {
+ return createDateFormat(formatStr);
+ }
+ // Excel supports fractions in format strings, which Java doesn't
+ if (formatStr.contains("#/") || formatStr.contains("?/")) {
+ String[] chunks = formatStr.split(";");
+ for (String chunk1 : chunks) {
+ String chunk = chunk1.replaceAll("\\?", "#");
+ Matcher matcher = fractionStripper.matcher(chunk);
+ chunk = matcher.replaceAll(" ");
+ chunk = chunk.replaceAll(" +", " ");
+ Matcher fractionMatcher = fractionPattern.matcher(chunk);
+ // take the first match
+ if (fractionMatcher.find()) {
+ String wholePart = (fractionMatcher.group(1) == null) ? "" : defaultFractionWholePartFormat;
+ return new FractionFormat(wholePart, fractionMatcher.group(3));
+ }
+ }
+
+ // Strip custom text in quotes and escaped characters for now as it can cause performance problems in
+ // fractions.
+ // String strippedFormatStr = formatStr.replaceAll("\\\\ ", " ").replaceAll("\\\\.",
+ // "").replaceAll("\"[^\"]*\"", " ").replaceAll("\\?", "#");
+ return new FractionFormat(defaultFractionWholePartFormat, defaultFractionFractionPartFormat);
+ }
+
+ if (numPattern.matcher(formatStr).find()) {
+ return createNumberFormat(formatStr);
+ }
+ return getDefaultFormat();
+ }
+
+ private Format checkSpecialConverter(String dataFormatString) {
+ if ("00000\\-0000".equals(dataFormatString) || "00000-0000".equals(dataFormatString)) {
+ return new ZipPlusFourFormat();
+ }
+ if ("[<=9999999]###\\-####;\\(###\\)\\ ###\\-####".equals(dataFormatString)
+ || "[<=9999999]###-####;(###) ###-####".equals(dataFormatString)
+ || "###\\-####;\\(###\\)\\ ###\\-####".equals(dataFormatString)
+ || "###-####;(###) ###-####".equals(dataFormatString)) {
+ return new PhoneFormat();
+ }
+ if ("000\\-00\\-0000".equals(dataFormatString) || "000-00-0000".equals(dataFormatString)) {
+ return new SSNFormat();
+ }
+ return null;
+ }
+
+ private Format createDateFormat(String pFormatStr) {
+ String formatStr = pFormatStr;
+ formatStr = formatStr.replaceAll("\\\\-", "-");
+ formatStr = formatStr.replaceAll("\\\\,", ",");
+ formatStr = formatStr.replaceAll("\\\\\\.", "."); // . is a special regexp char
+ formatStr = formatStr.replaceAll("\\\\ ", " ");
+ formatStr = formatStr.replaceAll("\\\\/", "/"); // weird: m\\/d\\/yyyy
+ formatStr = formatStr.replaceAll(";@", "");
+ formatStr = formatStr.replaceAll("\"/\"", "/"); // "/" is escaped for no reason in: mm"/"dd"/"yyyy
+ formatStr = formatStr.replace("\"\"", "'"); // replace Excel quoting with Java style quoting
+ formatStr = formatStr.replaceAll("\\\\T", "'T'"); // Quote the T is iso8601 style dates
+ formatStr = formatStr.replace("\"", "");
+
+ boolean hasAmPm = false;
+ Matcher amPmMatcher = amPmPattern.matcher(formatStr);
+ while (amPmMatcher.find()) {
+ formatStr = amPmMatcher.replaceAll("@");
+ hasAmPm = true;
+ amPmMatcher = amPmPattern.matcher(formatStr);
+ }
+ formatStr = formatStr.replaceAll("@", "a");
+
+ Matcher dateMatcher = daysAsText.matcher(formatStr);
+ if (dateMatcher.find()) {
+ String match = dateMatcher.group(0).toUpperCase(Locale.ROOT).replaceAll("D", "E");
+ formatStr = dateMatcher.replaceAll(match);
+ }
+
+ // Convert excel date format to SimpleDateFormat.
+ // Excel uses lower and upper case 'm' for both minutes and months.
+ // From Excel help:
+ /*
+ The "m" or "mm" code must appear immediately after the "h" or"hh"
+ code or immediately before the "ss" code; otherwise, Microsoft
+ Excel displays the month instead of minutes."
+ */
+ StringBuilder sb = new StringBuilder();
+ char[] chars = formatStr.toCharArray();
+ boolean mIsMonth = true;
+ List ms = new ArrayList();
+ boolean isElapsed = false;
+ for (int j = 0; j < chars.length; j++) {
+ char c = chars[j];
+ if (c == '\'') {
+ sb.append(c);
+ j++;
+
+ // skip until the next quote
+ while (j < chars.length) {
+ c = chars[j];
+ sb.append(c);
+ if (c == '\'') {
+ break;
+ }
+ j++;
+ }
+ } else if (c == '[' && !isElapsed) {
+ isElapsed = true;
+ mIsMonth = false;
+ sb.append(c);
+ } else if (c == ']' && isElapsed) {
+ isElapsed = false;
+ sb.append(c);
+ } else if (isElapsed) {
+ if (c == 'h' || c == 'H') {
+ sb.append('H');
+ } else if (c == 'm' || c == 'M') {
+ sb.append('m');
+ } else if (c == 's' || c == 'S') {
+ sb.append('s');
+ } else {
+ sb.append(c);
+ }
+ } else if (c == 'h' || c == 'H') {
+ mIsMonth = false;
+ if (hasAmPm) {
+ sb.append('h');
+ } else {
+ sb.append('H');
+ }
+ } else if (c == 'm' || c == 'M') {
+ if (mIsMonth) {
+ sb.append('M');
+ ms.add(Integer.valueOf(sb.length() - 1));
+ } else {
+ sb.append('m');
+ }
+ } else if (c == 's' || c == 'S') {
+ sb.append('s');
+ // if 'M' precedes 's' it should be minutes ('m')
+ for (int index : ms) {
+ if (sb.charAt(index) == 'M') {
+ sb.replace(index, index + 1, "m");
+ }
+ }
+ mIsMonth = true;
+ ms.clear();
+ } else if (Character.isLetter(c)) {
+ mIsMonth = true;
+ ms.clear();
+ if (c == 'y' || c == 'Y') {
+ sb.append('y');
+ } else if (c == 'd' || c == 'D') {
+ sb.append('d');
+ } else {
+ sb.append(c);
+ }
+ } else {
+ if (Character.isWhitespace(c)) {
+ ms.clear();
+ }
+ sb.append(c);
+ }
+ }
+ formatStr = sb.toString();
+
+ try {
+ return new ExcelStyleDateFormatter(formatStr, dateSymbols);
+ } catch (IllegalArgumentException iae) {
+ LOGGER.debug("Formatting failed for format {}, falling back", formatStr, iae);
+ // the pattern could not be parsed correctly,
+ // so fall back to the default number format
+ return getDefaultFormat();
+ }
+
+ }
+
+ private String cleanFormatForNumber(String formatStr) {
+ StringBuilder sb = new StringBuilder(formatStr);
+ // If they requested spacers, with "_",
+ // remove those as we don't do spacing
+ // If they requested full-column-width
+ // padding, with "*", remove those too
+ for (int i = 0; i < sb.length(); i++) {
+ char c = sb.charAt(i);
+ if (c == '_' || c == '*') {
+ if (i > 0 && sb.charAt((i - 1)) == '\\') {
+ // It's escaped, don't worry
+ continue;
+ }
+ if (i < sb.length() - 1) {
+ // Remove the character we're supposed
+ // to match the space of / pad to the
+ // column width with
+ sb.deleteCharAt(i + 1);
+ }
+ // Remove the _ too
+ sb.deleteCharAt(i);
+ i--;
+ }
+ }
+
+ // Now, handle the other aspects like
+ // quoting and scientific notation
+ for (int i = 0; i < sb.length(); i++) {
+ char c = sb.charAt(i);
+ // remove quotes and back slashes
+ if (c == '\\' || c == '"') {
+ sb.deleteCharAt(i);
+ i--;
+
+ // for scientific/engineering notation
+ } else if (c == '+' && i > 0 && sb.charAt(i - 1) == 'E') {
+ sb.deleteCharAt(i);
+ i--;
+ }
+ }
+
+ return sb.toString();
+ }
+
+ private static class InternalDecimalFormatWithScale extends Format {
+
+ private static final Pattern endsWithCommas = Pattern.compile("(,+)$");
+ private BigDecimal divider;
+ private static final BigDecimal ONE_THOUSAND = new BigDecimal(1000);
+ private final DecimalFormat df;
+
+ private static String trimTrailingCommas(String s) {
+ return s.replaceAll(",+$", "");
+ }
+
+ public InternalDecimalFormatWithScale(String pattern, DecimalFormatSymbols symbols) {
+ df = new DecimalFormat(trimTrailingCommas(pattern), symbols);
+ setExcelStyleRoundingMode(df);
+ Matcher endsWithCommasMatcher = endsWithCommas.matcher(pattern);
+ if (endsWithCommasMatcher.find()) {
+ String commas = (endsWithCommasMatcher.group(1));
+ BigDecimal temp = BigDecimal.ONE;
+ for (int i = 0; i < commas.length(); ++i) {
+ temp = temp.multiply(ONE_THOUSAND);
+ }
+ divider = temp;
+ } else {
+ divider = null;
+ }
+ }
+
+ private Object scaleInput(Object obj) {
+ if (divider != null) {
+ if (obj instanceof BigDecimal) {
+ obj = ((BigDecimal)obj).divide(divider, RoundingMode.HALF_UP);
+ } else if (obj instanceof Double) {
+ obj = (Double)obj / divider.doubleValue();
+ } else {
+ throw new UnsupportedOperationException();
+ }
+ }
+ return obj;
+ }
+
+ @Override
+ public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
+ obj = scaleInput(obj);
+ return df.format(obj, toAppendTo, pos);
+ }
+
+ @Override
+ public Object parseObject(String source, ParsePosition pos) {
+ throw new UnsupportedOperationException();
+ }
+ }
+
+ private Format createNumberFormat(String formatStr) {
+ String format = cleanFormatForNumber(formatStr);
+ DecimalFormatSymbols symbols = decimalSymbols;
+
+ // Do we need to change the grouping character?
+ // eg for a format like #'##0 which wants 12'345 not 12,345
+ Matcher agm = alternateGrouping.matcher(format);
+ if (agm.find()) {
+ char grouping = agm.group(2).charAt(0);
+ // Only replace the grouping character if it is not the default
+ // grouping character for the US locale (',') in order to enable
+ // correct grouping for non-US locales.
+ if (grouping != ',') {
+ symbols = DecimalFormatSymbols.getInstance(locale);
+
+ symbols.setGroupingSeparator(grouping);
+ String oldPart = agm.group(1);
+ String newPart = oldPart.replace(grouping, ',');
+ format = format.replace(oldPart, newPart);
+ }
+ }
+
+ try {
+ return new InternalDecimalFormatWithScale(format, symbols);
+ } catch (IllegalArgumentException iae) {
+ LOGGER.debug("Formatting failed for format {}, falling back", formatStr, iae);
+ // the pattern could not be parsed correctly,
+ // so fall back to the default number format
+ return getDefaultFormat();
+ }
+ }
+
+ private Format getDefaultFormat() {
+ // for numeric cells try user supplied default
+ if (defaultNumFormat != null) {
+ return defaultNumFormat;
+ // otherwise use general format
+ }
+ defaultNumFormat = new ExcelGeneralNumberFormat(locale);
+ return defaultNumFormat;
+ }
+
+ /**
+ * Performs Excel-style date formatting, using the supplied Date and format
+ */
+ private String performDateFormatting(Date d, Format dateFormat) {
+ Format df = dateFormat != null ? dateFormat : getDefaultFormat();
+ return df.format(d);
+ }
+
+ /**
+ * Returns the formatted value of an Excel date as a String based on the cell's DataFormat.
+ * i.e. "Thursday, January 02, 2003" , "01/02/2003" , "02-Jan" , etc.
+ *
+ * If any conditional format rules apply, the highest priority with a number format is used. If no rules contain a
+ * number format, or no rules apply, the cell's style format is used. If the style does not have a format, the
+ * default date format is applied.
+ *
+ * @param data
+ * to format
+ * @param dataFormat
+ * @param dataFormatString
+ * @return Formatted value
+ */
+ private String getFormattedDateString(Double data, Integer dataFormat, String dataFormatString) {
+ Format dateFormat = getFormat(dataFormat, dataFormatString);
+ if (dateFormat instanceof ExcelStyleDateFormatter) {
+ // Hint about the raw excel value
+ ((ExcelStyleDateFormatter)dateFormat).setDateToBeFormatted(data);
+ }
+ return performDateFormatting(DateUtil.getJavaDate(data, use1904windowing), dateFormat);
+ }
+
+ /**
+ * Returns the formatted value of an Excel number as a String based on the cell's DataFormat.
+ * Supported formats include currency, percents, decimals, phone number, SSN, etc.: "61.54%", "$100.00", "(800)
+ * 555-1234".
+ *
+ * Format comes from either the highest priority conditional format rule with a specified format, or from the cell
+ * style.
+ *
+ * @param data
+ * to format
+ * @param dataFormat
+ * @param dataFormatString
+ * @return a formatted number string
+ */
+ private String getFormattedNumberString(Double data, Integer dataFormat, String dataFormatString) {
+ Format numberFormat = getFormat(dataFormat, dataFormatString);
+ String formatted = numberFormat.format(data);
+ return formatted.replaceFirst("E(\\d)", "E+$1"); // to match Excel's E-notation
+ }
+
+ /**
+ * Format data.
+ *
+ * @param data
+ * @param dataFormat
+ * @param dataFormatString
+ * @return
+ */
+ public String format(Double data, Integer dataFormat, String dataFormatString) {
+ if (DateUtils.isADateFormat(dataFormat, dataFormatString)) {
+ return getFormattedDateString(data, dataFormat, dataFormatString);
+ }
+ return getFormattedNumberString(data, dataFormat, dataFormatString);
+ }
+
+ /**
+ *
+ * Sets a default number format to be used when the Excel format cannot be parsed successfully. Note: This is
+ * a fall back for when an error occurs while parsing an Excel number format pattern. This will not affect cells
+ * with the General format.
+ *
+ *
+ * The value that will be passed to the Format's format method (specified by java.text.Format#format)
+ * will be a double value from a numeric cell. Therefore the code in the format method should expect a
+ * Number value.
+ *
+ *
+ * @param format
+ * A Format instance to be used as a default
+ * @see Format#format
+ */
+ public void setDefaultNumberFormat(Format format) {
+ for (Map.Entry entry : formats.entrySet()) {
+ if (entry.getValue() == defaultNumFormat) {
+ entry.setValue(format);
+ }
+ }
+ defaultNumFormat = format;
+ }
+
+ /**
+ * Adds a new format to the available formats.
+ *
+ * The value that will be passed to the Format's format method (specified by java.text.Format#format)
+ * will be a double value from a numeric cell. Therefore the code in the format method should expect a
+ * Number value.
+ *
+ *
+ * @param excelFormatStr
+ * The data format string
+ * @param format
+ * A Format instance
+ */
+ public void addFormat(String excelFormatStr, Format format) {
+ formats.put(excelFormatStr, format);
+ }
+
+ // Some custom formats
+
+ /**
+ * @return a DecimalFormat with parseIntegerOnly set true
+ */
+ private static DecimalFormat createIntegerOnlyFormat(String fmt) {
+ DecimalFormatSymbols dsf = DecimalFormatSymbols.getInstance(Locale.ROOT);
+ DecimalFormat result = new DecimalFormat(fmt, dsf);
+ result.setParseIntegerOnly(true);
+ return result;
+ }
+
+ /**
+ * Enables excel style rounding mode (round half up) on the Decimal Format given.
+ */
+ public static void setExcelStyleRoundingMode(DecimalFormat format) {
+ setExcelStyleRoundingMode(format, RoundingMode.HALF_UP);
+ }
+
+ /**
+ * Enables custom rounding mode on the given Decimal Format.
+ *
+ * @param format
+ * DecimalFormat
+ * @param roundingMode
+ * RoundingMode
+ */
+ public static void setExcelStyleRoundingMode(DecimalFormat format, RoundingMode roundingMode) {
+ format.setRoundingMode(roundingMode);
+ }
+
+ /**
+ * Format class for Excel's SSN format. This class mimics Excel's built-in SSN formatting.
+ *
+ * @author James May
+ */
+ @SuppressWarnings("serial")
+ private static final class SSNFormat extends Format {
+ private static final DecimalFormat df = createIntegerOnlyFormat("000000000");
+
+ private SSNFormat() {
+ // enforce singleton
+ }
+
+ /** Format a number as an SSN */
+ public static String format(Number num) {
+ String result = df.format(num);
+ return result.substring(0, 3) + '-' + result.substring(3, 5) + '-' + result.substring(5, 9);
+ }
+
+ @Override
+ public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
+ return toAppendTo.append(format((Number)obj));
+ }
+
+ @Override
+ public Object parseObject(String source, ParsePosition pos) {
+ return df.parseObject(source, pos);
+ }
+ }
+
+ /**
+ * Format class for Excel Zip + 4 format. This class mimics Excel's built-in formatting for Zip + 4.
+ *
+ * @author James May
+ */
+ @SuppressWarnings("serial")
+ private static final class ZipPlusFourFormat extends Format {
+ private static final DecimalFormat df = createIntegerOnlyFormat("000000000");
+
+ private ZipPlusFourFormat() {
+ // enforce singleton
+ }
+
+ /** Format a number as Zip + 4 */
+ public static String format(Number num) {
+ String result = df.format(num);
+ return result.substring(0, 5) + '-' + result.substring(5, 9);
+ }
+
+ @Override
+ public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
+ return toAppendTo.append(format((Number)obj));
+ }
+
+ @Override
+ public Object parseObject(String source, ParsePosition pos) {
+ return df.parseObject(source, pos);
+ }
+ }
+
+ /**
+ * Format class for Excel phone number format. This class mimics Excel's built-in phone number formatting.
+ *
+ * @author James May
+ */
+ @SuppressWarnings("serial")
+ private static final class PhoneFormat extends Format {
+ private static final DecimalFormat df = createIntegerOnlyFormat("##########");
+
+ private PhoneFormat() {
+ // enforce singleton
+ }
+
+ /** Format a number as a phone number */
+ public static String format(Number num) {
+ String result = df.format(num);
+ StringBuilder sb = new StringBuilder();
+ String seg1, seg2, seg3;
+ int len = result.length();
+ if (len <= 4) {
+ return result;
+ }
+
+ seg3 = result.substring(len - 4, len);
+ seg2 = result.substring(Math.max(0, len - 7), len - 4);
+ seg1 = result.substring(Math.max(0, len - 10), Math.max(0, len - 7));
+
+ if (seg1.trim().length() > 0) {
+ sb.append('(').append(seg1).append(") ");
+ }
+ if (seg2.trim().length() > 0) {
+ sb.append(seg2).append('-');
+ }
+ sb.append(seg3);
+ return sb.toString();
+ }
+
+ @Override
+ public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
+ return toAppendTo.append(format((Number)obj));
+ }
+
+ @Override
+ public Object parseObject(String source, ParsePosition pos) {
+ return df.parseObject(source, pos);
+ }
+ }
+
+}
diff --git a/src/main/java/com/alibaba/excel/metadata/GlobalConfiguration.java b/src/main/java/com/alibaba/excel/metadata/GlobalConfiguration.java
index 921d70a8..12cc8d31 100644
--- a/src/main/java/com/alibaba/excel/metadata/GlobalConfiguration.java
+++ b/src/main/java/com/alibaba/excel/metadata/GlobalConfiguration.java
@@ -1,5 +1,7 @@
package com.alibaba.excel.metadata;
+import java.util.Locale;
+
/**
* Global configuration
*
@@ -18,6 +20,11 @@ public class GlobalConfiguration {
* @return
*/
private Boolean use1904windowing;
+ /**
+ * A Locale object represents a specific geographical, political, or cultural region. This parameter is
+ * used when formatting dates and numbers.
+ */
+ private Locale locale;
public Boolean getUse1904windowing() {
return use1904windowing;
@@ -34,4 +41,12 @@ public class GlobalConfiguration {
public void setAutoTrim(Boolean autoTrim) {
this.autoTrim = autoTrim;
}
+
+ public Locale getLocale() {
+ return locale;
+ }
+
+ public void setLocale(Locale locale) {
+ this.locale = locale;
+ }
}
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 69211617..782f635a 100644
--- a/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java
+++ b/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java
@@ -17,14 +17,16 @@ import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
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.converters.AutoConverter;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.HeadKindEnum;
import com.alibaba.excel.exception.ExcelCommonException;
import com.alibaba.excel.exception.ExcelGenerateException;
import com.alibaba.excel.metadata.Head;
+import com.alibaba.excel.metadata.Holder;
+import com.alibaba.excel.util.ClassUtils;
import com.alibaba.excel.util.StringUtils;
+import com.alibaba.excel.write.metadata.holder.AbstractWriteHolder;
/**
* Define the header attribute of excel
@@ -46,7 +48,6 @@ public class ExcelHeadProperty {
* The number of rows in the line with the most rows
*/
private int headRowNumber;
-
/**
* Configuration header information
*/
@@ -64,7 +65,7 @@ public class ExcelHeadProperty {
*/
private Map ignoreMap;
- public ExcelHeadProperty(Class headClazz, List> head, Boolean convertAllFiled) {
+ public ExcelHeadProperty(Holder holder, Class headClazz, List> head, Boolean convertAllFiled) {
this.headClazz = headClazz;
headMap = new TreeMap();
contentPropertyMap = new TreeMap();
@@ -73,14 +74,21 @@ public class ExcelHeadProperty {
headKind = HeadKindEnum.NONE;
headRowNumber = 0;
if (head != null && !head.isEmpty()) {
+ int headIndex = 0;
for (int i = 0; i < head.size(); i++) {
- headMap.put(i, new Head(i, null, head.get(i), Boolean.FALSE, Boolean.TRUE));
- contentPropertyMap.put(i, null);
+ if (holder instanceof AbstractWriteHolder) {
+ if (((AbstractWriteHolder)holder).ignore(null, i)) {
+ continue;
+ }
+ }
+ headMap.put(headIndex, new Head(headIndex, null, head.get(i), Boolean.FALSE, Boolean.TRUE));
+ contentPropertyMap.put(headIndex, null);
+ headIndex++;
}
headKind = HeadKindEnum.STRING;
} else {
// convert headClazz to head
- initColumnProperties(convertAllFiled);
+ initColumnProperties(holder, convertAllFiled);
}
initHeadRowNumber();
if (LOGGER.isDebugEnabled()) {
@@ -108,73 +116,49 @@ public class ExcelHeadProperty {
}
}
- private void initColumnProperties(Boolean convertAllFiled) {
+ private void initColumnProperties(Holder holder, Boolean convertAllFiled) {
if (headClazz == null) {
return;
}
- List fieldList = new ArrayList();
- Class tempClass = headClazz;
- // When the parent class is null, it indicates that the parent class (Object class) has reached the top
- // level.
- while (tempClass != null) {
- Collections.addAll(fieldList, tempClass.getDeclaredFields());
- // Get the parent class and give it to yourself
- tempClass = tempClass.getSuperclass();
- }
-
- ExcelIgnoreUnannotated excelIgnoreUnannotated =
- (ExcelIgnoreUnannotated)headClazz.getAnnotation(ExcelIgnoreUnannotated.class);
- // Screening of field
+ // Declared fields
List defaultFieldList = new ArrayList();
Map customFiledMap = new TreeMap();
- 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);
- boolean noExcelProperty = excelProperty == null
- && ((convertAllFiled != null && !convertAllFiled) || excelIgnoreUnannotated != null);
- if (noExcelProperty) {
- ignoreMap.put(field.getName(), field);
- continue;
- }
- boolean isStaticFinalOrTransient =
- (Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers()))
- || Modifier.isTransient(field.getModifiers());
- if (excelProperty == null && isStaticFinalOrTransient) {
- ignoreMap.put(field.getName(), field);
- continue;
- }
- if (excelProperty == null || excelProperty.index() < 0) {
- defaultFieldList.add(field);
- continue;
- }
- if (customFiledMap.containsKey(excelProperty.index())) {
- throw new ExcelGenerateException("The index of '" + customFiledMap.get(excelProperty.index()).getName()
- + "' and '" + field.getName() + "' must be inconsistent");
- }
- customFiledMap.put(excelProperty.index(), field);
- }
+ ClassUtils.declaredFields(headClazz, defaultFieldList, customFiledMap, ignoreMap, convertAllFiled);
int index = 0;
for (Field field : defaultFieldList) {
while (customFiledMap.containsKey(index)) {
- initOneColumnProperty(index, customFiledMap.get(index), Boolean.TRUE);
+ Field customFiled = customFiledMap.get(index);
customFiledMap.remove(index);
+ if (!initOneColumnProperty(holder, index, customFiled, Boolean.TRUE)) {
+ index++;
+ }
+ }
+ if (!initOneColumnProperty(holder, index, field, Boolean.FALSE)) {
index++;
}
- initOneColumnProperty(index, field, Boolean.FALSE);
- index++;
}
for (Map.Entry entry : customFiledMap.entrySet()) {
- initOneColumnProperty(entry.getKey(), entry.getValue(), Boolean.TRUE);
+ initOneColumnProperty(holder, entry.getKey(), entry.getValue(), Boolean.TRUE);
}
headKind = HeadKindEnum.CLASS;
}
- private void initOneColumnProperty(int index, Field field, Boolean forceIndex) {
+ /**
+ * Initialization column property
+ *
+ * @param holder
+ * @param index
+ * @param field
+ * @param forceIndex
+ * @return Ignore current field
+ */
+ private boolean initOneColumnProperty(Holder holder, int index, Field field, Boolean forceIndex) {
+ if (holder instanceof AbstractWriteHolder) {
+ if (((AbstractWriteHolder)holder).ignore(field.getName(), index)) {
+ return true;
+ }
+ }
ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
List tmpHeadList = new ArrayList();
boolean notForceName = excelProperty == null || excelProperty.value().length <= 0
@@ -206,6 +190,7 @@ public class ExcelHeadProperty {
headMap.put(index, head);
contentPropertyMap.put(index, excelContentProperty);
fieldNameContentPropertyMap.put(field.getName(), excelContentProperty);
+ return false;
}
public Class getHeadClazz() {
diff --git a/src/main/java/com/alibaba/excel/read/builder/AbstractExcelReaderParameterBuilder.java b/src/main/java/com/alibaba/excel/read/builder/AbstractExcelReaderParameterBuilder.java
new file mode 100644
index 00000000..4e5e3702
--- /dev/null
+++ b/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 extends AbstractParameterBuilder {
+ /**
+ * Count the number of added heads when read sheet.
+ *
+ *
+ * 0 - This Sheet has no head ,since the first row are the data
+ *
+ * 1 - This Sheet has one row head , this is the default
+ *
+ * 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());
+ }
+ parameter().getCustomReadListenerList().add(readListener);
+ return self();
+ }
+}
diff --git a/src/main/java/com/alibaba/excel/read/builder/ExcelReaderBuilder.java b/src/main/java/com/alibaba/excel/read/builder/ExcelReaderBuilder.java
index 340548b9..ae3b79c1 100644
--- a/src/main/java/com/alibaba/excel/read/builder/ExcelReaderBuilder.java
+++ b/src/main/java/com/alibaba/excel/read/builder/ExcelReaderBuilder.java
@@ -2,15 +2,15 @@ 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;
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.ModelBuildEventListener;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.read.metadata.ReadWorkbook;
import com.alibaba.excel.support.ExcelTypeEnum;
@@ -20,7 +20,7 @@ import com.alibaba.excel.support.ExcelTypeEnum;
*
* @author Jiaju Zhuang
*/
-public class ExcelReaderBuilder {
+public class ExcelReaderBuilder extends AbstractExcelReaderParameterBuilder {
/**
* Workbook
*/
@@ -131,105 +131,43 @@ public class ExcelReaderBuilder {
}
/**
- * Count the number of added heads when read sheet.
- *
- *
- * 0 - This Sheet has no head ,since the first row are the data
- *
- * 1 - This Sheet has one row head , this is the default
- *
- * 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> 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());
- }
- 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());
- }
- readWorkbook.getCustomReadListenerList().add(readListener);
- return this;
- }
-
- /**
- * true if date uses 1904 windowing, or false if using 1900 date windowing.
- *
- * default is false
+ * Whether the encryption
*
- * @param use1904windowing
+ * @param password
* @return
*/
- public ExcelReaderBuilder use1904windowing(Boolean use1904windowing) {
- readWorkbook.setUse1904windowing(use1904windowing);
+ public ExcelReaderBuilder password(String password) {
+ readWorkbook.setPassword(password);
return this;
}
/**
- * Automatic trim includes sheet name and content
+ * SAXParserFactory used when reading xlsx.
+ *
+ * The default will automatically find.
+ *
+ * Please pass in the name of a class ,like : "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl"
*
- * @param autoTrim
+ * @see SAXParserFactory#newInstance()
+ * @see SAXParserFactory#newInstance(String, ClassLoader)
+ * @param xlsxSAXParserFactoryName
* @return
*/
- public ExcelReaderBuilder autoTrim(Boolean autoTrim) {
- readWorkbook.setAutoTrim(autoTrim);
+ public ExcelReaderBuilder xlsxSAXParserFactoryName(String xlsxSAXParserFactoryName) {
+ readWorkbook.setXlsxSAXParserFactoryName(xlsxSAXParserFactoryName);
return this;
}
/**
- * Whether the encryption
+ * Whether to use the default listener, which is used by default.
+ *
+ * The {@link ModelBuildEventListener} is loaded by default to convert the object.
*
- * @param password
+ * @param useDefaultListener
* @return
*/
- public ExcelReaderBuilder password(String password) {
- readWorkbook.setPassword(password);
+ public ExcelReaderBuilder useDefaultListener(Boolean useDefaultListener) {
+ readWorkbook.setUseDefaultListener(useDefaultListener);
return this;
}
@@ -266,4 +204,9 @@ public class ExcelReaderBuilder {
}
return excelReaderSheetBuilder;
}
+
+ @Override
+ protected ReadWorkbook parameter() {
+ return readWorkbook;
+ }
}
diff --git a/src/main/java/com/alibaba/excel/read/builder/ExcelReaderSheetBuilder.java b/src/main/java/com/alibaba/excel/read/builder/ExcelReaderSheetBuilder.java
index af3b05e1..84ec6bae 100644
--- a/src/main/java/com/alibaba/excel/read/builder/ExcelReaderSheetBuilder.java
+++ b/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 {
private ExcelReader excelReader;
/**
* Sheet
@@ -54,98 +51,6 @@ public class ExcelReaderSheetBuilder {
return this;
}
- /**
- * Count the number of added heads when read sheet.
- *
- *
- * 0 - This Sheet has no head ,since the first row are the data
- *
- * 1 - This Sheet has one row head , this is the default
- *
- * 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> 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());
- }
- 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());
- }
- 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;
}
@@ -166,7 +71,7 @@ public class ExcelReaderSheetBuilder {
*
* @return
*/
- public List