java - Apache POI jar 无法在 excel 文件中写入特殊字符

标签 java excel apache-poi

我的字符串中有一个特殊字符,我想将其写入 excel 文件,但 poi jar 将其替换为 ?

特殊字符如下字符串:

enter image description here

一些网站低于 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])

例子:

我的字符串如下

enter image description here

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_ 作为一种类似于 &#xHHHH; 的实体确实具有特殊含义。

因此下面的代码将起作用,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/

相关文章:

java - 如何重命名提取的文件

java - 具有降序和升序的迭代器不起作用

java - 哪个是用 Java 读取大型 excel 文件的最佳 API?

java - Apache poi 将数据写入现有工作簿的方法

java - JPA ManyToMany - 始终在表中创建新值,即使该值存在

java - 寻求有关使用 DAWG 实现文字游戏的建议

excel - 过滤或导入特定日期的数据

vba - 遍历VBA中的指定工作表

java - Apache POI 出现错误,无法读取使用 FileInputStream 传递的文件

java - 使用 Apache POI for Java 在现有 Excel 工作簿中创建新工作表