From ce61bf5cb80671fffda5cf71a542d5a1136a5648 Mon Sep 17 00:00:00 2001 From: Parker Date: Wed, 24 Feb 2021 11:16:20 +0800 Subject: [PATCH] =?UTF-8?q?Excel=20=E5=AF=BC=E5=85=A5=E5=AF=BC=E5=87=BA?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../base/controller/BaseRestController.java | 4 +- .../java/org/opsli/core/utils/ExcelUtil.java | 65 +++++++++++++------ .../excel/factory/ModelFactoryHelper.java | 12 ++-- 3 files changed, 53 insertions(+), 28 deletions(-) diff --git a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/base/controller/BaseRestController.java b/opsli-base-support/opsli-core/src/main/java/org/opsli/core/base/controller/BaseRestController.java index 3dfa65e..4a08024 100644 --- a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/base/controller/BaseRestController.java +++ b/opsli-base-support/opsli-core/src/main/java/org/opsli/core/base/controller/BaseRestController.java @@ -196,7 +196,7 @@ public abstract class BaseRestController resultVo; String msgInfo; try { - List modelList = ExcelUtil.INSTANCE.readExcel(files.get(0), modelClazz); + List 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 , 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 List readExcel(MultipartFile excel, Class rowModel) throws ExcelPluginException { - List ts = EXCEL_PLUGIN.readExcel(excel, rowModel); + List ts = ExcelUtilSingletonHolder.EXCEL_PLUGIN.readExcel(excel, rowModel); // 处理数据 return this.handleDatas(ts, rowModel, ExcelOperate.READ); } public List radExcel(MultipartFile excel, Class rowModel, String sheetName) throws ExcelPluginException { - List ts = EXCEL_PLUGIN.readExcel(excel, rowModel, sheetName); + List ts = ExcelUtilSingletonHolder.EXCEL_PLUGIN.readExcel(excel, rowModel, sheetName); // 处理数据 return this.handleDatas(ts, rowModel, ExcelOperate.READ); } public List readExcel(MultipartFile excel, Class rowModel, String sheetName, int headLineNum) throws ExcelPluginException { - List ts = EXCEL_PLUGIN.readExcel(excel, rowModel, sheetName, headLineNum); + List ts = ExcelUtilSingletonHolder.EXCEL_PLUGIN.readExcel(excel, rowModel, sheetName, headLineNum); // 处理数据 return this.handleDatas(ts, rowModel, ExcelOperate.READ); } @@ -77,16 +96,16 @@ public enum ExcelUtil { public void writeExcel(HttpServletResponse response, List list, String fileName, String sheetName, Class classType, ExcelTypeEnum excelTypeEnum) throws ExcelPluginException { // 处理数据 List 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 - * @return + * @param datas 数据 + * @param typeClazz 数据CLazz + * @param operate 操作方式 + * @param 泛型 + * @return List */ private List handleDatas(List datas, Class 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 cast(Object msg){ + private static T cast(Object msg){ if(null == msg){ return null; } diff --git a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/utils/excel/factory/ModelFactoryHelper.java b/opsli-base-support/opsli-core/src/main/java/org/opsli/core/utils/excel/factory/ModelFactoryHelper.java index c91ff65..7825679 100644 --- a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/utils/excel/factory/ModelFactoryHelper.java +++ b/opsli-base-support/opsli-core/src/main/java/org/opsli/core/utils/excel/factory/ModelFactoryHelper.java @@ -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();