java读取word文档里面的内容(包括doc和docx格式)
java读取word文档里面的内容(包括doc和docx格式),使用POI架包
使用的POI架包如下
poi-3.16.jar
poi-examples-3.16.jar
poi-excelant-3.16.jar
poi-ooxml-3.16.jar
poi-ooxml-schemas-3.16.jar
poi-scratchpad-3.16.jar
目前POI的最新发布版本是3.10_FINAL.该版本保护的jar包有:
很多人都困惑POI那么多Jar到底应该导入哪一个。
实际上很多时候我们只利用POI来操作Excel。甚至只用xls这一种格式。
那么就没有必要全部都导入了。具体应该使用哪个JAR包请参考以下内容:
当我们只要使用xls格式时、只要导入poi-version-yyyymmdd.jar就可以了。
当我们还要使用xlsx格式、还要导入poi-ooxml-version-yyyymmdd.jar。
至于poi-ooxml-schemas-version-yyyymmdd.jar这个jar基本不太会用到的。
当我们需要操作word、ppt、viso、outlook等时需要用到poi-scratchpad-version-yyyymmdd.jar。
读取word文档中,doc后缀的文件
直接上代码
//读取word文档中,doc后缀的文件
public static List<String> searchWordDoc(String fileUrl){
List<String> docList = new ArrayList<String>();
String content=null;
//读取字节流,读取文件路径
InputStream input = null;
try {
input = new FileInputStream(new File(request.getSession().getServletContext().getRealPath(fileUrl)));
WordExtractor wex = new WordExtractor(input);
content = wex.getText();
//System.out.println(content);
docList.add(content);
} catch (Exception e) {
e.printStackTrace();
}
return docList;
}
将文件路径传进来,最后得到的使一个List集合,是一行一行的读取,每一行的内容对应List的下标
读取word文档中,docx后缀的文件
直接上代码
public static List<String> searchWordDocX(String fileUrl){
//读取文件路径
OPCPackage opcPackage = null;
String content = null;
List<String> docxList = new ArrayList<String>();
try {
opcPackage = POIXMLDocument.openPackage(request.getSession().getServletContext().getRealPath(fileUrl));
XWPFDocument xwpf = new XWPFDocument(opcPackage);
POIXMLTextExtractor poiText = new XWPFWordExtractor(xwpf);
content = poiText.getText();
docxList.add(content);
} catch (IOException e) {
e.printStackTrace();
}
return docxList;
}
将文件路径传进来,最后得到的使一个List集合,是一行一行的读取,每一行的内容对应List的下标
最后附上jar包下载位置
百度网盘:https://pan.baidu.com/s/1Y4BLto_mEUkS7Xhx1GJmzA
提取码:vor5