Excel 导入导出优化

v1.4.1
Parker 4 years ago
parent 4c1b623466
commit ce61bf5cb8

@ -196,7 +196,7 @@ public abstract class BaseRestController <T extends BaseEntity, E extends ApiWra
ResultVo<?> resultVo;
String msgInfo;
try {
List<E> modelList = ExcelUtil.INSTANCE.readExcel(files.get(0), modelClazz);
List<E> modelList = ExcelUtil.getInstance().readExcel(files.get(0), modelClazz);
if(CollUtil.isNotEmpty(modelList)){
if(modelList.size() > globalProperties.getExcel().getImportMaxCount()){
String maxError = StrUtil.format(CoreMsg.EXCEL_HANDLE_MAX.getMessage(), modelList.size(),
@ -307,7 +307,7 @@ public abstract class BaseRestController <T extends BaseEntity, E extends ApiWra
modelList = WrapperUtil.transformInstance(entityList, modelClazz);
}
// 导出Excel
ExcelUtil.INSTANCE.writeExcel(response, modelList ,fileName,"sheet", modelClazz ,ExcelTypeEnum.XLSX);
ExcelUtil.getInstance().writeExcel(response, modelList ,fileName,"sheet", modelClazz ,ExcelTypeEnum.XLSX);
// 花费毫秒数
long timerCount = timer.interval();
// 提示信息

@ -23,6 +23,8 @@ import cn.hutool.core.util.ReflectUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.opsli.api.base.warpper.ApiWrapper;
import org.opsli.api.wrapper.system.dict.DictWrapper;
@ -37,6 +39,7 @@ import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
/**
* @BelongsProject: opsli-boot
@ -45,31 +48,47 @@ import java.util.List;
* @CreateTime: 2020-09-23 21:53
* @Description: ExcelUtil
*/
public enum ExcelUtil {
/** 实例 */
INSTANCE;
@Slf4j
public final class ExcelUtil {
/** 字典KEY */
public final String DICT_NAME_KEY = "dictName";
public final String DICT_VALUE_KEY = "dictValue";
private final ExcelPlugin EXCEL_PLUGIN = new ExcelPlugin();
public static final String DICT_NAME_KEY = "dictName";
public static final String DICT_VALUE_KEY = "dictValue";
/** 字段字典Map */
private static final Map<Class<?>, JSONObject> FIELD_DICT_MAP = Maps.newHashMap();
private ExcelUtil(){}
/** 静态内部类,里面实例化外部类 */
private static class ExcelUtilSingletonHolder {
private static final ExcelPlugin EXCEL_PLUGIN = new ExcelPlugin();
private static final ExcelUtil INSTANCE = new ExcelUtil();
}
/**
*
* @return ExcelUtil
*/
public static ExcelUtil getInstance(){
return ExcelUtilSingletonHolder.INSTANCE;
}
// ================================================
public <T> List<T> readExcel(MultipartFile excel, Class<T> rowModel) throws ExcelPluginException {
List<T> ts = EXCEL_PLUGIN.readExcel(excel, rowModel);
List<T> ts = ExcelUtilSingletonHolder.EXCEL_PLUGIN.readExcel(excel, rowModel);
// 处理数据
return this.handleDatas(ts, rowModel, ExcelOperate.READ);
}
public <T> List<T> radExcel(MultipartFile excel, Class<T> rowModel, String sheetName) throws ExcelPluginException {
List<T> ts = EXCEL_PLUGIN.readExcel(excel, rowModel, sheetName);
List<T> ts = ExcelUtilSingletonHolder.EXCEL_PLUGIN.readExcel(excel, rowModel, sheetName);
// 处理数据
return this.handleDatas(ts, rowModel, ExcelOperate.READ);
}
public <T> List<T> readExcel(MultipartFile excel, Class<T> rowModel, String sheetName, int headLineNum) throws ExcelPluginException {
List<T> ts = EXCEL_PLUGIN.readExcel(excel, rowModel, sheetName, headLineNum);
List<T> ts = ExcelUtilSingletonHolder.EXCEL_PLUGIN.readExcel(excel, rowModel, sheetName, headLineNum);
// 处理数据
return this.handleDatas(ts, rowModel, ExcelOperate.READ);
}
@ -77,16 +96,16 @@ public enum ExcelUtil {
public <T> void writeExcel(HttpServletResponse response, List<T> list, String fileName, String sheetName, Class<T> classType, ExcelTypeEnum excelTypeEnum) throws ExcelPluginException {
// 处理数据
List<T> ts = this.handleDatas(list, classType, ExcelOperate.WRITE);
EXCEL_PLUGIN.writeExcel(response, ts, fileName, sheetName, classType, excelTypeEnum);
ExcelUtilSingletonHolder.EXCEL_PLUGIN.writeExcel(response, ts, fileName, sheetName, classType, excelTypeEnum);
}
/**
*
* @param datas
* @param typeClazz
* @param operate
* @param <T>
* @return
* @param datas
* @param typeClazz CLazz
* @param operate
* @param <T>
* @return List<T>
*/
private <T> List<T> handleDatas(List<T> datas, Class<T> typeClazz, ExcelOperate operate){
// 计时器
@ -117,11 +136,12 @@ public enum ExcelUtil {
}
}catch (Exception e){
log.error(e.getMessage(), e);
return datas;
}finally {
// 花费毫秒数
long timerCount = timer.interval();
System.out.println("Excel 处理数据耗时:"+ DateUtil.formatBetween(timerCount));
log.info("Excel 处理数据耗时:"+ DateUtil.formatBetween(timerCount));
}
return datas;
@ -134,7 +154,13 @@ public enum ExcelUtil {
* @return JSONObject
*/
public JSONObject getFields(Class<?> clazz){
JSONObject fieldNameAndTypeCodeDict = JSONUtil.createObj();
// 加入内部缓存 防止每次导出都重复反射对象
JSONObject fieldNameAndTypeCodeDict = FIELD_DICT_MAP.get(clazz);
if(!JSONUtil.isNull(fieldNameAndTypeCodeDict)){
return fieldNameAndTypeCodeDict;
}
fieldNameAndTypeCodeDict = JSONUtil.createObj();
Field[] fields = ReflectUtil.getFields(clazz);
for (Field field : fields) {
ExcelInfo excelInfo = field.getAnnotation(ExcelInfo.class);
@ -146,6 +172,7 @@ public enum ExcelUtil {
}
}
}
FIELD_DICT_MAP.put(clazz, fieldNameAndTypeCodeDict);
return fieldNameAndTypeCodeDict;
}
@ -196,7 +223,7 @@ public enum ExcelUtil {
}
private <T extends ApiWrapper> T cast(Object msg){
private static <T extends ApiWrapper> T cast(Object msg){
if(null == msg){
return null;
}

@ -80,8 +80,6 @@ public final class ModelFactoryHelper {
clazzPool.importPackage(ApiWrapper.class.getName());
clazzPool.importPackage(modelClazz.getName());
// 抽象助手类
CtClass abstractEntityHelper = clazzPool.getCtClass(AbstractModelHelper.class.getName());
// 助手类名称
@ -147,9 +145,9 @@ public final class ModelFactoryHelper {
createByImportSb.append("String dictValue").append(i).append(" = \"\";").append("\n")
.append("if(dictJson.getJSONObject(fieldNameAndTypeCodeDict.getStr(\"").append(field.getName()).append("\")").append(") != null &&")
.append("dictJson.getJSONObject(fieldNameAndTypeCodeDict.getStr(\"").append(field.getName()).append("\")")
.append(").getJSONObject(ExcelUtil.INSTANCE.DICT_NAME_KEY) != null ){").append("\n")
.append(").getJSONObject(ExcelUtil.DICT_NAME_KEY) != null ){").append("\n")
.append("dictValue").append(i).append(" = dictJson.getJSONObject(fieldNameAndTypeCodeDict.getStr(\"").append(field.getName()).append("\")")
.append(").getJSONObject(ExcelUtil.INSTANCE.DICT_NAME_KEY).getStr(")
.append(").getJSONObject(ExcelUtil.DICT_NAME_KEY).getStr(")
.append("model.").append(readMethod.getName()).append("());").append("\n")
.append("}").append("\n");
@ -191,9 +189,9 @@ public final class ModelFactoryHelper {
createByExportSb.append("String dictValue").append(i).append(" = \"\";").append("\n")
.append("if(dictJson.getJSONObject(fieldNameAndTypeCodeDict.getStr(\"").append(field.getName()).append("\")").append(") != null &&")
.append("dictJson.getJSONObject(fieldNameAndTypeCodeDict.getStr(\"").append(field.getName()).append("\")")
.append(").getJSONObject(ExcelUtil.INSTANCE.DICT_VALUE_KEY) != null ){").append("\n")
.append(").getJSONObject(ExcelUtil.DICT_VALUE_KEY) != null ){").append("\n")
.append("dictValue").append(i).append(" = dictJson.getJSONObject(fieldNameAndTypeCodeDict.getStr(\"").append(field.getName()).append("\")")
.append(").getJSONObject(ExcelUtil.INSTANCE.DICT_VALUE_KEY).getStr(")
.append(").getJSONObject(ExcelUtil.DICT_VALUE_KEY).getStr(")
.append("model.").append(readMethod.getName()).append("());").append("\n")
.append("}").append("\n");
@ -205,7 +203,7 @@ public final class ModelFactoryHelper {
helperClazz.addMethod(createByExportMethod);
// 生成文件 测试查看使用
helperClazz.writeFile("C:/Users/zhoupengcheng/Desktop/test");
//helperClazz.writeFile("C:/Users/zhoupengcheng/Desktop/test");
// 获取 JAVA 类
Class<?> javaClazz = helperClazz.toClass();

Loading…
Cancel
Save