package com.alibaba.excel.read.builder; import java.io.File; import java.io.InputStream; import java.util.ArrayList; import java.util.List; 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; /** * Build ExcelWriter * * @author Jiaju Zhuang */ public class ExcelReaderBuilder { /** * Workbook */ private ReadWorkbook readWorkbook; public ExcelReaderBuilder() { this.readWorkbook = new ReadWorkbook(); } public ExcelReaderBuilder excelType(ExcelTypeEnum excelType) { readWorkbook.setExcelType(excelType); return this; } /** * Read InputStream *
* If 'inputStream' and 'file' all not empty,file first */ public ExcelReaderBuilder file(InputStream inputStream) { readWorkbook.setInputStream(inputStream); return this; } /** * Read file *
* If 'inputStream' and 'file' all not empty,file first */ public ExcelReaderBuilder file(File file) { readWorkbook.setFile(file); return this; } /** * Read file *
* If 'inputStream' and 'file' all not empty,file first */ public ExcelReaderBuilder file(String pathName) { return file(new File(pathName)); } /** * Mandatory use 'inputStream' .Default is false. *
* if false,Will transfer 'inputStream' to temporary files to improve efficiency */ public ExcelReaderBuilder mandatoryUseInputStream(Boolean mandatoryUseInputStream) { readWorkbook.setMandatoryUseInputStream(mandatoryUseInputStream); return this; } /** * Default true * * @param autoCloseStream * @return */ public ExcelReaderBuilder autoCloseStream(Boolean autoCloseStream) { readWorkbook.setAutoCloseStream(autoCloseStream); return this; } /** * Ignore empty rows.Default is true. * * @param ignoreEmptyRow * @return */ public ExcelReaderBuilder ignoreEmptyRow(Boolean ignoreEmptyRow) { readWorkbook.setIgnoreEmptyRow(ignoreEmptyRow); return this; } /** * This object can be read in the Listener {@link AnalysisEventListener#invoke(Object, AnalysisContext)} * {@link AnalysisContext#getCustom()} * * @param customObject * @return */ public ExcelReaderBuilder customObject(Object customObject) { readWorkbook.setCustomObject(customObject); return this; } /** * A cache that stores temp data to save memory. * * @param readCache * @return */ public ExcelReaderBuilder readCache(ReadCache readCache) { readWorkbook.setReadCache(readCache); return this; } /** * Select the cache.Default use {@link com.alibaba.excel.cache.selector.SimpleReadCacheSelector} * * @param readCacheSelector * @return */ public ExcelReaderBuilder readCacheSelector(ReadCacheSelector readCacheSelector) { readWorkbook.setReadCacheSelector(readCacheSelector); 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 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