java POI解析word為文本內容


package com.github.tangyi.exam.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

public class TestWord {

    public static void main(String[] args) {
        File file = new File("E:\\yaoyu\\exam\\秦洲試題單選.docx");
        file = new File("E:\\yaoyu\\exam\\醫療單選整合后帶答案 (自動保存的).docx");
        XWPFDocument doc = null;
        POIXMLTextExtractor extractor = null;
        try {
            doc = new XWPFDocument(new FileInputStream(file));
            extractor = new XWPFWordExtractor(doc);
            String content = extractor.getText();
            System.out.println(content);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (doc != null) {
                    doc.close();
                }
                if (extractor != null) {
                    extractor.close();
                }
            } catch (IOException e) {
            }
        }
    }

}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM