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