站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题Java抽取Office、PDF的四件兵器

Java抽取Office、PDF的四件兵器

摘要: 很多人问到如何抽取word、excel、pdf等文件,在这里我总结一下抽取word、pdf的几种方法
  很多人问到如何抽取word、excel、pdf等文件,在这里我总结一下抽取word、pdf的几种方法。

<iframe align=right marginWidth=0 marginHeight=0 src="http://images.chinabyte.com/adjs/iframe-pip/y-software-pip.html" frameBorder=0 width=360 scrolling=no height=300></iframe>  1、用jacob.

  其实jacob是一个bridage,连接java和com或者win32函数的一个中间件,jacob并不能直接抽取word,excel等文件,需要自己写dll哦,不过已经有为你写好的了,就是jacob的作者一并提供了。

  jacob下载:http://www.matrix.org.cn/down_view.asp?id=13

  下载了jacob并放到指定的路径之后(dll放到path,jar文件放到classpath),就可以写你自己的抽取程序了,下面是一个例子:

<table width="90%" bgColor=#ffffff border=0> <tr> <td>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[] {});
}

}
}</td></tr></table>
  2、用apache的poi来抽取word,excel

  poi是apache的一个项目,不过就算用poi你可能都觉得很烦,不过不要紧,这里提供了更加简单的一个接口给你:

  下载经过封装后的poi包:http://www.matrix.org.cn/down_view.asp?id=14

  下载之后,放到你的classpath就可以了,下面是如何使用它的一个例子:

<table width="90%" bgColor=#ffffff border=0> <tr> <td>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);
}
}</td></tr></table>
zmbbs=1;



↑返回目录
前一篇: 用Jbuilder9开发媒体播放器
后一篇: JAR文件包及jar命令详解