matrix.boot.common.converter.ExcelColumnConvert Maven / Gradle / Ivy
package matrix.boot.common.converter;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import matrix.boot.common.annotation.ExcelColumn;
import matrix.boot.common.exception.ServiceException;
import matrix.boot.common.utils.AssertUtil;
import matrix.boot.common.utils.ReflectUtil;
import matrix.boot.common.utils.StringUtil;
import matrix.boot.common.dto.ExcelColumnDto;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;
import java.lang.reflect.Field;
import java.util.*;
/**
* excel列转换器
*
* @author wangcheng
* 2020/7/17
**/
public class ExcelColumnConvert {
public static void main(String[] args) {
ExcelColumnDto excelColumnDto = new ExcelColumnDto();
System.out.println(excelColumnDto.getClass().getSuperclass());
}
/**
* bean类型转换
*
* @param items 数据
* @return 导出的excel数据
*/
public static List> convertForBean(List> items) {
List> result = new ArrayList<>();
for (Object item : items) {
//创建行
List row = new ArrayList<>();
List fields = ReflectUtil.getClassDeclaredFields(item.getClass());
AssertUtil.state(!fields.isEmpty(), "field size = 0");
for (Field field : fields) {
try {
field.setAccessible(true);
ExcelColumn annotation = field.getDeclaredAnnotation(ExcelColumn.class);
if (annotation == null) {
continue;
}
//创建列
ExcelColumnDto column = new ExcelColumnDto();
//设置列名
column.setName(StringUtil.notEmpty(annotation.value()) ? annotation.value() : field.getName());
//设置值
column.setValue(field.get(item));
//设置宽度
column.setWidth(annotation.width());
//设置类型
column.setType(field.getType());
row.add(column);
} catch (Exception e) {
//无需操作
}
}
result.add(row);
}
return result;
}
/**
* map类型转换
*
* @param items 数据
* @return 导出的excel数据
*/
public static List> convertForMap(List> items) {
List> result = new ArrayList<>();
for (LinkedHashMap item : items) {
//创建行
List row = new ArrayList<>();
for (String name : item.keySet()) {
//创建列
ExcelColumnDto column = new ExcelColumnDto();
//设置列名
column.setName(name);
//设置值
column.setValue(item.get(name));
//设置类型
column.setType(item.get(name) == null ? String.class : item.get(name).getClass());
row.add(column);
}
result.add(row);
}
return result;
}
/**
* 转换为列值
*
* @param cell excel cell
* @return cell value
*/
public static Object convertCellValue(Cell cell) {
if (cell == null) {
return null;
}
String cellType = cell.getCellType().name();
if (CellType._NONE.name().equals(cellType)) {
return null;
}
if (CellType.NUMERIC.name().equals(cellType)) {
if (DateUtil.isCellDateFormatted(cell)) {
return cell.getDateCellValue();
} else {
return cell.getNumericCellValue();
}
}
if (CellType.STRING.name().equals(cellType)) {
return cell.getStringCellValue();
}
if (CellType.FORMULA.name().equals(cellType)) {
throw new ServiceException(String.format("excel parse error not support formula %s by row:%d cell:%d", cell.getCellFormula(), cell.getRowIndex() + 1, cell.getColumnIndex() + 1));
}
if (CellType.BLANK.name().equals(cellType)) {
return "";
}
if (CellType.BOOLEAN.name().equals(cellType)) {
return cell.getBooleanCellValue();
}
if (CellType.ERROR.name().equals(cellType)) {
throw new ServiceException("excel import error code: " + cell.getErrorCellValue());
}
return cell.getStringCellValue();
}
/**
* 将json转换为泛型的值
*
* @param jsonObject json对象
* @param clazz 泛型
* @return new 泛型对象
*/
public static T convertJsonToGeneric(JSONObject jsonObject, Class clazz) {
try {
if (Map.class.isAssignableFrom(clazz)) {
return JSONObject.parseObject(JSONObject.toJSONString(jsonObject, SerializerFeature.WriteMapNullValue), clazz, SerializerFeature.WRITE_MAP_NULL_FEATURES);
} else {
//实体模式
T t = clazz.newInstance();
List fields = ReflectUtil.getClassDeclaredFields(t.getClass());
for (Field field : fields) {
field.setAccessible(true);
ExcelColumn annotation = field.getDeclaredAnnotation(ExcelColumn.class);
if (annotation == null) {
continue;
}
try {
String key = StringUtil.notEmpty(annotation.value()) ? annotation.value() : field.getName();
if (Date.class.equals(field.getType())) {
field.set(t, jsonObject.getDate(key));
} else if (Double.class.equals(field.getType())) {
field.set(t, jsonObject.getDoubleValue(key));
} else if (Boolean.class.equals(field.getType())) {
field.set(t, jsonObject.getBooleanValue(key));
} else if (Integer.class.equals(field.getType())) {
field.set(t, jsonObject.getInteger(key));
} else {
field.set(t, jsonObject.getString(key));
}
} catch (Exception e) {
//无需操作
}
}
return t;
}
} catch (Exception e) {
throw new ServiceException(e);
}
}
}