From 61034d4ddecd0b3056a7fb38a737b0e0cfc33b9c Mon Sep 17 00:00:00 2001 From: RuoYi Date: Sun, 27 Mar 2022 14:36:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=AF=BC=E5=87=BAexcel?= =?UTF-8?q?=E5=8D=95=E5=85=83=E6=A0=BC=E9=AA=8C=E8=AF=81,=E5=8C=85?= =?UTF-8?q?=E5=90=AB=E5=8F=98=E6=9B=B4=E4=B8=BA=E5=BC=80=E5=A4=B4.?= =?UTF-8?q?=E9=98=B2=E6=AD=A2=E6=AD=A3=E5=B8=B8=E5=86=85=E5=AE=B9=E8=A2=AB?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/ruoyi/common/utils/poi/ExcelUtil.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 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 eb9407509..9a837c430 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 @@ -22,6 +22,7 @@ import java.util.Set; import java.util.UUID; import java.util.stream.Collectors; import javax.servlet.http.HttpServletResponse; +import org.apache.commons.lang3.RegExUtils; import org.apache.poi.hssf.usermodel.HSSFClientAnchor; import org.apache.poi.hssf.usermodel.HSSFPicture; import org.apache.poi.hssf.usermodel.HSSFPictureData; @@ -88,6 +89,8 @@ public class ExcelUtil { private static final Logger log = LoggerFactory.getLogger(ExcelUtil.class); + public static final String FORMULA_REGEX_STR = "=|-|\\+|@"; + public static final String[] FORMULA_STR = { "=", "-", "+", "@" }; /** @@ -714,9 +717,9 @@ public class ExcelUtil { String cellValue = Convert.toStr(value); // 对于任何以表达式触发字符 =-+@开头的单元格,直接使用tab字符作为前缀,防止CSV注入。 - if (StringUtils.containsAny(cellValue, FORMULA_STR)) + if (StringUtils.startsWithAny(cellValue, FORMULA_STR)) { - cellValue = StringUtils.replaceEach(cellValue, FORMULA_STR, new String[] { "\t=", "\t-", "\t+", "\t@" }); + cellValue = RegExUtils.replaceFirst(cellValue, FORMULA_REGEX_STR, "\t$0"); } cell.setCellValue(StringUtils.isNull(cellValue) ? attr.defaultValue() : cellValue + attr.suffix()); }