com.github.kunalk16.excel.factory.WorkBookFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lightExcelReader Show documentation
Show all versions of lightExcelReader Show documentation
A lightweight Java framework to read .xlsx excel files.
package com.github.kunalk16.excel.factory;
import com.github.kunalk16.excel.factory.extractor.SheetByNameExtractor;
import com.github.kunalk16.excel.factory.extractor.SheetNamesExtractor;
import com.github.kunalk16.excel.file.validation.ExcelFileValidation;
import com.github.kunalk16.excel.model.factory.ExcelArchiveFiles;
import com.github.kunalk16.excel.model.factory.ExcelWorkBook;
import com.github.kunalk16.excel.model.factory.ExcelXMLData;
import com.github.kunalk16.excel.model.user.Sheet;
import com.github.kunalk16.excel.model.user.WorkBook;
import com.github.kunalk16.excel.utils.logger.ExcelReaderLogger;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.IntStream;
public class WorkBookFactory {
public static WorkBook create(String filePath) {
if (ExcelFileValidation.validate(filePath)) {
return getWorkBookData(new File(filePath));
}
return null;
}
private static WorkBook getWorkBookData(File excelFile) {
try (ExcelArchiveFiles excelArchiveFiles = ArchiveFilesFactory.create(excelFile)) {
ExcelXMLData excelXMLData = XMLDataFactory.create(excelArchiveFiles);
return getWorkBookData(excelXMLData);
} catch (Exception exception) {
ExcelReaderLogger.getInstance().severe("Excel Resources not handles properly!" + exception.getLocalizedMessage());
return null;
}
}
private static WorkBook getWorkBookData(ExcelXMLData excelXMLData) {
return Optional.ofNullable(excelXMLData)
.map(WorkBookFactory::getSheets)
.map(new SheetByNameExtractor())
.map(ExcelWorkBook::new)
.orElse(null);
}
private static List getSheets(ExcelXMLData excelXMLData) {
List sheetNames = new SheetNamesExtractor().apply(excelXMLData.getWorkBook());
List sheets = new ArrayList<>();
IntStream.range(0, sheetNames.size())
.mapToObj(index -> SheetFactory.create(excelXMLData.getSharedString(),
excelXMLData.getSheets().get(index), sheetNames.get(index)))
.forEach(sheets::add);
return sheets;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy