poi讀取word的內容,poi讀取word內容

來源:互聯網
上載者:User

poi讀取word的內容,poi讀取word內容

pache POI是Apache軟體基金會的開放源碼函式庫,POI提供API給Java程式對Microsoft Office格式檔案讀和寫的功能。

1.讀取word 2003及word 2007需要的jar包

  讀取 2003 版本(.doc)的word檔案相對來說比較簡單,只需要 poi-3.5-beta6-20090622.jar 和 poi-scratchpad-3.5-beta6-20090622.jar 兩個 jar 包即可, 而 2007 版本(.docx)就麻煩多,我說的這個麻煩不是我們寫代碼的時候麻煩,是要匯入的 jar 包比較的多,有如下 7 個之多:
 1. openxml4j-bin-beta.jar
 2. poi-3.5-beta6-20090622.jar
 3. poi-ooxml-3.5-beta6-20090622.jar
 4 .dom4j-1.6.1.jar
 5. geronimo-stax-api_1.0_spec-1.0.jar
 6. ooxml-schemas-1.0.jar
 7. xmlbeans-2.3.0.jar
其中 4-7 是 poi-ooxml-3.5-beta6-20090622.jar 所依賴的 jar 包(在 poi-bin-3.5-beta6-20090622.tar.gz 中的 ooxml-lib 目錄下可以找到)。

2.讀取.doc的word的文檔

public static String readDoc(File file) {

System.out.println(file.getAbsolutePath() + "....doc");
String text = "";
FileInputStream in;
try {
in = new FileInputStream(file);
WordExtractor extractor = new WordExtractor(in);
text = extractor.getText();
in.close();
} catch (Exception e) {
System.out.println("Word解析錯誤!");
}
return text;

}

3、讀取.docx的word的文檔

public static String readDocx(File file) {
System.out.println(file.getAbsolutePath() + "....docx");
String str = "";
try {
FileInputStream fis = new FileInputStream(file);
XWPFDocument xdoc = new XWPFDocument(fis);
XWPFWordExtractor extractor = new XWPFWordExtractor(xdoc);
str = extractor.getText();
System.out.println(str);
fis.close();
} catch (Exception e) {
e.printStackTrace();
}

return str;

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.