1.实体对象TestModel
import cn.afterturn.easypoi.excel.annotation.Excel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
public class TestModel{
@Excel(name = "编号", orderNum = "1", width = 10)
@ApiModelProperty(value = "编号")
private String no;
@Excel(name = "标题", orderNum = "2", width = 50)
@ApiModelProperty(value = "标题")
private String title;
@Excel(name = "公开范围", dict = "is_public", orderNum = "3", width = 10)
@ApiModelProperty(value = "公开范围")
private String publicScope;
}
工具类ExcelPoiUtil
- getExportHeads 获取自定义表头所有字段【给前端自定义选择用的】
- ①notExportFieldNames:预留了特殊情况下不需要导出的属性剔除,不需要该功能的,可将其删除即可,不影响。
- ②exportFieldNames:一开始是打算处理前端返回来的结果,后面加了getExportEntitys去处理,去掉也不影响。
- getExportEntitys 根据需要导出的字段生成ExcelExportEntity数组(这里去拿原来实体对象的排序,数据字典,还有宽度)
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ExcelPoiUtil {
public static List<ExcelExportEntity> getExportEntitys(Class tClass, List<String> exportFieldNames, List<String> notExportFieldNames) {
List<ExcelExportEntity> results = new ArrayList<>();
boolean allExport = (null != exportFieldNames && !exportFieldNames.isEmpty())?false:true;
boolean exitsNotExport = (null != notExportFieldNames && !notExportFieldNames.isEmpty())?true:false;
Field[] fields = tClass.getDeclaredFields();
Map<String, String> resultMap = new HashMap();
for (Field field : fields) {
System.out.println(field.getName());
if (field.isAnnotationPresent(Excel.class)) {
Field f = ExcelPoiUtil.getFieldOfCheck(field, allExport, exitsNotExport, exportFieldNames, notExportFieldNames);
if (null != f) {
Excel annotation = f.getAnnotation(Excel.class);
ExcelExportEntity excelExportEntity = new ExcelExportEntity(annotation.name(),field.getName());
excelExportEntity.setOrderNum(Integer.parseInt(annotation.orderNum()));
excelExportEntity.setWidth((double)annotation.width());
excelExportEntity.setDict(annotation.dict());
results.add(excelExportEntity);
}
}
}
return results;
}
private static Map<String, String> getExportHeads(Class tClass, List<String> exportFieldNames, List<String> notExportFieldNames) {
boolean allExport = (null != exportFieldNames && !exportFieldNames.isEmpty())?false:true;
boolean exitsNotExport = (null != notExportFieldNames && !notExportFieldNames.isEmpty())?true:false;
Field[] fields = tClass.getDeclaredFields();
Map<String, String> resultMap = new HashMap();
for (Field field : fields) {
System.out.println(field.getName());
if (field.isAnnotationPresent(Excel.class)) {
Field f = ExcelPoiUtil.getFieldOfCheck(field, allExport, exitsNotExport, exportFieldNames, notExportFieldNames);
if (null != f) {
resultMap.put(field.getName(), field.getAnnotation(Excel.class).name());
}
}
}
return resultMap;
}
private static Field getFieldOfCheck(Field field, boolean allExport, boolean exitsNotExport, List<String> exportFieldNames, List<String> notExportFieldNames){
if (allExport) {
if (exitsNotExport) {
return (!notExportFieldNames.contains(field.getName()))?field:null;
}
return field;
} else {
if (exitsNotExport) {
return (!notExportFieldNames.contains(field.getName()) && exportFieldNames.contains(field.getName()))?field:null;
}
return (exportFieldNames.contains(field.getName()))?field:null;
}
}
}
数据字典实现类DictExcelHandler
import cn.afterturn.easypoi.handler.inter.IExcelDictHandler;
import com.gdgxkj.common.security.model.DictDataModel;
import com.gdgxkj.common.security.utils.DictUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.stream.Collectors;
@Component
public class DictExcelHandler implements IExcelDictHandler {
@Override
public String toName(String dict, Object obj, String value, Object key) {
return getDictCache(dict, key, null);
}
@Override
public String toValue(String dict, Object obj, String value, Object key) {
return getDictCache(dict, null, value);
}
private String getDictCache(String type, Object key, Object value){
List<DictDataModel> dictCache = DictUtils.getDictCache(type);
if (null != key) {
List<DictDataModel> collect = dictCache.stream().filter(item -> item.getDictValue().equals(key)).collect(Collectors.toList());
if (!collect.isEmpty()) {
return collect.get(0).getDictLabel();
}
}
if (null != value) {
List<DictDataModel> collect = dictCache.stream().filter(item -> item.getDictLabel().equals(value)).collect(Collectors.toList());
if (!collect.isEmpty()) {
return collect.get(0).getDictValue();
}
}
return StringUtils.EMPTY;
}
}
使用测试
- 如果还需要自定义排序,也可以让前端传回来再处理,目前没实现,业务暂时没这个需求
public void export(@RequestBody SearchParam param, HttpServletResponse response) {
List<TestModel> list = XXService.findList(param);
try {
List<ExcelExportEntity> exportEntitys = ExcelPoiUtil.getExportEntitys(TestModel.class, null, null);
ExportParams exportParams = new ExportParams();
exportParams.setDictHandler(new DictExcelHandler());
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, exportEntitys, result);
FileOutputStream fos =new FileOutputStream("H:/test.xlsx");
workbook.write(fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}