如何使用 POI API 保存 Word 文档? [英] How to save the Word Document using POI API?

查看:44
本文介绍了如何使用 POI API 保存 Word 文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用POI API保存同名的Doc文件

how to save the Doc file using the same name using POI API

public class readDoc {
    public static void main( String[] args ) {
        String filesname = "Hello.doc";
        POIFSFileSystem fs = null;
        try {
            fs = new POIFSFileSystem(new FileInputStream(filesname; 
            //Couldn't close the braces at the end as 
            my site did not allow it to close
            HWPFDocument doc = new HWPFDocument(fs);
            WordExtractor we = new WordExtractor(doc);
            String[] paragraphs = we.getParagraphText();
            System.out.println( "Word Document has " +
            paragraphs.length + " paragraphs" );
            for( int i=0; i<paragraphs .length; i++ ) {
                System.out.println("Length:"+paragraphs[i].length());
            }
        }
        catch(Exception e) { 
            e.printStackTrace();
        }
    }
}

推荐答案

FileOutputStream fos = new FileOutputStream(filesname);
doc.write(fos);
fos.close();

您也可以尝试写入 POIFsFileSystem.

FileOutputStream fos = new FileOutputStream(filesname);
fs.write(fos);
fos.close(); 

这篇关于如何使用 POI API 保存 Word 文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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