我的字符串中有一个特殊字符,我想将其写入 excel 文件,但 poi jar 将其替换为 ?
特殊字符如下字符串:
一些网站低于 unicode
1)
Unicode character Oct Dec Hex HTML
\x{F} si shift in ctrl-o 017 15 0xF 
2)
U+000F : <control-000F> (SHIFT IN [SI])
例子:
我的字符串如下
Excel 显示输出为:
PrincipalOffice~?DIRECTION
我使用的代码如下:
String filename= "D:\\DataFiles\\"+File+"";
XSSFWorkbook hwb =new XSSFWorkbook();
XSSFSheet sheet = hwb.createSheet("Data");
XSSFRow rowhead= sheet.createRow((short)0);
rowhead.createCell((short) 0).setCellValue("my");
XSSFRow row= sheet.createRow((short)i);
String value = "PrincipalOffice~DIRECTION";
row.createCell((short) 0).setCellValue(value);
FileOutputStream fileOut = new FileOutputStream(filename);
hwb.write(fileOut);
fileOut.close();
hwb.close();
我试过 3.15 和 3.17 jar 的 apache poi 我需要使用其他库吗?如果是,请建议
最佳答案
您提到的字符 0x0F
或 不能直接存储在
XML
中,因为它是一个控件特点。因此,由于 *.xlsx
文件使用 XML
存储内容,因此无法直接存储该字符。
然而,Microsoft 定义: ECMA-376 Part 1 22.4 Variant Types 22.4.2.4 bstr (Basic String) :
22.4.2.4 bstr (Basic String)
This element defines a binary basic string variant type, which can store any valid Unicode character. Unicode characters that cannot be directly represented in XML as defined by the XML 1.0 specification, shall be escaped using the Unicode numerical character representation escape character format
_xHHHH_
, where H represents a hexadecimal character in the character's value. [Example: The Unicode character 8 is not permitted in an XML 1.0 document, so it shall be escaped as_x0008_
. end example] To store the literal form of an escape sequence, the initial underscore shall itself be escaped (i.e. stored as_x005F_
). [Example: The string literal_x0008_
would be stored as_x005F_x0008_
. end example]The possible values for this element are defined by the W3C XML Schema string datatype.
这扩展了 W3C XML 架构字符串数据类型。因此,字符序列 _xHHHH_
作为一种类似于 HHHH;
的实体确实具有特殊含义。
因此下面的代码将起作用,Excel
会将字符 0x0F
作为单元格内容。
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class CreateExcelControlCharacter {
public static void main(String[] args) throws Exception {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet();
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
String value = "PrincipalOffice\u000FDIRECTION";
value = value.replace("\u000F", "_x000F_");
cell.setCellValue(value);
wb.write(new FileOutputStream("CreateExcelControlCharacter.xlsx"));
wb.close();
}
}
但请问为什么您需要这个控制字符作为Excel
单元格内容?这可能是我问题的答案 Useful use cases for escape character format _xHHHH_ in Office Open XML? .
关于java - Apache POI jar 无法在 excel 文件中写入特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52185309/