优化Excel导出代码

v1.4.1
Parker 5 years ago
parent 12bb565eee
commit dddb0b322e

@ -11,7 +11,7 @@ import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.opsli.plugins.excel.annotation.CellStyleFormat;
import org.opsli.plugins.excel.annotation.ExcelInfo;
import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable;
@ -46,24 +46,24 @@ public abstract class ApiWrapper implements Serializable {
/** ID */
@TableId
@ApiModelProperty(value = "ID")
@ExcelIgnore
//@ApiModelProperty(value = "ID")
//@ExcelProperty(value = "ID", order = 1000)
@CellStyleFormat
//@ExcelInfo
private String id;
/** 创建人 */
@ApiModelProperty(value = "创建人")
@ExcelIgnore
//@ApiModelProperty(value = "创建人")
//@ExcelProperty(value = "创建人", order = 1001)
@CellStyleFormat
//@ExcelInfo
private String createBy;
/** 创建时间 */
@ApiModelProperty(value = "创建时间")
@ExcelIgnore
//@ExcelProperty(value = "创建时间", order = 1002)
//@CellStyleFormat
//@ExcelInfo
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
@ -72,14 +72,14 @@ public abstract class ApiWrapper implements Serializable {
@ApiModelProperty(value = "修改人")
@ExcelIgnore
//@ExcelProperty(value = "修改人", order = 1003)
//@CellStyleFormat
//@ExcelInfo
private String updateBy;
/** 更新时间 */
@ApiModelProperty(value = "修改时间")
@ExcelIgnore
//@ExcelProperty(value = "修改时间", order = 1004)
//@CellStyleFormat
//@ExcelInfo
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date updateTime;

@ -1,14 +1,11 @@
package org.opsli.api.wrapper.test;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.*;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.opsli.api.base.warpper.ApiWrapper;
import org.opsli.common.annotation.DictType;
import org.opsli.plugins.excel.annotation.CellStyleFormat;
import org.opsli.plugins.excel.annotation.ExcelInfo;
/**
* @BelongsProject: opsli-boot
@ -26,18 +23,17 @@ public class TestModel extends ApiWrapper {
@ApiModelProperty(value = "名称")
@ExcelProperty(value = "名称", order = 1)
@CellStyleFormat
@ExcelInfo
private String name;
@ApiModelProperty(value = "分类")
@DictType("testType")
@ExcelProperty(value = "分类", order = 2)
@CellStyleFormat
@ExcelInfo(dictType = "testType")
private String type;
@ApiModelProperty(value = "备注")
@ExcelProperty(value = "备注", order = 3)
@CellStyleFormat
@ExcelInfo
private String remark;
}

@ -6,9 +6,9 @@ import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.opsli.api.wrapper.system.dict.DictModel;
import org.opsli.common.annotation.DictType;
import org.opsli.common.enums.ExcelOperate;
import org.opsli.plugins.excel.ExcelPlugin;
import org.opsli.plugins.excel.annotation.ExcelInfo;
import org.opsli.plugins.excel.exception.ExcelPluginException;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
@ -33,27 +33,27 @@ public class ExcelUtil extends ExcelPlugin {
public <T> List<T> readExcel(MultipartFile excel, Class<T> rowModel) throws ExcelPluginException {
List<T> ts = super.readExcel(excel, rowModel);
// 处理数据
return this.handleDict(ts, rowModel, ExcelOperate.READ);
return this.handleDatas(ts, rowModel, ExcelOperate.READ);
}
@Override
public <T> List<T> readExcel(MultipartFile excel, Class<T> rowModel, String sheetName) throws ExcelPluginException {
List<T> ts = super.readExcel(excel, rowModel, sheetName);
// 处理数据
return this.handleDict(ts, rowModel, ExcelOperate.READ);
return this.handleDatas(ts, rowModel, ExcelOperate.READ);
}
@Override
public <T> List<T> readExcel(MultipartFile excel, Class<T> rowModel, String sheetName, int headLineNum) throws ExcelPluginException {
List<T> ts = super.readExcel(excel, rowModel, sheetName, headLineNum);
// 处理数据
return this.handleDict(ts, rowModel, ExcelOperate.READ);
return this.handleDatas(ts, rowModel, ExcelOperate.READ);
}
@Override
public <T> void writeExcel(HttpServletResponse response, List<T> list, String fileName, String sheetName, Class<T> classType, ExcelTypeEnum excelTypeEnum) throws ExcelPluginException {
// 处理数据
List<T> ts = this.handleDict(list, classType, ExcelOperate.WRITE);
List<T> ts = this.handleDatas(list, classType, ExcelOperate.WRITE);
super.writeExcel(response, ts, fileName, sheetName, classType, excelTypeEnum);
}
@ -65,7 +65,7 @@ public class ExcelUtil extends ExcelPlugin {
* @param <T>
* @return
*/
private <T> List<T> handleDict(List<T> datas, Class<T> typeClazz, ExcelOperate operate){
private <T> List<T> handleDatas(List<T> datas, Class<T> typeClazz, ExcelOperate operate){
// 空处理
if(datas == null || datas.size() == 0){
@ -75,33 +75,55 @@ public class ExcelUtil extends ExcelPlugin {
// 字段名 - 字典code
Map<String,String> fieldAndTypeCode = Maps.newHashMap();
// 字典code - 字典值
Map<String,List<DictModel>> typeCodeAndValue = Maps.newHashMap();
Map<String,List<DictModel>> typeCodeAndValue = null;
Field[] fields = ReflectUtil.getFields(typeClazz);
for (Field field : fields) {
DictType dictType = field.getAnnotation(DictType.class);
if(dictType != null){
String typeCode = dictType.value();
fieldAndTypeCode.put(field.getName(), typeCode);
ExcelInfo excelInfo = field.getAnnotation(ExcelInfo.class);
if(excelInfo != null){
// 字典
String dictType = excelInfo.dictType();
if(StringUtils.isNotEmpty(dictType)){
fieldAndTypeCode.put(field.getName(), dictType);
}
}
}
// 如果有字典
if(fieldAndTypeCode.size() != 0){
typeCodeAndValue = this.getDictMap(fieldAndTypeCode);
}
// 如果没有字典 则直接返回
if(fieldAndTypeCode.size() == 0){
// 数据字典赋值
for (T data : datas) {
// 处理字典
this.handleDict(data, operate, fieldAndTypeCode, typeCodeAndValue);
}
return datas;
}
// 取Redis 值
for (Map.Entry<String, String> entry : fieldAndTypeCode.entrySet()) {
String typeCode = entry.getValue();
List<DictModel> dictList = DictUtil.getDictList(typeCode);
// 如果字典 List 为空 则走下一个
if(dictList == null || dictList.size() == 0) continue;
typeCodeAndValue.put(typeCode, dictList);
// ========================= 处理字典 =========================
/**
*
* @param data
* @param operate excel
* @param fieldAndTypeCode - code
* @param typeCodeAndValue code -
* @param <T>
* @return
*/
private <T> void handleDict(T data, ExcelOperate operate, Map<String,String> fieldAndTypeCode,
Map<String,List<DictModel>> typeCodeAndValue
){
// 如果没有字典 则直接退出
if(fieldAndTypeCode.size() == 0 || typeCodeAndValue == null || typeCodeAndValue.size() == 0){
return;
}
// 数据字典赋值
for (T data : datas) {
for (Map.Entry<String, String> entry : fieldAndTypeCode.entrySet()) {
try {
String fieldName = entry.getKey();
@ -120,7 +142,24 @@ public class ExcelUtil extends ExcelPlugin {
}
}
}
return datas;
/**
* Map
* @param fieldAndTypeCode
* @return
*/
public Map<String,List<DictModel>> getDictMap(Map<String,String> fieldAndTypeCode){
// 字典code - 字典值
Map<String,List<DictModel>> typeCodeAndValue = Maps.newHashMap();
// 取Redis 值
for (Map.Entry<String, String> entry : fieldAndTypeCode.entrySet()) {
String typeCode = entry.getValue();
List<DictModel> dictList = DictUtil.getDictList(typeCode);
// 如果字典 List 为空 则走下一个
if(dictList == null || dictList.size() == 0) continue;
typeCodeAndValue.put(typeCode, dictList);
}
return typeCodeAndValue;
}
/**
@ -152,4 +191,8 @@ public class ExcelUtil extends ExcelPlugin {
}
return val;
}
// ========================= 反射字段 =========================
}

@ -0,0 +1,30 @@
package org.opsli.plugins.excel.annotation;
import java.lang.annotation.*;
/**
* @BelongsProject: opsli-boot
* @BelongsPackage: org.opsli.common.annotation
* @Author: Parker
* @CreateTime: 2020-09-16 16:36
* @Description:
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Documented
public @interface ExcelInfo {
/** 字典类型 code */
String dictType() default "";
/** 字段反射类型 */
//Class<?> reflectFieldType() default Class.class;
/** 字段反射名称 */
//String reflectFieldName() default "";
/** 字段样式 */
CellStyleFormat cellStyleFormat() default @CellStyleFormat();
}
Loading…
Cancel
Save