Как сохранить документ Word с помощью POI API? - PullRequest
1 голос
/ 13 декабря 2011

как сохранить файл Doc под тем же именем с помощью 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 Ответ

2 голосов
/ 13 декабря 2011
FileOutputStream fos = new FileOutputStream(filesname);
doc.write(fos);
fos.close();

Вы также можете попробовать записать в POIFsFileSystem.

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