JAVA解析Excel工具easyexcel之深入源代码解密原理

JAVA解析Excel⼯具easyexcel之深⼊源代码解密原理
创建ExcelReaderBuilder并将三个参数保存到ExcelReaderBuilder中的⼀个属性ReadWorkbook中。
public static ExcelReaderBuilder read(File file, Class head, ReadListener readListener) {
//创建excel读构建器
ExcelReaderBuilder excelReaderBuilder = new ExcelReaderBuilder();
//分别传⼊file、head、readListener
excelReaderBuilder.file(file);
if (head != null) {
excelReaderBuilder.head(head);
}
if (readListener != null) {
}
return excelReaderBuilder;
}
public class ExcelReaderBuilder {
/**
* Workbook
*/
private ReadWorkbook readWorkbook;
public ExcelReaderBuilder() {
zipa
}
袖阀管
ReadWorkbook extends ReadBasicParameter
public ExcelReaderSheetBuilder sheet(Integer sheetNo) {
return sheet(sheetNo, null);
}
sheet(0)返回 excelReaderSheetBuilder构建器
public ExcelReaderSheetBuilder sheet(Integer sheetNo, String sheetName) {
ExcelReaderSheetBuilder excelReaderSheetBuilder = new ExcelReaderSheetBuilder(build());
if (sheetNo != null) {
excelReaderSheetBuilder.sheetNo(sheetNo);
}
if (sheetName != null) {
excelReaderSheetBuilder.sheetName(sheetName);
}
return excelReaderSheetBuilder;
}
⽽build()就是利⽤⽂件、表头类、读返回⼀个ExcelReader,readWorkbook中保存了⽂件、表头类、读。
public ExcelReader build() {
return new ExcelReader(readWorkbook);
}
ExcelReader
public ExcelReader(ReadWorkbook readWorkbook) {
excelAnalyser = new ExcelAnalyserImpl(readWorkbook);
}
ExcelAnalyserImpl
public ExcelAnalyserImpl(ReadWorkbook readWorkbook) {
try {
analysisContext = new AnalysisContextImpl(readWorkbook);
choiceExcelExecutor();
} catch (RuntimeException e) {
finish();
throw e;
} catch (Throwable e) {
finish();
throw new ExcelAnalysisException(e);
}
}
//选择不同的excel执⾏器,XLS、XLSX
private void choiceExcelExecutor() throws Exception {
ReadWorkbookHolder readWorkbookHolder = adWorkbookHolder();
ExcelTypeEnum excelType = ExcelType();
if (excelType == null) {
excelReadExecutor = new XlsxSaxAnalyser(analysisContext, null);
return;
}
switch (excelType) {
case XLS:
**//POIFSFileSystem poi的⽂件读取api
POIFSFileSystem poifsFileSystem;**
if (File() != null) {
poifsFileSystem = new File());
} else {
poifsFileSystem = new InputStream());
}
// So in encrypted excel, it looks like XLS but it's actually XLSX
if (Root().hasEntry(Decryptor.DEFAULT_POIFS_ENTRY)) {
InputStream decryptedStream = null;
try {
decryptedStream =
excelReadExecutor = new XlsxSaxAnalyser(analysisContext, decryptedStream);
return;
} finally {
IOUtils.closeQuietly(decryptedStream);
// as we processed the full stream already, we can close the filesystem here
柔道教学>裳凤蝶// otherwise file handles are leaked
poifsFileSystem.close();
}
}
if (adWorkbookHolder().getPassword() != null) {
Biff8EncryptionKey.adWorkbookHolder().getPassword());                }
excelReadExecutor = new XlsSaxAnalyser(analysisContext, poifsFileSystem);
break;
case XLSX:
excelReadExecutor = new XlsxSaxAnalyser(analysisContext, null);
break;
default:
}
}
promaxAnalysisContextImpl
if (readWorkbook == null) {
throw new IllegalArgumentException("Workbook argument cannot be null");        }
readWorkbookHolder = new ReadWorkbookHolder(readWorkbook);
currentReadHolder = readWorkbookHolder;
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Initialization 'AnalysisContextImpl' complete");
}
}
ReadWorkbookHolder开始读取readWorkbook中传进来的属性。
super(readWorkbook, null, ConvertAllFiled());
if (InputStream() != null) {
if (InputStream().markSupported()) {诺维乔克
this.inputStream = InputStream();
} else {
this.inputStream = new InputStream());
}
}
this.file = File();
if (file == null && inputStream == null) {
throw new ExcelAnalysisException("File and inputStream must be a non-null.");
}
if (MandatoryUseInputStream() == null) {
this.mandatoryUseInputStream = Boolean.FALSE;
} else {
this.mandatoryUseInputStream = MandatoryUseInputStream();
}
if (AutoCloseStream() == null) {
this.autoCloseStream = Boolean.TRUE;
} else {
this.autoCloseStream = AutoCloseStream();
}
// The type of excel is read according to the judgment.Because encrypted XLSX needs to be specified as XLS to        // properly parse.
if (ExcelTypeEnum.XLS == excelType && getGlobalConfiguration().getUse1904windowing() == null) {
getGlobalConfiguration().setUse1904windowing(Boolean.FALSE);
}
this.customObject = CustomObject();
if (IgnoreEmptyRow() == null) {
this.ignoreEmptyRow = Boolean.TRUE;
} else {
this.ignoreEmptyRow = IgnoreEmptyRow();
}
if (ReadCache() != null) {
if (ReadCacheSelector() != null) {
throw new ExcelAnalysisException("'readCache' and 'readCacheSelector' only one choice.");
}
} else {
if (ReadCacheSelector() == null) {
} else {
}
}
if (DefaultReturnMap() == null) {
this.defaultReturnMap = Boolean.TRUE;
} else {
this.defaultReturnMap = DefaultReturnMap();
}
this.xlsxSAXParserFactoryName = XlsxSAXParserFactoryName();
this.hasReadSheet = new HashSet<Integer>();
this.ignoreRecord03 = Boolean.FALSE;
this.password = Password();
}
以上便是初始化创建环境,下⾯开始读取⽂档内容:
public void doRead() {
if (excelReader == null) {
throw new ExcelGenerateException("Must use 'ad().sheet()' to call this method");        }
excelReader.finish();
}
public ReadSheet build() {
//在前⾯环境构建的时候设置了readSheet对象,关于sheet的序号,名称
return readSheet;
}
调⽤excelReader的read
public ExcelReader readSheet) {
return read(Arrays.asList(readSheet));
}
public ExcelReader read(List<ReadSheet> readSheetList) {
excelAnalyser.analysis(readSheetList, Boolean.FALSE);
return this;
}
核⼼读取步骤:
@Override
public void analysis(List<ReadSheet> readSheetList, Boolean readAll) {
try {
if (!readAll && CollectionUtils.isEmpty(readSheetList)) {
throw new IllegalArgumentException("Specify at least one read sheet.");
}
try {
//根据创建环境时选择的excelReadExecutor进⾏读取
} catch (ExcelAnalysisStopException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Custom stop!");
}
}
// The last sheet is read
if (excelReadExecutor instanceof XlsSaxAnalyser) {
if (adSheetHolder() != null) {
}
}
} catch (RuntimeException e) {
finish();
throw e;
} catch (Throwable e) {
finish();
throw new ExcelAnalysisException(e);
}
}

本文发布于:2024-09-21 20:43:05,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/423868.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:环境   创建   读取   监听器   开始
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议