java读取word内容
Posted wzzkaifa
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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内容的主要内容,如果未能解决你的问题,请参考以下文章