mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-02 20:02:49 +08:00
add EscapeStrCellSetter
This commit is contained in:
parent
42fbe12f69
commit
3cb4aa0796
@ -13,6 +13,7 @@
|
||||
* 【core 】 增加Partition
|
||||
* 【http 】 SoapClient.sendForResponse改为public(issue#I466NN@Gitee)
|
||||
* 【core 】 XmlUtil增加append重载(issue#I466Q0@Gitee)
|
||||
* 【poi 】 增加EscapeStrCellSetter(issue#I466ZZ@Gitee)
|
||||
|
||||
### 🐞Bug修复
|
||||
|
||||
|
@ -0,0 +1,45 @@
|
||||
package cn.hutool.poi.excel.cell.setters;
|
||||
|
||||
import cn.hutool.core.util.ReUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* 字符串转义Cell值设置器<br>
|
||||
* 使用 _x005F前缀转义_xXXXX_,避免被decode的问题<br>
|
||||
* 如用户传入'_x5116_'会导致乱码,使用此设置器转义为'_x005F_x5116_'
|
||||
*
|
||||
* @author looly
|
||||
* @since 5.7.10
|
||||
*/
|
||||
public class EscapeStrCellSetter extends CharSequenceCellSetter {
|
||||
|
||||
private static final Pattern utfPtrn = Pattern.compile("_x[0-9A-Fa-f]{4}_");
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param value 值
|
||||
*/
|
||||
public EscapeStrCellSetter(CharSequence value) {
|
||||
super(escape(StrUtil.str(value)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用 _x005F前缀转义_xXXXX_,避免被decode的问题<br>
|
||||
* issue#I466ZZ@Gitee
|
||||
*
|
||||
* @param value 被转义的字符串
|
||||
* @return 转义后的字符串
|
||||
*/
|
||||
private static String escape(String value) {
|
||||
if (value == null || false == value.contains("_x")) {
|
||||
return value;
|
||||
}
|
||||
|
||||
// 使用 _x005F前缀转义_xXXXX_,避免被decode的问题
|
||||
// issue#I466ZZ@Gitee
|
||||
return ReUtil.replaceAll(value, utfPtrn, "_x005F$0");
|
||||
}
|
||||
}
|
@ -8,6 +8,7 @@ import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.poi.excel.cell.setters.EscapeStrCellSetter;
|
||||
import cn.hutool.poi.excel.style.StyleUtil;
|
||||
import org.apache.poi.ss.usermodel.BorderStyle;
|
||||
import org.apache.poi.ss.usermodel.BuiltinFormats;
|
||||
@ -740,7 +741,7 @@ public class ExcelWriteTest {
|
||||
// https://gitee.com/dromara/hutool/issues/I466ZZ
|
||||
// 需要输出S_20000314_x5116_0004
|
||||
// 此处加入一个转义前缀:_x005F
|
||||
List<Object> row = ListUtil.of("S_20000314_x005F_x5116_0004");
|
||||
List<Object> row = ListUtil.of(new EscapeStrCellSetter("S_20000314_x5116_0004"));
|
||||
|
||||
ExcelWriter writer = ExcelUtil.getWriter("d:/test/_x.xlsx");
|
||||
writer.writeRow(row);
|
||||
|
Loading…
Reference in New Issue
Block a user