2014-01-08 52 views
2

我使用的Apache POI實用程序(POI暫存器-3.9.jar和相關的3.9版本POI罐子)轉換成DOC文件txt.it爲TXT工作與大多數的文件,但我得到一個異常就像如下獲取java.lang.IndexOutOfBoundsException轉換DOC文件時,使用Apache POI

java.lang.IndexOutOfBoundsException: 0 not accessible in a list of length 0 
at org.apache.poi.util.IntList.get(IntList.java:346) 
at org.apache.poi.poifs.storage.BlockAllocationTableReader.fetchBlocks(BlockAllocationTableReader.java:224) 
at org.apache.poi.poifs.storage.BlockListImpl.fetchBlocks(BlockListImpl.java:123) 
at org.apache.poi.poifs.storage.SmallDocumentBlockList.fetchBlocks(SmallDocumentBlockList.java:30) 
at org.apache.poi.poifs.filesystem.POIFSFileSystem.processProperties(POIFSFileSystem.java:521) 
at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:163) 
at org.apache.poi.hwpf.HWPFDocumentCore.verifyAndBuildPOIFS(HWPFDocumentCore.java:106) 
at org.apache.poi.hwpf.HWPFDocument.<init>(HWPFDocument.java:174) 

守則是繼

fileInputStream = new FileInputStream(file.getAbsolutePath()); 

// A HWPFDocument used to read document file from FileInputStream 
HWPFDocument doc = new HWPFDocument(fileInputStream); 

// A WordExtractor used to read textual content from document 
WordExtractor docExtractor = new WordExtractor(doc); 

// This Array stores each line from the document file. 
String[] docArray = docExtractor.getParagraphText(); 
StringBuilder contents = new StringBuilder(); 
for (int i = 0; i < docArray.length; i++) { 
    if (docArray[i] != null) { 
     contents.append(docArray[i]); 
     contents.append(System.getProperty("line.separator")); 
    } 
} 
isConverted = FileDirectoryOperations.writeTextOutputFile(targetFilePath, contents.toString()); 

我們正在線越來越例外HWPFDocument doc = new HWPFDocument(fileInputStream);

做我們對此有任何修正。

請分享您的意見。

在此先感謝。

Sourabh

+0

您可以發佈您的代碼? – Bhushan

回答

0

你得到的例外表明有什麼東西了底層OLE2容器的結構方式。

當涉及到OLE2結構時,較舊的POIFSFileSystem比較新的(但只讀的)NPOIFSFileSystem更挑剔一點,所以你應該嘗試切換到那個。然後,您的設置代碼將是:

NPOIFSFileSystem fs = new NPOIFSFileSystem(file); 
HWPFDocument doc = new HWPFDocument(fs.getRoot()); 
WordExtractor docExtractor = new WordExtractor(doc); 

作爲獎勵,NPOIFSFileSystem也稍快和更低的內存

+0

是的,這是現在的工作...我們可以使用NPOIFSFileSystem爲XWPFDocument(對於docx)。 – user3172677

+0

不,NPOIFS/POISF僅適用於像.doc這樣以前的基於OLE2的文檔。對於像.docx這樣的OOXML文檔,您需要根據所有文檔 – Gagravarr

+0

使用OPCPackage,感謝您的幫助.... – user3172677

相關問題