码迷,mamicode.com
首页 > 编程语言 > 详细

java读取word内容

时间:2016-02-22 15:19:45      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:

暂时只写读取word内容的方法。

依赖的jar:

poi-3.9-20121203.jar
poi-ooxml-3.9-20121203.jar
xmlbeans-2.3.0.jar

 

package com.word;

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.extractor.WordExtractor;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;

/** 
 * @author QiaoJiafei 
 * @version 创建时间:2016年2月22日 上午11:30:04 
 * 类说明 
 */
public class OptionWord {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
    }
    
    public static StringBuffer readWord(String path) {
        String s = "";
        try {
            if(path.endsWith(".doc")) {
                InputStream is = new FileInputStream(new File(path));
                WordExtractor ex = new WordExtractor(is);
                s = ex.getText();
            }else if (path.endsWith("docx")) {
                OPCPackage opcPackage = POIXMLDocument.openPackage(path);
                POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
                s = extractor.getText();
            }else {
                System.out.println("传入的word文件不正确:"+path);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        StringBuffer bf = new StringBuffer(s);
        return bf;
    }

}

 

java读取word内容

标签:

原文地址:http://www.cnblogs.com/qiaoyeye/p/5206626.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!