2011-08-14 83 views
1

請有人幫我將文本放入段落中。我有這樣的代碼:如何使用apache poi創建HWPF文檔

private void createDOCDocument(String from, File file) throws Exception { 

    POIFSFileSystem fs = new POIFSFileSystem(DOCGenerator.class.getClass().getResourceAsStream("/poi/template.doc")); 
    HWPFDocument doc = new HWPFDocument(fs); 
    Range range = doc.getRange(); 
    Paragraph par1 = range.insertAfter(new ParagraphProperties(), 0);  
    CharacterRun run1 = par1.insertAfter(from); 
    run1.setFontSize(11); 
    DocumentSummaryInformation dsi = doc.getDocumentSummaryInformation(); 
    CustomProperties cp = dsi.getCustomProperties(); 
    if (cp == null) 
     cp = new CustomProperties(); 
    cp.put("myProperty", "foo bar baz"); 
    dsi.setCustomProperties(cp); 
    doc.write(new FileOutputStream(file)); 

} 

但問題是,如果我把「從」串直接進入的範圍內,這將是生成的文檔中,但如果我創建了一個段落,並把它放在那裏,而不是,該文件是空的。即使我用apache tika和它的WordExtractor處理它,也沒有任何結果。

btw /poi/template.doc是空文檔。

如果我不喜歡這樣寫道:

Paragraph par1 = range.getParagraph(0); 
CharacterRun run1 = par1.insertAfter(from); 

和是「無所謂」,那麼在文件中有「W」(初始)開頭的字符......這到底是什麼?

回答

4

嘗試使用最近的每晚構建/ svn結帳POI。 HWPF代碼庫目前正在由Sergey進行大量的工作,並且像你剛剛描述的那個bug最近已經修復。

相關問題