
com.github.houbb.iexcel.hutool.support.write.DefaultBeanToMapWriter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of iexcel-hutool Show documentation
Show all versions of iexcel-hutool Show documentation
Excel read and write tool based on hutool-poi.
package com.github.houbb.iexcel.hutool.support.write;
import cn.hutool.core.collection.CollectionUtil;
import com.github.houbb.heaven.util.lang.reflect.ClassUtil;
import com.github.houbb.iexcel.hutool.annotation.ExcelField;
import com.github.houbb.iexcel.hutool.exception.ExcelException;
import java.lang.reflect.Field;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* 默认实现
*/
public class DefaultBeanToMapWriter implements BeanToMapWriter {
@Override
public Map beanToMap(Object object) {
try {
Map resultMap = new LinkedHashMap<>();
if(object == null) {
return resultMap;
}
Class> tClass = object.getClass();
List fields = ClassUtil.getAllFieldList(tClass);
if(CollectionUtil.isEmpty(fields)) {
return resultMap;
}
for(Field field : fields) {
ExcelField excelField = field.getAnnotation(ExcelField.class);
if(excelField == null) {
continue;
}
if(!excelField.writeRequire()) {
continue;
}
String headerName = excelField.headerName();
field.setAccessible(true);
// 添加 converter
Object value = field.get(object);
resultMap.put(headerName, value);
}
return resultMap;
} catch (Exception e) {
throw new ExcelException(e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy