From ccf84377f8609d6f867a556d2802c57a497232ec Mon Sep 17 00:00:00 2001
From: RuoYi <yzz_ivy@163.com>
Date: Sun, 27 Mar 2022 15:34:13 +0800
Subject: [PATCH] 优化导出excel单元格验证,包含变更为开头.防止正常内容被替换
---
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java | 126 ++++++++++++++++++++++++++++-------------
1 files changed, 85 insertions(+), 41 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..1650f4d 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;
@@ -17,6 +18,7 @@
import java.util.Set;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
+import org.apache.commons.lang3.RegExUtils;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
@@ -57,12 +59,16 @@
/**
* Excel相关处理
- *
+ *
* @author ruoyi
*/
public class ExcelUtil<T>
{
private static final Logger log = LoggerFactory.getLogger(ExcelUtil.class);
+
+ public static final String FORMULA_REGEX_STR = "=|-|\\+|@";
+
+ public static final String[] FORMULA_STR = { "=", "-", "+", "@" };
/**
* Excel sheet最大行数,默认65536
@@ -108,7 +114,7 @@
* 当前行号
*/
private int rownum;
-
+
/**
* 标题
*/
@@ -173,7 +179,7 @@
/**
* 对excel表单默认第一个索引名转换成list
- *
+ *
* @param is 输入流
* @return 转换后集合
*/
@@ -184,7 +190,7 @@
/**
* 对excel表单默认第一个索引名转换成list
- *
+ *
* @param is 输入流
* @param titleNum 标题占用行数
* @return 转换后集合
@@ -196,7 +202,7 @@
/**
* 对excel表单指定表格索引名转换成list
- *
+ *
* @param sheetName 表格索引名
* @param titleNum 标题占用行数
* @param is 输入流
@@ -281,7 +287,7 @@
String dateFormat = field.getAnnotation(Excel.class).dateFormat();
if (StringUtils.isNotEmpty(dateFormat))
{
- val = DateUtils.parseDateToStr(dateFormat, (Date) val);
+ val = parseDateToStr(dateFormat, val);
}
else
{
@@ -293,7 +299,7 @@
{
val = Convert.toInt(val);
}
- else if (Long.TYPE == fieldType || Long.class == fieldType)
+ else if ((Long.TYPE == fieldType || Long.class == fieldType) && StringUtils.isNumeric(Convert.toStr(val)))
{
val = Convert.toLong(val);
}
@@ -350,21 +356,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 +378,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 +437,6 @@
finally
{
IOUtils.closeQuietly(wb);
- IOUtils.closeQuietly(out);
}
}
@@ -465,7 +470,7 @@
/**
* 填充excel数据
- *
+ *
* @param index 序号
* @param row 单元格行
*/
@@ -490,7 +495,7 @@
/**
* 创建表格样式
- *
+ *
* @param wb 工作薄对象
* @return 样式列表
*/
@@ -582,7 +587,7 @@
/**
* 设置单元格信息
- *
+ *
* @param value 单元格值
* @param attr 注解相关
* @param cell 单元格信息
@@ -591,7 +596,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.startsWithAny(cellValue, FORMULA_STR))
+ {
+ cellValue = RegExUtils.replaceFirst(cellValue, FORMULA_REGEX_STR, "\t$0");
+ }
+ cell.setCellValue(StringUtils.isNull(cellValue) ? attr.defaultValue() : cellValue + attr.suffix());
}
else if (ColumnType.NUMERIC == attr.cellType())
{
@@ -695,7 +706,7 @@
String separator = attr.separator();
if (StringUtils.isNotEmpty(dateFormat) && StringUtils.isNotNull(value))
{
- cell.setCellValue(DateUtils.parseDateToStr(dateFormat, (Date) value));
+ cell.setCellValue(parseDateToStr(dateFormat, value));
}
else if (StringUtils.isNotEmpty(readConverterExp) && StringUtils.isNotNull(value))
{
@@ -726,7 +737,7 @@
/**
* 设置 POI XSSFSheet 单元格提示
- *
+ *
* @param sheet 表单
* @param promptTitle 提示标题
* @param promptContent 提示内容
@@ -749,7 +760,7 @@
/**
* 设置某些列的值只能输入预制的数据,显示下拉框.
- *
+ *
* @param sheet 要设置的sheet.
* @param textlist 下拉框显示的内容
* @param firstRow 开始行
@@ -783,7 +794,7 @@
/**
* 解析导出值 0=男,1=女,2=未知
- *
+ *
* @param propertyValue 参数值
* @param converterExp 翻译注解
* @param separator 分隔符
@@ -820,7 +831,7 @@
/**
* 反向解析值 男=0,女=1,未知=2
- *
+ *
* @param propertyValue 参数值
* @param converterExp 翻译注解
* @param separator 分隔符
@@ -857,7 +868,7 @@
/**
* 数据处理器
- *
+ *
* @param value 数据值
* @param excel 数据注解
* @return
@@ -925,7 +936,7 @@
/**
* 获取bean中的属性值
- *
+ *
* @param vo 实体对象
* @param field 字段
* @param excel 注解
@@ -938,7 +949,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 +967,7 @@
/**
* 以类的属性的get方法方法形式获取值
- *
+ *
* @param o
* @param name
* @return value
@@ -1033,7 +1044,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 +1062,7 @@
/**
* 创建工作表
- *
+ *
* @param sheetNo sheet数量
* @param index 序号
*/
@@ -1068,7 +1079,7 @@
/**
* 获取单元格值
- *
+ *
* @param row 获取的行
* @param column 获取单元格列号
* @return 单元格值
@@ -1128,7 +1139,7 @@
/**
* 判断是否是空行
- *
+ *
* @param row 判断的行
* @return
*/
@@ -1148,4 +1159,37 @@
}
return true;
}
-}
\ No newline at end of file
+
+ /**
+ * 格式化不同类型的日期对象
+ *
+ * @param dateFormat 日期格式
+ * @param val 被格式化的日期对象
+ * @return 格式化后的日期字符
+ */
+ public String parseDateToStr(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