用java读取word2003和word2007的内容

需要使用apache的poi开源项目包;下载地址:http://poi.apache.org/download.html

引入其中的6个包poi-3.8-20120326.jar;poi-ooxml-3.8-20120326.jar;poi-ooxml-schemas-3.8-20120326.jar;poi-scratchpad-3.8-20120326.jar;xmlbeans-2.3.0.jar;dom4j-1.6.1.jar。


java的函数代码如下:

    private void readWord2007() {  
        try {
            OPCPackage oPCPackage = POIXMLDocument.openPackage("e:/test.docx");
            XWPFDocument xwpf = new XWPFDocument(oPCPackage);
            POIXMLTextExtractor ex = new XWPFWordExtractor(xwpf);
            System.out.print(ex.getText());
        } catch (FileNotFoundException e) {
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace(); 
        }  
    }
    
    private void readWord2003() {
        try {
            FileInputStream fis = new FileInputStream("e:/test.doc");
            WordExtractor wordExtractor = new WordExtractor(fis);
            System.out.println(wordExtractor.getText());
        } catch (FileNotFoundException e) {
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace(); 
        }  
    } 

你可能感兴趣的:(java,apache)