2015-11-05 165 views
0

我在Excel文件中讀取日文字符時遇到問題。讀者的構造函數是:在ReadOnlySharedStringsTable中無法正確顯示日文字符

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文件,我曾嘗試創建一個工作簿,但它給出了內存錯誤,所以使用它不是一個選項。

任何想法哪裏可能出錯?

+0

對不起。這是Java。感謝您的注意。 – paaaaat

+0

介意給我一個教訓? :) – paaaaat

回答

0

找到了答案。修改構造函數爲:

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; 
     } 
     } 
    } 
    } 

和更改的字符串表到SharedStringsTable。我不確定爲什麼XSSFReader必須先走。任何可以解釋的人都歡迎他們這樣做。