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

java读取word内容

时间:2017-04-17 16:11:17      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:return   wpf   title   user   xtext   import   main   doc   package   

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内容

标签:return   wpf   title   user   xtext   import   main   doc   package   

原文地址:http://www.cnblogs.com/wzzkaifa/p/6723027.html

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