国外网站代做,wordpress 豆瓣评分,苏州谢谢网络公司,163免费邮箱登录入口在上一篇文章中 #xff0c;lucene只能全文检索word2003#xff0c;无法检索2007#xff0c;并且只能加载部分内容#xff0c;无法加载全文内容。为解决此问题#xff0c;找到了如下方法 POI 读取word (word 2003 和 word 2007) 最近在给客户做系统的时候#xff0c;用户… 在上一篇文章中 lucene只能全文检索word2003无法检索2007并且只能加载部分内容无法加载全文内容。为解决此问题找到了如下方法 POI 读取word (word 2003 和 word 2007) 最近在给客户做系统的时候用户提出需求要能够导入 word 文件现在 microsoft word 有好几个版本 97、2003、2007的这三个版本存储数据的格式上都有相当大的差别而现在 97 基本上已经退出市场几乎没有人用这个版本了 所以在我们的系统中只考虑 2003 版本和 2007 版本的因为我们只要求能够读取 word 中的文字内容即可其中的文字样式、图片等信息可以忽略也不用直接操作 word 文件 所以我们选择 用 apache 的 POI 进行读取。 读取 2003 版本(.doc)的word文件相对来说比较简单只需要 poi-3.5-beta6-20090622.jar 和 poi-scratchpad-3.5-beta6-20090622.jar 两个 jar 包即可 而 2007 版本(.docx)就麻烦多我说的这个麻烦不是我们写代码的时候麻烦是要导入的 jar 包比较的多有如下 7 个之多 1. openxml4j-bin-beta.jar 2. poi-3.5-beta6-20090622.jar 3. poi-ooxml-3.5-beta6-20090622.jar 4 .dom4j-1.6.1.jar 5. geronimo-stax-api_1.0_spec-1.0.jar 6. ooxml-schemas-1.0.jar 7. xmlbeans-2.3.0.jar其中 4-7 是 poi-ooxml-3.5-beta6-20090622.jar 所依赖的 jar 包在 poi-bin-3.5-beta6-20090622.tar.gz 中的 ooxml-lib 目录下可以找到。 编写代码之前我们得先下载所需要的 jar 包 我们只需下载 poi-bin-3.5-beta6-20090622.tar.gz 和 openxml4j-bin-beta.jar 即可因为所需要的其他 jar 包都能在 poi-bin-3.5-beta6-20090622.tar.gz 中找到, 下面是下载地址poi-bin-3.5-beta6-20090622.tar.gzhttp://apache.etoak.com/poi/dev/bin/poi-bin-3.5-beta6-20090622.tar.gzopenxml4j-bin-beta.jarhttp://mirror.optus.net/sourceforge/o/op/openxml4j/openxml4j-bin-beta.jar 下方是读取 word 文件的 Java 代码值得注意的是 POI 在读取 word 文件的时候不会读取 word 文件中的图片信息 还有就是对于 2007 版的 word(.docx), 如果 word 文件中有表格所有表格中的数据都会在读取出来的字符串的最后。 import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;/*** POI 读取 word 2003 和 word 2007 中文字内容的测试类br /* createDate 2009-07-25* author Carl He*/
public class Test {public static void main(String[] args) {try {//word 2003 图片不会被读取InputStream is new FileInputStream(new File(c://files//2003.doc));WordExtractor ex new WordExtractor(is);String text2003 ex.getText();System.out.println(text2003);//word 2007 图片不会被读取 表格中的数据会被放在字符串的最后OPCPackage opcPackage POIXMLDocument.openPackage(c://files//2007.docx);POIXMLTextExtractor extractor new XWPFWordExtractor(opcPackage);String text2007 extractor.getText();System.out.println(text2007);} catch (Exception e) {e.printStackTrace();}}
}找到方法后我们对上一篇文章indexer.java的源码进行更改新增函数getDocument2007getDocument2003 本版本lucene是4.9 public static Document getDocument2007(File file) throws Exception {String docPath file.getAbsolutePath();String title file.getName();// 鍒涘缓DocumentDocument document new Document();OPCPackage opcPackage POIXMLDocument.openPackage(docPath);POIXMLTextExtractor extractor new XWPFWordExtractor(opcPackage);String cont extractor.getText();document.add(new StringField(filename, title, Field.Store.YES));//TOKENIZED//document.add(new Field(contents, contents));document.add(new TextField(contents, cont,Field.Store.YES));document.add(new TextField(path, docPath, Field.Store.YES));document.add(new StringField(indexDate,DateTools.dateToString(new Date(), DateTools.Resolution.DAY),Field.Store.YES));return document;}public static Document getDocument2003(File file) throws Exception {String docPath file.getAbsolutePath();String title file.getName();// 鍒涘缓DocumentDocument document new Document();InputStream is new FileInputStream(new File(docPath));WordExtractor ex new WordExtractor(is);//is鏄疻ORD鏂囦欢鐨処nputStream String cont ex.getText();document.add(new StringField(filename, title, Field.Store.YES));//TOKENIZEDdocument.add(new TextField(contents, cont,Field.Store.YES));document.add(new TextField(path, docPath, Field.Store.YES));document.add(new StringField(indexDate,DateTools.dateToString(new Date(), DateTools.Resolution.DAY),Field.Store.YES));return document;}同时修改for循环中的读取文件 if(files[i].getName().endsWith(.doc)){ doc getDocument2003(files[i]); }else if(files[i].getName().endsWith(.docx)){ doc getDocument2007(files[i]);} 转载于:https://www.cnblogs.com/zzlp/p/4757568.html