From 3b42abef44bbc439ec2fd63d1b223efc121e945d Mon Sep 17 00:00:00 2001
From: RuoYi <yzz_ivy@163.com>
Date: Wed, 22 Sep 2021 09:03:01 +0800
Subject: [PATCH] Excel注解支持自定义数据处理器
---
ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java | 104 +++++++++++++++++++++++++++++++++++++++++++++------
1 files changed, 91 insertions(+), 13 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 ccc7b20..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;
@@ -19,6 +20,12 @@
import java.util.UUID;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
+import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
+import org.apache.poi.hssf.usermodel.HSSFPicture;
+import org.apache.poi.hssf.usermodel.HSSFPictureData;
+import org.apache.poi.hssf.usermodel.HSSFShape;
+import org.apache.poi.hssf.usermodel.HSSFSheet;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
@@ -60,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;
@@ -185,7 +192,16 @@
{
throw new IOException("文件sheet不存在");
}
- Map<String, PictureData> pictures = getSheetPictrues((XSSFSheet) sheet, (XSSFWorkbook) wb);
+ boolean isXSSFWorkbook = !(wb instanceof HSSFWorkbook);
+ Map<String, PictureData> pictures;
+ if (isXSSFWorkbook)
+ {
+ pictures = getSheetPictures07((XSSFSheet) sheet, (XSSFWorkbook) wb);
+ }
+ else
+ {
+ pictures = getSheetPictures03((HSSFSheet) sheet, (HSSFWorkbook) wb);
+ }
// 获取最后一个非空行的行下标,比如总行数为n,则返回的为n-1
int rows = sheet.getLastRowNum();
@@ -318,19 +334,22 @@
{
val = reverseDictByExp(Convert.toStr(val), attr.dictType(), attr.separator());
}
- else if (ColumnType.IMAGE == attr.cellType())
+ else if (!attr.handler().equals(ExcelHandlerAdapter.class))
{
- if (StringUtils.isNull(pictures))
- {
- val = "";
- }
+ val = dataFormatHandlerAdapter(val, attr);
+ }
+ else if (ColumnType.IMAGE == attr.cellType() && StringUtils.isNotEmpty(pictures))
+ {
PictureData image = pictures.get(row.getRowNum() + "_" + entry.getKey());
if (image == null)
{
val = "";
}
- byte[] data = image.getData();
- val = FileUtils.writeImportBytes(data);
+ else
+ {
+ byte[] data = image.getData();
+ val = FileUtils.writeImportBytes(data);
+ }
}
ReflectUtils.invokeSetter(entity, propertyName, val);
}
@@ -439,7 +458,7 @@
catch (Exception e)
{
log.error("导出Excel异常{}", e.getMessage());
- throw new CustomException("导出Excel失败,请联系网站管理员!");
+ throw new UtilException("导出Excel失败,请联系网站管理员!");
}
finally
{
@@ -715,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
{
// 设置列类型
@@ -885,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);
}
/**
@@ -1179,13 +1224,46 @@
}
/**
- * 获取Excel图片
+ * 获取Excel2003图片
*
* @param sheet 当前sheet对象
* @param workbook 工作簿对象
* @return Map key:图片单元格索引(1_1)String,value:图片流PictureData
*/
- public static Map<String, PictureData> getSheetPictrues(XSSFSheet sheet, XSSFWorkbook workbook)
+ public static Map<String, PictureData> getSheetPictures03(HSSFSheet sheet, HSSFWorkbook workbook)
+ {
+ Map<String, PictureData> sheetIndexPicMap = new HashMap<String, PictureData>();
+ List<HSSFPictureData> pictures = workbook.getAllPictures();
+ if (!pictures.isEmpty())
+ {
+ for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren())
+ {
+ HSSFClientAnchor anchor = (HSSFClientAnchor) shape.getAnchor();
+ if (shape instanceof HSSFPicture)
+ {
+ HSSFPicture pic = (HSSFPicture) shape;
+ int pictureIndex = pic.getPictureIndex() - 1;
+ HSSFPictureData picData = pictures.get(pictureIndex);
+ String picIndex = String.valueOf(anchor.getRow1()) + "_" + String.valueOf(anchor.getCol1());
+ sheetIndexPicMap.put(picIndex, picData);
+ }
+ }
+ return sheetIndexPicMap;
+ }
+ else
+ {
+ return sheetIndexPicMap;
+ }
+ }
+
+ /**
+ * 获取Excel2007图片
+ *
+ * @param sheet 当前sheet对象
+ * @param workbook 工作簿对象
+ * @return Map key:图片单元格索引(1_1)String,value:图片流PictureData
+ */
+ public static Map<String, PictureData> getSheetPictures07(XSSFSheet sheet, XSSFWorkbook workbook)
{
Map<String, PictureData> sheetIndexPicMap = new HashMap<String, PictureData>();
for (POIXMLDocumentPart dr : sheet.getRelations())
@@ -1209,4 +1287,4 @@
}
return sheetIndexPicMap;
}
-}
\ No newline at end of file
+}
--
Gitblit v1.9.2