Java读取word文档

package com.zhihua.poi.readWord;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

public class ReadWordUtils {

    /**
     * 读取word文档
     * <请替换成功能描述> <br>
     * <请替换成详细描述>
     * @param fileName 带有完整路径的文件名
     * @author caizh
     * @since [1.0.0]
     * @version [1.0.0,2017年2月10日]
     */
    public static String readWord(String fileName){

        File file = new File(fileName);
        if(file.getName().endsWith(".doc")){
            return readWord_2003(file);
        }else if(file.getName().endsWith(".docx")){
            return readWord_2007(fileName);
        }else{
            System.out.println("该文件不是word文档,请重新选择!");
            return "";
        }
    }

    /**
     * 读取2003版本的word文档 .doc
     * <请替换成功能描述> <br>
     * <请替换成详细描述>
     * @param file 带有完整路径的文件名
     * @author caizh
     * @since [1.0.0]
     * @version [1.0.0,2017年2月10日]
     */
    @SuppressWarnings("resource")
    public static String readWord_2003(File file){
        String text = "";          
        try{
            InputStream stream = new FileInputStream(file);
            HWPFDocument document = new HWPFDocument(stream);
            WordExtractor word = new WordExtractor(document);
            text = word.getText();
            //去掉word文档中的多个换行
            text = text.replaceAll("(\\r\\n){2,}", "\r\n");
            text = text.replaceAll("(\\n){2,}", "\n");
            System.out.println("读取Word文档成功!");
            stream.close();
            return text;
        }catch(Exception e){
            e.printStackTrace();           
        }
        return "";
    }

    /**
     * 读取2007版本的word文档 .docx
     * <请替换成功能描述> <br>
     * <请替换成详细描述>
     * @param fileName 带有完整路径的文件名
     * @author caizh
     * @since [1.0.0]
     * @version [1.0.0,2017年2月10日]
     */
    @SuppressWarnings("resource")
    public static String readWord_2007(String fileName){
        String text = "";
        try{
            OPCPackage oPCPackage =POIXMLDocument.openPackage(fileName);
            XWPFDocument xwpf = new XWPFDocument(oPCPackage);
            POIXMLTextExtractor ex = new XWPFWordExtractor(xwpf);
            text = ex.getText();
            //去掉word文档中的多个换行  
            text = text.replaceAll("(\\r\\n){2,}", "\r\n");  
            text = text.replaceAll("(\\n){2,}", "\n"); 
            System.out.println("读取Word文档成功!");
            return text;
        }catch(Exception e){
            e.printStackTrace();
        }
        return "";
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值