簡體   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