使用 Apache POI XSSF 将 Unicode 字符从 Java 写入 Excel 文件“.xlsx”
我正在开发一个项目,该项目当前正在使用 POI 使用 XSSF 写入 Excel 文件作业簿。我在将 Unicode 字符写入 Excel 文档并使其正确显示时遇到问题。
我使用 Pattern 和 Matcher 来查找文本中的其他标签并将其替换为相应的 Unicode 字符。
private static String replaceTags(String stringToChange, String replacementTag, String originalTag) {
Pattern pattern = Pattern.compile(originalTag);
Matcher matcher = pattern.matcher(stringToChange);
return matcher.replaceAll(replacementTag);
}
我在代码中调用该函数来查找和替换文本中的标签。它适用于我的大多数替换标签,但不适用于商标符号和引号等内容,因为我无法使用它们的实际符号作为替换,我必须为它们添加 Unicode。
if (replacementString.contains("%0022")) {
replacementString = replaceTags(replacementString, "\\U005C", "%0022");
}
c.setCellValue(replacementString);
现在我遇到的问题是,当我将字符串写入 Excel 文件时,Excel 会忽略 U005C
前面的反斜杠,而只是像普通文本一样写出代码。有没有办法让这些字符从我丢失的代码中出现,或者甚至可以做到?
更新:
除了引号之外,我已经获得了工作所需的所有符号。使用 \\\\U005c
或 \\\\U0022
会使 Excel 电子表格显示 \U005c 或 \U0022,而不是 "
。
I am working on a project that is currently using POI to write out to an Excel file using the XSSF Workbook. I have an issue with writing out Unicode characters to the Excel document and having them displaying correctly.
I am using Pattern and Matcher to find and replace other tags in text with the corresponding Unicode characters.
private static String replaceTags(String stringToChange, String replacementTag, String originalTag) {
Pattern pattern = Pattern.compile(originalTag);
Matcher matcher = pattern.matcher(stringToChange);
return matcher.replaceAll(replacementTag);
}
I call that function in my code to find and replace tags in my text. It's working for most of my replacement tags, but not for things like the Trademark symbol and the quotation mark, because I can't use the actual symbol for them as replacements, I have to put the Unicode for them.
if (replacementString.contains("%0022")) {
replacementString = replaceTags(replacementString, "\\U005C", "%0022");
}
c.setCellValue(replacementString);
Now the problem I'm running into is when I write the string to the Excel file Excel ignores the backslash in front of the U005C
and just writes the code out like normal text. Is there a way to get these characters to appear from the code that I'm missing or is it even possible to do?
Update:
I have gotten every symbol I need to work except for the quotation mark. Using \\\\U005c
or \\\\U0022
has the Excel spreadsheet displaying \U005c or \U0022 instead of "
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论