From 105c9d99c886c17125f6333bb10a5aec78244aab Mon Sep 17 00:00:00 2001
From: duandazhi <ab601026460@163.com>
Date: Fri, 25 Feb 2022 21:09:50 +0800
Subject: [PATCH] poi ExcelUtil 增加对java8 日志类型的支持
---
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java | 113 ++++++++++++++++++++++++++++++++++++--------------------
1 files changed, 73 insertions(+), 40 deletions(-)
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java
index d314398..ca2b196 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java
@@ -2,11 +2,12 @@
import java.io.IOException;
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.time.LocalDate;
+import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
@@ -57,12 +58,14 @@
/**
* Excel相关处理
- *
+ *
* @author ruoyi
*/
public class ExcelUtil<T>
{
private static final Logger log = LoggerFactory.getLogger(ExcelUtil.class);
+
+ public static final String[] FORMULA_STR = { "=", "-", "+", "@" };
/**
* Excel sheet最大行数,默认65536
@@ -108,7 +111,7 @@
* 当前行号
*/
private int rownum;
-
+
/**
* 标题
*/
@@ -173,7 +176,7 @@
/**
* 对excel表单默认第一个索引名转换成list
- *
+ *
* @param is 输入流
* @return 转换后集合
*/
@@ -184,7 +187,7 @@
/**
* 对excel表单默认第一个索引名转换成list
- *
+ *
* @param is 输入流
* @param titleNum 标题占用行数
* @return 转换后集合
@@ -196,7 +199,7 @@
/**
* 对excel表单指定表格索引名转换成list
- *
+ *
* @param sheetName 表格索引名
* @param titleNum 标题占用行数
* @param is 输入流
@@ -281,7 +284,7 @@
String dateFormat = field.getAnnotation(Excel.class).dateFormat();
if (StringUtils.isNotEmpty(dateFormat))
{
- val = DateUtils.parseDateToStr(dateFormat, (Date) val);
+ val = this.parseDateToStr(dateFormat, (Date) val);
}
else
{
@@ -350,21 +353,21 @@
/**
* 对list数据源将其里面的数据导入到excel表单
- *
+ *
* @param response 返回数据
* @param list 导出数据集合
* @param sheetName 工作表的名称
* @return 结果
* @throws IOException
*/
- public void exportExcel(HttpServletResponse response, List<T> list, String sheetName)throws IOException
+ public void exportExcel(HttpServletResponse response, List<T> list, String sheetName)
{
exportExcel(response, list, sheetName, StringUtils.EMPTY);
}
/**
* 对list数据源将其里面的数据导入到excel表单
- *
+ *
* @param response 返回数据
* @param list 导出数据集合
* @param sheetName 工作表的名称
@@ -372,57 +375,57 @@
* @return 结果
* @throws IOException
*/
- public void exportExcel(HttpServletResponse response, List<T> list, String sheetName, String title) throws IOException
+ public void exportExcel(HttpServletResponse response, List<T> list, String sheetName, String title)
{
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
this.init(list, sheetName, title, Type.EXPORT);
- exportExcel(response.getOutputStream());
+ exportExcel(response);
}
/**
* 对list数据源将其里面的数据导入到excel表单
- *
+ *
* @param sheetName 工作表的名称
* @return 结果
*/
/**
* 对list数据源将其里面的数据导入到excel表单
- *
+ *
* @param sheetName 工作表的名称
* @return 结果
*/
- public void importTemplateExcel(HttpServletResponse response, String sheetName) throws IOException
+ public void importTemplateExcel(HttpServletResponse response, String sheetName)
{
importTemplateExcel(response, sheetName, StringUtils.EMPTY);
}
/**
* 对list数据源将其里面的数据导入到excel表单
- *
+ *
* @param sheetName 工作表的名称
* @param title 标题
* @return 结果
*/
- public void importTemplateExcel(HttpServletResponse response, String sheetName, String title) throws IOException
+ public void importTemplateExcel(HttpServletResponse response, String sheetName, String title)
{
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
this.init(null, sheetName, title, Type.IMPORT);
- exportExcel(response.getOutputStream());
+ exportExcel(response);
}
/**
* 对list数据源将其里面的数据导入到excel表单
- *
+ *
* @return 结果
*/
- public void exportExcel(OutputStream out)
+ public void exportExcel(HttpServletResponse response)
{
try
{
writeSheet();
- wb.write(out);
+ wb.write(response.getOutputStream());
}
catch (Exception e)
{
@@ -431,7 +434,6 @@
finally
{
IOUtils.closeQuietly(wb);
- IOUtils.closeQuietly(out);
}
}
@@ -465,7 +467,7 @@
/**
* 填充excel数据
- *
+ *
* @param index 序号
* @param row 单元格行
*/
@@ -490,7 +492,7 @@
/**
* 创建表格样式
- *
+ *
* @param wb 工作薄对象
* @return 样式列表
*/
@@ -582,7 +584,7 @@
/**
* 设置单元格信息
- *
+ *
* @param value 单元格值
* @param attr 注解相关
* @param cell 单元格信息
@@ -591,7 +593,13 @@
{
if (ColumnType.STRING == attr.cellType())
{
- cell.setCellValue(StringUtils.isNull(value) ? attr.defaultValue() : value + attr.suffix());
+ String cellValue = Convert.toStr(value);
+ // 对于任何以表达式触发字符 =-+@开头的单元格,直接使用tab字符作为前缀,防止CSV注入。
+ if (StringUtils.containsAny(cellValue, FORMULA_STR))
+ {
+ cellValue = StringUtils.replaceEach(cellValue, FORMULA_STR, new String[] { "\t=", "\t-", "\t+", "\t@" });
+ }
+ cell.setCellValue(StringUtils.isNull(cellValue) ? attr.defaultValue() : cellValue + attr.suffix());
}
else if (ColumnType.NUMERIC == attr.cellType())
{
@@ -695,7 +703,7 @@
String separator = attr.separator();
if (StringUtils.isNotEmpty(dateFormat) && StringUtils.isNotNull(value))
{
- cell.setCellValue(DateUtils.parseDateToStr(dateFormat, (Date) value));
+ cell.setCellValue(this.parseDateToStr(dateFormat, (Date) value));
}
else if (StringUtils.isNotEmpty(readConverterExp) && StringUtils.isNotNull(value))
{
@@ -726,7 +734,7 @@
/**
* 设置 POI XSSFSheet 单元格提示
- *
+ *
* @param sheet 表单
* @param promptTitle 提示标题
* @param promptContent 提示内容
@@ -749,7 +757,7 @@
/**
* 设置某些列的值只能输入预制的数据,显示下拉框.
- *
+ *
* @param sheet 要设置的sheet.
* @param textlist 下拉框显示的内容
* @param firstRow 开始行
@@ -783,7 +791,7 @@
/**
* 解析导出值 0=男,1=女,2=未知
- *
+ *
* @param propertyValue 参数值
* @param converterExp 翻译注解
* @param separator 分隔符
@@ -820,7 +828,7 @@
/**
* 反向解析值 男=0,女=1,未知=2
- *
+ *
* @param propertyValue 参数值
* @param converterExp 翻译注解
* @param separator 分隔符
@@ -857,7 +865,7 @@
/**
* 数据处理器
- *
+ *
* @param value 数据值
* @param excel 数据注解
* @return
@@ -925,7 +933,7 @@
/**
* 获取bean中的属性值
- *
+ *
* @param vo 实体对象
* @param field 字段
* @param excel 注解
@@ -938,7 +946,7 @@
if (StringUtils.isNotEmpty(excel.targetAttr()))
{
String target = excel.targetAttr();
- if (target.indexOf(".") > -1)
+ if (target.contains("."))
{
String[] targets = target.split("[.]");
for (String name : targets)
@@ -956,7 +964,7 @@
/**
* 以类的属性的get方法方法形式获取值
- *
+ *
* @param o
* @param name
* @return value
@@ -1033,7 +1041,7 @@
for (Object[] os : this.fields)
{
Excel excel = (Excel) os[1];
- maxHeight = maxHeight > excel.height() ? maxHeight : excel.height();
+ maxHeight = Math.max(maxHeight, excel.height());
}
return (short) (maxHeight * 20);
}
@@ -1051,7 +1059,7 @@
/**
* 创建工作表
- *
+ *
* @param sheetNo sheet数量
* @param index 序号
*/
@@ -1068,7 +1076,7 @@
/**
* 获取单元格值
- *
+ *
* @param row 获取的行
* @param column 获取单元格列号
* @return 单元格值
@@ -1128,7 +1136,7 @@
/**
* 判断是否是空行
- *
+ *
* @param row 判断的行
* @return
*/
@@ -1148,4 +1156,29 @@
}
return true;
}
-}
\ No newline at end of file
+
+ /**
+ * 增加ExcelUtil对java8 日期的支持
+ * 格式化日期,日期可能是:{@link Date}、{@link LocalDateTime}、 {@link LocalDate} 其他日期暂不支持
+ * @param dateFormat 日期格式
+ * @param val 被格式化的日期对象
+ * @see DateUtils#parseDateToStr(String, Date)
+ */
+ private String parseDateToStr(final String dateFormat, Object val)
+ {
+ if (val == null) {
+ return "";
+ }
+ String str;
+ if (val instanceof Date) {
+ str = DateUtils.parseDateToStr(dateFormat, (Date) val);
+ } else if (val instanceof LocalDateTime) {
+ str = DateUtils.parseDateToStr(dateFormat, DateUtils.toDate((LocalDateTime) val));
+ } else if (val instanceof LocalDate) {
+ str = DateUtils.parseDateToStr(dateFormat, DateUtils.toDate((LocalDate) val));
+ } else {
+ str = val.toString();
+ }
+ return str;
+ }
+}
--
Gitblit v1.9.3