HWPF操作Word

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

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值