网上找来的用java读取word文档的代码

public class WordTest {

    public static void main(String[] args){

        FileOutputStream ostream=null;
        FileInputStream  istream=null;
        try{
            istream=new FileInputStream("c://test.doc");

            HWPFDocument doc=new HWPFDocument(istream);
            ostream =new FileOutputStream("C://testout.doc");


            CharacterProperties props = new CharacterProperties();

            Range range=doc.getRange();
            // Slowly increase the font size
            for (int x = 8; x <= 64; x += 4){
              // Set the half point size of the font
              props.setFontSize(x);
              range.insertAfter("Test write information into file",props);
              range.insertAfter("/n/r");
            }

            // Display Bold characters
            props.setBold(true);
            range.insertAfter(" Bold", props);
            range.insertAfter("/n/r");

            // Display Italic characters
            props.setItalic(true);
            range.insertAfter(" Italic", props);
            range.insertAfter("/n/r");

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

            // Insert an empty paragraph for readability
            range.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);

            // Insert a list entry

            range.insertAfter(new ParagraphProperties(), listID, 1,0);
            props.setIco24(0xff0000);
            range.insertAfter(" Blue list entry", props);

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

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

            doc.write(ostream);
            ostream.flush();
        }
        catch(IOException ioe){
            ioe.printStackTrace();
        }
        finally{
             try{
                if(ostream!=null){
                    ostream.close();
                }
                if(istream!=null){
                    istream.close();
                }
             }
            catch(IOException ioe){
                ioe.printStackTrace();
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值