我想讀表和詳細的數據在Microsoft Word文檔(DOCX文件)使用Apache POI細胞。該文件包含UTF-8編碼的字符(僧伽羅語言)。我正在使用以下代碼塊。讀UTF-8編碼的文本里面的內容表中MS-Word文件使用Apache POI
FileInputStream fis = new FileInputStream("path\\to\\file.docx");
XWPFDocument doc = new XWPFDocument(fis);
Iterator<IBodyElement> iter = doc.getBodyElementsIterator();
while (iter.hasNext()) {
IBodyElement elem = iter.next();
if (elem instanceof XWPFTable) {
List<XWPFTableRow> rows = ((XWPFTable) elem).getRows();
for(XWPFTableRow row :rows){
List<XWPFTableCell> cells = row.getTableCells();
for(XWPFTableCell cell : cells){
PrintStream out = new PrintStream(System.out, true, "UTF-8");
out.println(cell.getText());
}
}
}
}
但我沒有在輸出控制檯中得到正確的UTF-8字符。
我已經參考了多種解決方案,包括以下內容。
How to parse UTF-8 characters in Excel files using POI |我正在嘗試在Word文件中讀取表格。所以我的Cell
對象沒有getStringCellValue()
方法。
http://www.herongyang.com/Java-Tools/native2ascii-Set-UTF-8-Encoding-in-PrintStream.html |我已經試過這個解決方案,它不工作。
沒有人知道如何使用Apache POI的word文件讀取UTF-8編碼的字符?
您是否嘗試過'URLEncoder.encode(cell.getText(), 「UTF-8」)'?並打印這個呢? – alirabiee
@alirabiee,剛剛嘗試過,它也沒有工作。無論如何,文本不是一個URL。 –
這是IDE的控制檯設置的問題。哪個IDE被使用?通常'System.out.println(cell.getText());'應該工作。沒有明確的'PrintStream'定義需要。 –