有些HWPF POI文档构建例子 [英] Some HWPF POI document building examples

查看:370
本文介绍了有些HWPF POI文档构建例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要找的建设不平凡的字(97-2003)与 POI 文件的例子。我已经达到了创建一个以Hello World的:

I'm looking for examples of building non-trivial Word (97-2003) documents with POI. I already reached to create one with "Hello World":

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

你会有一些指针/有关的例子吗?

Would you have some pointers/examples about that?

感谢您。

推荐答案

如图所示这里 HWPF是POI的孤儿子项目。有没有办法从头编写复杂的旧的.doc文件。添加图片/页眉/页脚/表只能由XWPF和.DOCX格式的管理。

As indicated here HWPF is an orphan subproject of POI. There's no way to write complex old .doc files from scratch. Adding pictures/headers/footers/tables is only managed by XWPF and .docx format.

所以我选择用RTF(扩展名为.doc)。这里有一些解决方案来构建RTF报告:

So I chose to use RTF (with a .doc extension). Here are some solution to build RTF reports:

  • http://rtftemplate.sourceforge.net/
  • http://code.google.com/p/jrtf/
  • http://itextrtf.sourceforge.net/

这篇关于有些HWPF POI文档构建例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆