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

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

问题描述

如何使用相同的名称使用POI API来保存文档文件

 公共类readDoc {
    公共静态无效的主要(字串[] args){
        字符串filesname =Hello.doc;
        POIFSFileSystem FS =无效;
        尝试{
            FS =新POIFSFileSystem(新的FileInputStream(filesname;
            //不能在最后的关闭括号
            我的网站没有允许它关闭
            HWPFDocument DOC =新HWPFDocument(FS);
            WordExtractor我们=新WordExtractor(DOC);
            的String [] =段we.getParagraphText();
            的System.out.println(Word文档有+
            paragraphs.length +段落);
            的for(int i = 0; I<段落。长度;我++){
                的System.out.println(持续时间+段落[I]。长度());
            }
        }
        赶上(例外五){
            e.printStackTrace();
        }
    }
}


解决方案

 的FileOutputStream FOS =新的FileOutputStream(filesname);
doc.write(FOS);
fos.close();

您可以尝试写入 POIFsFileSystem 以及

 的FileOutputStream FOS =新的FileOutputStream(filesname);
fs.write(FOS);
fos.close();

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

You could try writing to the POIFsFileSystem as well.

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

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

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