繁体   English   中英

日语字符未在ReadOnlySharedStringsTable中正确显示

[英]Japanese characters not displayed properly in ReadOnlySharedStringsTable

我在读取Excel文件中的日语字符时遇到问题。 读者的构造函数是:

public XExcelFileReader(final String excelPath) throws Exception {
    this.opcPkg = OPCPackage.open(excelPath, PackageAccess.READ);
    this.stringsTable = new ReadOnlySharedStringsTable(this.opcPkg);

    XSSFReader xssfReader = new XSSFReader(this.opcPkg);
    XMLInputFactory factory = XMLInputFactory.newInstance();
    InputStream inputStream = xssfReader.getSheetsData().next();
    this.xmlReader = factory.createXMLStreamReader(inputStream);

    while (this.xmlReader.hasNext()) {
      this.xmlReader.next();
      if (this.xmlReader.isStartElement()) {
        if (this.xmlReader.getLocalName().equals("sheetData"))
          break;
      }
    }
  }

此时,stringsTable具有日语字符,如予算ヨサン但在Excel文件中,它仅读取为予算 有些显示为与Excel文件中的显示相同,有些则没有。 我不确定哪里出错了,编码是UTF-8。

我正在读取一个较大的Excel文件,并且尝试创建工作簿,但它会发出内存错误,因此使用它不是一种选择。

有什么地方可能出错的任何想法吗?

找到了答案。 将构造函数修改为:

public XExcelFileReader(final String excelPath) throws Exception {
    this.opcPkg = OPCPackage.open(excelPath, PackageAccess.READ);
    XSSFReader xssfReader = new XSSFReader(this.opcPkg);
    this.stringsTable = xssfReader.getSharedStringsTable();

    XMLInputFactory factory = XMLInputFactory.newInstance();
    InputStream inputStream = xssfReader.getSheetsData().next();
    this.xmlReader = factory.createXMLStreamReader(inputStream);

    while (this.xmlReader.hasNext()) {
      this.xmlReader.next();
      if (this.xmlReader.isStartElement()) {
        if (this.xmlReader.getLocalName().equals("sheetData")) {
          break;
        }
      }
    }
  }

并将stringsTable更改为SharedStringsTable。 我不太确定为什么必须先使用XSSFReader。 任何可以解释的人都非常欢迎这样做。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM