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

java读取word内容

时间:2014-07-08 21:37:20      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:java   word   2003   2007   读取内容   

package word;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.IOException;


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

public class JavaWord {


/**
* @Title: main
* @Description:
* @param:
* @return void 
* @user: wangzg
* @Date:2014-7-3
* @throws
*/
public static void main(String[] args) {

readWord2003("doc\\wzg.doc");

//readWord2007("doc\\wzg1.docx");
}



/**

* @Title: readWord2003
* @Description:
* @param:
* @return String 
* @user: wangzg
* @Date:2014-7-4
* @throws
*/
public static String readWord2003(String filePath) {     
        FileInputStream fis;
        HWPFDocument doc;
        String text = null;
try {
File f = new File(filePath);
fis = new FileInputStream(f);
doc = new HWPFDocument(fis);
Range rang = doc.getRange();     
text = rang.text();  


       System.out.println(text);
       
       fis.close();     
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}        
        return text;     
    } 

/**

* @Title: readWord2007
* @Description:
* @param:
* @return String 
* @user: wangzg
* @Date:2014-7-4
* @throws
*/
public static String readWord2007(String filePath){

String text = null;
try {
           OPCPackage oPCPackage = POIXMLDocument.openPackage(filePath);
           XWPFDocument xwpf = new XWPFDocument(oPCPackage);
           POIXMLTextExtractor ex = new XWPFWordExtractor(xwpf);
           text = ex.getText();
           System.out.println(text);
           oPCPackage.close();
       } catch (FileNotFoundException e) {
           e.printStackTrace();  
       } catch (IOException e) {  
           e.printStackTrace(); 
       } 
return text;
}

}


详情:请查看poi官网——http://poi.apache.org/


java读取word内容,布布扣,bubuko.com

java读取word内容

标签:java   word   2003   2007   读取内容   

原文地址:http://blog.csdn.net/sd_tz_wzg/article/details/37500123

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