Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
* By annotated class type:
* use method {@code fromAnnotatedClass()} or {@code fromAnnotatedObject()} to
* build items from an annotated (@{@link Mapping}) class or it's instance object.
*
* By API dynamically:
* use method {@code itemBuilder()} create a new {@link MetaItemBuilder} to build items.
* use method {@code sheet()} to indicate a sheet by name or index.
* use method {@code items()} to collect all items from {@link MetaItemBuilder} to current sheet in builder.
* use method {@code handler()} with a {@link Consumer} to access a POI sheet object directly.
*
*
* @author swiftech
* @see MetaItemBuilder
* @see SheetMeta
* @see Mapping
*/
public class SheetMetaBuilder {
private final Logger log = LoggerFactory.getLogger(SheetMetaBuilder.class);
/**
* current sheet id
*/
private SheetId sheetId;
/**
* mapping of sheet id to items.
*/
private final MetaMap metaMap = new MetaMap();
/**
* Class fields of annotated class.
*/
private List fields;
/**
* Identity a sheet by index.
*
* @param index
* @return
*/
public SheetMetaBuilder sheet(int index) {
sheetId = new SheetId();
sheetId.setSheetIndex(index);
return this;
}
/**
* Identity a sheet by name.
*
* @param name Should follow the Excel's sheet name rule, if not, it will be converted by force.
* @return
*/
public SheetMetaBuilder sheet(String name) {
sheetId = new SheetId();
sheetId.setSheetName(name);
return this;
}
/**
* Identity a sheet by index or name.
* The index has higher priority than the name.
*
* @param index
* @param name Should follow the Excel's sheet name rule, if not, it will be converted by force.
* @return
*/
public SheetMetaBuilder sheet(int index, String name) {
sheetId = new SheetId();
sheetId.setSheetIndex(index);
sheetId.setSheetName(name);
return this;
}
/**
* Specify a handler to access a sheet directly. Only works for Excel.
*
* @param sheetHandler
* @return
*/
public SheetMetaBuilder handler(Consumer extends SheetInfo> sheetHandler) {
if (sheetId == null) {
sheetId = SheetId.DEFAULT_SHEET; // be the first sheet (index is 0 and name is 'Sheet 1') if not provides
}
this.metaMap.setSheetHandler(this.sheetId, sheetHandler);
return this;
}
/**
* Create a new {@link MetaItemBuilder}.
*
* @return
*/
public MetaItemBuilder itemBuilder() {
return new MetaItemBuilder();
}
/**
* Append valid meta items which is built from {@link MetaItemBuilder}.
* If {@link SheetId} has not been provided, a default one with sheetIndex=0 and sheetName="Sheet 1" will be used.
* The non-null {@link SheetId} in meta item overrides current {@link SheetId}.
*
* @param itemBuilder
* @return
*/
public SheetMetaBuilder items(MetaItemBuilder itemBuilder) {
List items = itemBuilder.build();
if (sheetId == null) {
sheetId = SheetId.DEFAULT_SHEET; // be the first sheet (names 'Sheet 1') if not provides
}
for (MetaItem item : items) {
if (item == null) {
log.warn("One meta item is null, ignored");
continue;
}
if (item.getArea() == null) {
log.warn(String.format("Meta item's area is null: %s, ignored", item.getKey()));
continue;
}
if (item.getArea().getSheetId() == null) {
item.getArea().setSheetId(sheetId);
}
metaMap.addItem(item.getArea().getSheetId(), item);
}
return this;
}
/**
* Load meta items from an object which has fields with mapping annotation @{@link Mapping}.
* Should be used for export.
*
* @param object
* @return
*/
public SheetMetaBuilder fromAnnotatedObject(Object object) {
List fields = BeanUtils.getDeclaredFieldsByAnnotation(object.getClass(), Mapping.class);
MetaItemBuilder itemBuilder = itemBuilder();
fields.forEach(field -> {
Mapping annotation = field.getAnnotation(Mapping.class);
String expression = annotation.value();
if (StringUtils.isBlank(expression)) {
return; // Ignore
}
Object value = BeanUtils.forceGetProperty(object, field);
itemBuilder.newItem().key(field.getName()).parse(expression).value(value);
});
this.items(itemBuilder);
return this;
}
/**
* Init meta items from class which has fields with mapping annotation @{@link Mapping}.
* Should be used for import.
*
* @param clazz
* @return
*/
public SheetMetaBuilder fromAnnotatedClass(Class> clazz) {
List fields = BeanUtils.getDeclaredFieldsByAnnotation(clazz, Mapping.class);
MetaItemBuilder itemBuilder = itemBuilder();
fields.forEach(field -> {
Mapping annotation = field.getAnnotation(Mapping.class);
String expression = annotation.value();
if (StringUtils.isBlank(expression)) {
return;// Ignore
}
itemBuilder.newItem().key(field.getName()).parse(expression);
});
this.items(itemBuilder);
this.fields = fields;
return this;
}
/**
* Enable reading images from Excel sheets.
*
* @return
*/
public SheetMetaBuilder withImages() {
this.metaMap.setWithImages(true);
return this;
}
/**
* Converters to convert image data form Excel sheets into any format you want.
*
* @param converter
* @return
*/
public SheetMetaBuilder imageConverter(Function