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) {
            }
        }
    }

}

  

posted on 2020-06-10 00:45  yaoyu  阅读(731)  评论(0编辑  收藏  举报

导航