From 3b42abef44bbc439ec2fd63d1b223efc121e945d Mon Sep 17 00:00:00 2001 From: RuoYi <yzz_ivy@163.com> Date: 星期三, 22 九月 2021 09:03:01 +0800 Subject: [PATCH] Excel注解支持自定义数据处理器 --- ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java | 50 ++++++++++++++++++++++++++++++++++++++++++-------- 1 files changed, 42 insertions(+), 8 deletions(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java index 3d95f31..3884790 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java @@ -6,6 +6,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.lang.reflect.Field; +import java.lang.reflect.Method; import java.math.BigDecimal; import java.text.DecimalFormat; import java.util.ArrayList; @@ -66,7 +67,7 @@ import com.ruoyi.common.config.RuoYiConfig; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.text.Convert; -import com.ruoyi.common.exception.CustomException; +import com.ruoyi.common.exception.UtilException; import com.ruoyi.common.utils.DateUtils; import com.ruoyi.common.utils.DictUtils; import com.ruoyi.common.utils.StringUtils; @@ -195,11 +196,11 @@ Map<String, PictureData> pictures; if (isXSSFWorkbook) { - pictures = getSheetPictrues07((XSSFSheet) sheet, (XSSFWorkbook) wb); + pictures = getSheetPictures07((XSSFSheet) sheet, (XSSFWorkbook) wb); } else { - pictures = getSheetPictrues03((HSSFSheet) sheet, (HSSFWorkbook) wb); + pictures = getSheetPictures03((HSSFSheet) sheet, (HSSFWorkbook) wb); } // 获取最后一个非空行的行下标,比如总行数为n,则返回的为n-1 int rows = sheet.getLastRowNum(); @@ -333,6 +334,10 @@ { val = reverseDictByExp(Convert.toStr(val), attr.dictType(), attr.separator()); } + else if (!attr.handler().equals(ExcelHandlerAdapter.class)) + { + val = dataFormatHandlerAdapter(val, attr); + } else if (ColumnType.IMAGE == attr.cellType() && StringUtils.isNotEmpty(pictures)) { PictureData image = pictures.get(row.getRowNum() + "_" + entry.getKey()); @@ -340,8 +345,11 @@ { val = ""; } - byte[] data = image.getData(); - val = FileUtils.writeImportBytes(data); + else + { + byte[] data = image.getData(); + val = FileUtils.writeImportBytes(data); + } } ReflectUtils.invokeSetter(entity, propertyName, val); } @@ -450,7 +458,7 @@ catch (Exception e) { log.error("导出Excel异常{}", e.getMessage()); - throw new CustomException("导出Excel失败,请联系网站管理员!"); + throw new UtilException("导出Excel失败,请联系网站管理员!"); } finally { @@ -726,6 +734,10 @@ { cell.setCellValue((((BigDecimal) value).setScale(attr.scale(), attr.roundingMode())).toString()); } + else if (!attr.handler().equals(ExcelHandlerAdapter.class)) + { + cell.setCellValue(dataFormatHandlerAdapter(value, attr)); + } else { // 设置列类型 @@ -896,6 +908,28 @@ public static String reverseDictByExp(String dictLabel, String dictType, String separator) { return DictUtils.getDictValue(dictType, dictLabel, separator); + } + + /** + * 数据处理器 + * + * @param value 数据值 + * @param excel 数据注解 + * @return + */ + public String dataFormatHandlerAdapter(Object value, Excel excel) + { + try + { + Object instance = excel.handler().newInstance(); + Method formatMethod = excel.handler().getMethod("format", new Class[] { Object.class, String[].class }); + value = formatMethod.invoke(instance, value, excel.args()); + } + catch (Exception e) + { + log.error("不能格式化数据 " + excel.handler(), e.getMessage()); + } + return Convert.toStr(value); } /** @@ -1196,7 +1230,7 @@ * @param workbook 工作簿对象 * @return Map key:图片单元格索引(1_1)String,value:图片流PictureData */ - public static Map<String, PictureData> getSheetPictrues03(HSSFSheet sheet, HSSFWorkbook workbook) + public static Map<String, PictureData> getSheetPictures03(HSSFSheet sheet, HSSFWorkbook workbook) { Map<String, PictureData> sheetIndexPicMap = new HashMap<String, PictureData>(); List<HSSFPictureData> pictures = workbook.getAllPictures(); @@ -1229,7 +1263,7 @@ * @param workbook 工作簿对象 * @return Map key:图片单元格索引(1_1)String,value:图片流PictureData */ - public static Map<String, PictureData> getSheetPictrues07(XSSFSheet sheet, XSSFWorkbook workbook) + public static Map<String, PictureData> getSheetPictures07(XSSFSheet sheet, XSSFWorkbook workbook) { Map<String, PictureData> sheetIndexPicMap = new HashMap<String, PictureData>(); for (POIXMLDocumentPart dr : sheet.getRelations()) -- Gitblit v1.9.2