【问题标题】:How to create HWPF document with apache poi如何使用 apache poi 创建 HWPF 文档
【发布时间】:2011-08-14 15:21:32
【问题描述】:

请有人帮助我将文本放入段落中。我有这个代码:

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

}

但问题是,如果我将“from”字符串直接放入范围,它将在结果文档中,但如果我创建一个段落并将其放入其中,则文档为空。即使我用 apache tika 及其 WordExtractor 处理它,它也一无所获。

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

如果我这样做:

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

and from 是“whatever”,然后在文档中开头有“w”(初始)字符...这是什么鬼?

【问题讨论】:

    标签: apache-poi hwpf


    【解决方案1】:

    尝试使用最近的 POI 夜间构建/svn 结帐。 Sergey 目前正在大力开发 HWPF 代码库,并且最近修复了您所描述的错误。

    【讨论】:

      猜你喜欢
      • 2012-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多