首页 > 代码库 > java读取word内容
java读取word内容
package word;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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;
}
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.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内容
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。