日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

POI读取word文件,(支持HSSF和XSSF两种方式)

發布時間:2025/3/12 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POI读取word文件,(支持HSSF和XSSF两种方式) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

POI讀取word文件,(支持HSSF和XSSF兩種方式)

參考:HSSF,XSSF,SXSSF三種方式

1.引用maven(版本必須一致)

<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.1.1</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.1</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-scratchpad</artifactId><version>4.1.1</version></dependency>

2.讀取word

public class POIUtil {/*** @Description: POI 讀取 word* @create: 2019-07-27 9:48* @update logs*/public static String readWord(String path) throws Exception {// WordExtractor extractor = new WordExtractor(is);String content = null;File file = new File(path);if (file.exists() && file.isFile()) {InputStream is = null;XWPFDocument xwpfDocument = null;POIXMLTextExtractor extractor = null;HWPFDocument hwpfDocument = null;WordExtractor wordExtractor = null;try {is = new FileInputStream(file);xwpfDocument = new XWPFDocument(is);extractor = new XWPFWordExtractor(xwpfDocument);// 文檔文本內容content = extractor.getText(); // // 文檔圖片內容 // List<XWPFPictureData> pictures = docx.getAllPictures(); // for (XWPFPictureData picture : pictures) { // byte[] bytev = picture.getData(); // // 輸出圖片到磁盤 // FileOutputStream out = new FileOutputStream( // "D:\\temp\\temp\\" + UUID.randomUUID() + picture.getFileName()); // out.write(bytev); // out.close(); // }} catch (FileNotFoundException e) {} catch (IOException e) {} catch (OLE2NotOfficeXmlFileException e) {//較低版本的word文件is = new FileInputStream(file);hwpfDocument = new HWPFDocument(is);wordExtractor = new WordExtractor(hwpfDocument);// 文檔文本內容content = wordExtractor.getText();} finally {try {if (extractor != null) {extractor.close();}if (xwpfDocument != null) {xwpfDocument.close();}if (wordExtractor != null) {wordExtractor.close();}if (hwpfDocument != null) {hwpfDocument.close();}if (is != null) {is.close();}} catch (IOException e) {}}}return content;}public static void main(String[] args) {String path = "/Users/jj/Desktop/勝多負少的范德薩.doc"; // String path = "/Users/jj/Desktop/測試1 2.doc"; // String path = "/Users/jj/Desktop/測試1.docx";try {System.out.println(readWord(path));} catch (Exception e) {e.printStackTrace();}}}

總結

以上是生活随笔為你收集整理的POI读取word文件,(支持HSSF和XSSF两种方式)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。