How to save the Word Document using POI API?

僤鯓⒐⒋嵵緔 提交于 2019-12-24 16:22:21

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/8484906/how-to-save-the-word-document-using-poi-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!