com.github.kunalk16.excel.objectmapper.ExcelObjectFromSheetMapper 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.
The newest version!
package com.github.kunalk16.excel.objectmapper;
import com.github.kunalk16.excel.model.user.Row;
import com.github.kunalk16.excel.objectmapper.annotations.ExcelColumn;
import com.github.kunalk16.excel.objectmapper.extractor.ColumnByHeaderRowValueExtractor;
import com.github.kunalk16.excel.objectmapper.extractor.FieldsFromClassExtractor;
import com.github.kunalk16.excel.objectmapper.extractor.ObjectFromRowExtractor;
import java.lang.reflect.Field;
import java.util.*;
import java.util.function.Function;
public class ExcelObjectFromSheetMapper {
static List createObjectFromSheet(Class modelClass, Iterator rowIterator) {
if (rowIterator.hasNext()) {
Map fieldsFromClass = new FieldsFromClassExtractor<>(ExcelColumn.class).apply(modelClass);
if (fieldsFromClass.isEmpty()) {
return Collections.emptyList();
}
Map columnByHeaderRowValue = new ColumnByHeaderRowValueExtractor().apply(rowIterator.next());
if (columnByHeaderRowValue.isEmpty()) {
return Collections.emptyList();
}
if (!rowIterator.hasNext()) {
return Collections.emptyList();
}
List modelRows = new ArrayList<>();
Function objectFromRowExtractor = new ObjectFromRowExtractor<>(modelClass,
fieldsFromClass, columnByHeaderRowValue);
while (rowIterator.hasNext()) {
Optional.ofNullable(rowIterator.next())
.map(objectFromRowExtractor)
.ifPresent(modelRows::add);
}
return Collections.unmodifiableList(modelRows);
}
return Collections.emptyList();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy