com.github.bingoohuang.excel2beans.ExcelBeanFieldParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of excel2javabeans Show documentation
Show all versions of excel2javabeans Show documentation
a little utility to convert excel rows to java beans
package com.github.bingoohuang.excel2beans;
import com.github.bingoohuang.excel2beans.annotations.ExcelColAlign;
import com.github.bingoohuang.excel2beans.annotations.ExcelColIgnore;
import com.github.bingoohuang.excel2beans.annotations.ExcelColStyle;
import com.github.bingoohuang.utils.reflect.Fields;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Sheet;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@Slf4j
public class ExcelBeanFieldParser {
private final Sheet sheet;
private final Field[] declaredFields;
public ExcelBeanFieldParser(Class> beanClass, Sheet sheet) {
this.sheet = sheet;
this.declaredFields = beanClass.getDeclaredFields();
}
public List parseBeanFields(Set includedFields, ReflectAsmCache reflectAsmCache) {
List beanFields = new ArrayList<>(declaredFields.length);
for (val field : declaredFields) {
processField(field, beanFields, includedFields, reflectAsmCache);
}
return filterTitledFields(beanFields);
}
private List filterTitledFields(List beanFields) {
val titledFields = beanFields.stream().filter(ExcelBeanField::hasTitle).collect(Collectors.toList());
if (titledFields.isEmpty()) return beanFields;
if (log.isDebugEnabled()) {
beanFields.stream().filter(x -> !x.hasTitle())
.forEach(x -> log.debug("ignore field {} without @ExcelColTitle", x.getField()));
}
return titledFields;
}
@SuppressWarnings("unchecked")
private void processField(
Field field, List fields,
Set includedFields,
ReflectAsmCache reflectAsmCache) {
if (Fields.shouldIgnored(field, ExcelColIgnore.class)) return;
if (includedFields != null && !includedFields.contains(field.getName())) return;
val bf = new ExcelBeanField(field, fields.size(), reflectAsmCache);
if (bf.isElementTypeSupported()) {
setStyle(field, bf);
fields.add(bf);
} else {
log.debug("bean field {} was ignored by unsupported type {}",
field, bf.getElementType());
}
}
private void setStyle(Field field, ExcelBeanField beanField) {
val colStyle = field.getAnnotation(ExcelColStyle.class);
if (colStyle == null) return;
beanField.setCellStyle(createAlign(colStyle));
}
private CellStyle createAlign(ExcelColStyle colStyle) {
val style = sheet.getWorkbook().createCellStyle();
style.setAlignment(convertAlign(colStyle.align(), style.getAlignmentEnum()));
return style;
}
private HorizontalAlignment convertAlign(ExcelColAlign align, HorizontalAlignment defaultAlign) {
switch (align) {
case LEFT:
return HorizontalAlignment.LEFT;
case CENTER:
return HorizontalAlignment.CENTER;
case RIGHT:
return HorizontalAlignment.RIGHT;
}
return defaultAlign;
}
}