HWPF操作Word

来源:互联网 发布:电脑视频剪辑软件中文 编辑:程序博客网 时间:2024/06/18 04:07

1、HWPFDocument通过读入一个已存在DOC文件,加载一个Word文档【似乎没有发现直接创建一个文档的方法】

如:

 FileInputStream in = new FileInputStream("C:\\blank.doc");

 HWPFDocument doc = new HWPFDocument(in);

2、Range、Paragraph、Section是操作Word重要的区域操作对象【具体说明待更新】

Range一般用于比较大的范围

Paragraph是一个段落

Section任意的范围

如:Range range = doc.getRange();

3、Word有很多样式,CharacterProperties、ParagraphProperties【具体说明待更新】

其中:

CharacterProperties对应===>

ParagraphProperties对应===>

4、标题多级样式需要用HWPFList来实现【具体说明待更新】



下面是一个简单的例子,简单说明如何用HWPF操作Word的样式

引用一下:http://bbs.csdn.net/topics/290046899中6楼songnian1983的例子

结果在一个空白的文档输出一下内容:

例子代码引用:http://bbs.csdn.net/topics/290046899【6楼】

public static void main(String[] args) throws Exception {
        try {
            //加载一个空白的Word文档
            FileInputStream in = new FileInputStream("file/123.doc");
            HWPFDocument doc = new HWPFDocument(in);
            //获取文档内容区域
            Range range = doc.getRange();
            //新建字体样式
            CharacterProperties props = new CharacterProperties();
            Range currentRange = range;
            // Slowly increase the font size
            for (int x = 8; x <= 64; x += 4) {
                // Set the half point size of the font
                props.setFontSize(x);
                currentRange = currentRange.insertAfter(" Hello World!", props);
            }
            
            // Display Bold characters
            props.setBold(true);
            currentRange = currentRange.insertAfter(" Bold", props);

            // Display Italic characters
            props.setItalic(true);
            currentRange = currentRange.insertAfter(" Italic", props);

            // Display charcters with a Double Strikethrough
            props.setDoubleStrikeThrough(true);
            currentRange = currentRange.insertAfter(" Double Strikethrough",props);

            // Insert an empty paragraph for readability
            currentRange = currentRange.insertAfter(new ParagraphProperties(),0);

            // Reset the character properties
            props = new CharacterProperties();
            props.setFontSize(32);

            // Create a numbered list
            HWPFList list = new HWPFList(true,doc.getStyleSheet());
            int listID = doc.registerList(list);
            /**
             *底层包poi-scratchpad-3.9-20121203.jar
             *类org.apache.poi.hwpf.model.PlfLfo @方法 void add( LFO lfo, LFOData lfoData)
             *_rgLfo[_lfoMac + 1] = lfo;
             *_rgLfoData[_lfoMac + 1] = lfoData;
             *这两句代码有明显BUG,需修改之
             **/
            // Insert a list entry
            currentRange = currentRange.insertAfter(new ParagraphProperties(),listID, 1, 0);
            props.setIco24(0xff0000);
            currentRange = currentRange.insertAfter(" Blue list entry", props);

            // Insert another list entry
            currentRange = currentRange.insertAfter(new ParagraphProperties(),
                    listID, 1, 0);
            props.setIco24(0xff);
            props.setFontSize(38);
            props.setCapitalized(true);
            currentRange = currentRange.insertAfter(" larger red capitalized",props);

            // Last list entry
            currentRange = currentRange.insertAfter(new ParagraphProperties(),listID, 1, 0);
            props.setIco24(0);
            props.setCapitalized(false);
            props.setCharacterSpacing(150);
            props.setOutline(true);
            currentRange = currentRange.insertAfter(" Large character spacing",props);

            // Write out the document
            FileOutputStream out = new FileOutputStream("file/hello.doc");
            doc.write(out);
            out.flush();
            out.close();
        }
        catch (Throwable t){
            t.printStackTrace();
        }
    }

0 0
原创粉丝点击