首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >一些HWPF POI文档构建示例

一些HWPF POI文档构建示例
EN

Stack Overflow用户
提问于 2012-11-19 14:23:30
回答 1查看 11.1K关注 0票数 0

我正在寻找用POI构建非平凡Word (97-2003)文档的例子。我已经用"Hello“创建了一个:

代码语言:javascript
复制
package com.mygroup.myapp.poi.word;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

import org.apache.log4j.Logger;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Range;

public class DocFileWriter {

    private static final Logger LOGGER = Logger.getLogger(DocFileWriter.class);
    private static final String FILE_EXTENSION = ".doc";
    private static final URL EMPTY_DOC_URL = DocFileWriter.class.getClassLoader().getResource("empty.doc");
    private String pathname;
    private HWPFDocument document;

    /**
     * Constructor
     * @param pathname the target path name (e.g.: "/tmp/test.doc", etc.)
     * @throws IOException 
     */
    public DocFileWriter(String pathname) throws IOException {
        if (!pathname.endsWith(FILE_EXTENSION)) {
            throw new RuntimeException("The file name must ends with " + FILE_EXTENSION);
        }
        this.pathname = pathname;
        try {
            document = new HWPFDocument(EMPTY_DOC_URL.openStream());
        } catch (IOException e) {
            LOGGER.error("Empty document resource missing");
            throw e;
        }
    }


    /**
     * Adds a "Hello World!" to the document.
     */
    public void addHelloWorld() {
        Range range = document.getRange();
        CharacterRun charRun = range.insertBefore("Hello World!");
        charRun.setFontSize(18);
        charRun.setItalic(true);
    }

    /**
     * Writes the document on disk.
     */
    public void writeDocument() {
        try {
            document.write(new FileOutputStream(new File(pathname)));
        } catch (FileNotFoundException e) {
            LOGGER.error("The file cannot be created", e);
        } catch (IOException e) {
            LOGGER.error("Unable to write the document", e);
        }
    }
}

现在我想补充一句:

  • 一幅画
  • 空白页
  • 标题(只有一个字符串)
  • 页脚(只有一个字符串)
  • 表(10行,3列)

你有一些关于这方面的建议/例子吗?

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-27 14:14:43

正如所指出的,这里 HWPF是POI的一个孤立子项目。没有办法从头开始编写复杂的旧.doc文件。添加图片/头/页脚/表仅由XWPF和.docx格式管理。

因此,我选择使用RTF (带有.doc扩展)。以下是构建RTF报告的一些解决方案:

  • http://rtftemplate.sourceforge.net/
  • http://code.google.com/p/jrtf/
  • http://itextrtf.sourceforge.net/
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13455762

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档