下载本文示例代码
很多人问到如何抽取word、excel、pdf等文件,在这里我总结一下抽取word、pdf的几种方法。 1、用jacob. 其实jacob是一个bridage,连接java和com或者win32函数的一个中间件,jacob并不能直接抽取word,excel等文件,需要自己写dll哦,不过已经有为你写好的了,就是jacob的作者一并提供了。 jacob下载: 下载了jacob并放到指定的路径之后(dll放到path,jar文件放到classpath),就可以写你自己的抽取程序了,下面是一个例子:
import java.io.File;import com.jacob.com.*;import com.jacob.activeX.*; public class FileExtracter{public static void main(String[] args) {ActiveXComponent app = new ActiveXComponent("Word.Application");String inFile = "c:\\test.doc";String tpFile = "c:\\temp.htm";String otFile = "c:\\temp.xml";boolean flag = false;try {app.setProperty("Visible", new Variant(false));Object docs = app.getProperty("document.").toDispatch();Object doc = Dispatch.invoke(docs,"Open", Dispatch.Method, new Object[]{inFile,new Variant(false), new Variant(true)}, new int[1]).toDispatch();Dispatch.invoke(doc,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);Variant f = new Variant(false);Dispatch.call(doc, "Close", f);flag = true;} catch (Exception e) {e.printStackTrace();} finally {app.invoke("Quit", new Variant[] {});}}} 2、用apache的poi来抽取word,excel poi是apache的一个项目,不过就算用poi你可能都觉得很烦,不过不要紧,这里提供了更加简单的一个接口给你: 下载经过封装后的poi包: 下载之后,放到你的classpath就可以了,下面是如何使用它的一个例子:
import java.io.*;import org.textmining.text.extraction.WordExtractor;/*** <p>Title: pdf extraction</p>* <p>Description: email:chris@matrix.org.cn</p>* <p>Copyright: Matrix Copyright (c) 2003</p>* <p>Company: Matrix.org.cn</p>* @author chris* @version 1.0,who use this example pls remain the declare*/public class PdfExtractor {public PdfExtractor() {}public static void main(String args[]) throws Exception{FileInputStream in = new FileInputStream ("c:\\a.doc");WordExtractor extractor = new WordExtractor();String str = extractor.extractText(in);System.out.println("the result length is" str.length());System.out.println("the result is" str);}}zmbbs=1;共2页。 1 2 :
很多人问到如何抽取word、excel、pdf等文件,在这里我总结一下抽取word、pdf的几种方法。 1、用jacob. 其实jacob是一个bridage,连接java和com或者win32函数的一个中间件,jacob并不能直接抽取word,excel等文件,需要自己写dll哦,不过已经有为你写好的了,就是jacob的作者一并提供了。 jacob下载: 下载了jacob并放到指定的路径之后(dll放到path,jar文件放到classpath),就可以写你自己的抽取程序了,下面是一个例子:
import java.io.File;import com.jacob.com.*;import com.jacob.activeX.*; public class FileExtracter{public static void main(String[] args) {ActiveXComponent app = new ActiveXComponent("Word.Application");String inFile = "c:\\test.doc";String tpFile = "c:\\temp.htm";String otFile = "c:\\temp.xml";boolean flag = false;try {app.setProperty("Visible", new Variant(false));Object docs = app.getProperty("document.").toDispatch();Object doc = Dispatch.invoke(docs,"Open", Dispatch.Method, new Object[]{inFile,new Variant(false), new Variant(true)}, new int[1]).toDispatch();Dispatch.invoke(doc,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);Variant f = new Variant(false);Dispatch.call(doc, "Close", f);flag = true;} catch (Exception e) {e.printStackTrace();} finally {app.invoke("Quit", new Variant[] {});}}} 2、用apache的poi来抽取word,excel poi是apache的一个项目,不过就算用poi你可能都觉得很烦,不过不要紧,这里提供了更加简单的一个接口给你: 下载经过封装后的poi包: 下载之后,放到你的classpath就可以了,下面是如何使用它的一个例子:
import java.io.*;import org.textmining.text.extraction.WordExtractor;/*** <p>Title: pdf extraction</p>* <p>Description: email:chris@matrix.org.cn</p>* <p>Copyright: Matrix Copyright (c) 2003</p>* <p>Company: Matrix.org.cn</p>* @author chris* @version 1.0,who use this example pls remain the declare*/public class PdfExtractor {public PdfExtractor() {}public static void main(String args[]) throws Exception{FileInputStream in = new FileInputStream ("c:\\a.doc");WordExtractor extractor = new WordExtractor();String str = extractor.extractText(in);System.out.println("the result length is" str.length());System.out.println("the result is" str);}}zmbbs=1;共2页。 1 2 :
下载本文示例代码
Java抽取Office、PDF的四件兵器Java抽取Office、PDF的四件兵器Java抽取Office、PDF的四件兵器Java抽取Office、PDF的四件兵器Java抽取Office、PDF的四件兵器Java抽取Office、PDF的四件兵器Java抽取Office、PDF的四件兵器Java抽取Office、PDF的四件兵器Java抽取Office、PDF的四件兵器Java抽取Office、PDF的四件兵器Java抽取Office、PDF的四件兵器Java抽取Office、PDF的四件兵器Java抽取Office、PDF的四件兵器Java抽取Office、PDF的四件兵器Java抽取Office、PDF的四件兵器
阅读(202) | 评论(0) | 转发(0) |