1. 用jacob
其实jacob是一个bridage,连接java和com或者win32函数的一个中间件,jacob并不能直接抽取word,excel等文件,需要自己写dll哦,不过已经有为你写好的了,就是jacob的作者一并提供了。
jacob jar与dll文件下载: http://
下载了jacob并放到指定的路径之后(dll放到path,jar文件放到classpath),就可以写你自己的抽取程序了,下面是一个简单的例子:
Java代码
import
import com.*;
import com.jacob.activeX.*;
/**
* Title: pdf extraction
* Description: email:chris@
* Copyright: Matrix Copyright (c) 2003
* Company:
* @author chris
* @version 1.0,who use this example pls remain the declare
*/
public class FileExtracter{
public static void main(String[] args) {
ActiveXComponent component = new ActiveXComponent("Word.Application");
String inFile = "c:\\test.doc";
String tpFile = "c:\\temp.htm";
String otFile = "c:\\temp.xml";
boolean flag = false;
try {
component.setProperty("Visible", new Variant(false));
Object wordacc = component.getProperty("document.").toDispatch();
Object wordfile = Dispatch.invoke(wordacc,"Open", Dispatch.Method,
new Object[]{inFile,new Variant(false), new Variant(true)},
new int[1] ).toDispatch();
Dispatch.invoke(wordfile,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);
Variant f = new Variant(false);
Dispatch.call(wordfile, "Close", f);
flag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
component.invoke("Quit", new Variant[] {});
}
}
}
import
import com.*;
import com.jacob.activeX.*;
/**
* Title: pdf extraction
* Description: email:chris@
* Copyright: Matrix Copyright (c) 2003
* Company:
* @author chris
* @version 1.0,who use this example pls remain the declare
*/
public class FileExtracter{
public static void main(String[] args) {
ActiveXComponent component = new ActiveXComponent("Word.Application");
String inFile = "c:\\test.doc";
String tpFile = "c:\\temp.htm";
String otFile = "c:\\temp.xml";
boolean flag = false;
try {
component.setProperty("Visible", new Variant(false));
Object wordacc = component.getProperty("document.").toDispatch();
Object wordfile = Dispatch.invoke(wordacc,"Open", Dispatch.Method,
new Object[]{inFile,new Variant(false), new Variant(true)},
new int[1] ).toDispatch();
Dispatch.invoke(wordfile,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);
Variant f = new Variant(false);
Dispatch.call(wordfile, "Close", f);
flag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
component.invoke("Quit", new Variant[] {});
}
}
}
2. 用apache的poi来抽取word,excel。
poi是apache的一个项目,不过就算用poi你可能都觉得很烦,不过不要紧,这里提供了更加简单的一个接口给你:
下载经过封装后的poi包: http://jakarta.
下载之后,放到你的classpath就可以了,下面是如何使用它的一个例子:
Java代码
import *;
import org.textmining.text.extraction.WordExtractor;
/**
* <p>Title: word extraction</p>
* <p>Description: email:chris@
* <p>Copyright: Matrix Copyright (c) 2003</p>
* <p>Company:
* @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);
}
}
import *;
import org.textmining.text.extraction.WordExtractor;
/**
* <p>Title: word extraction</p>
* <p>Description: email:chris@
* <p>Copyright: Matrix Copyright (c) 2003</p>
* <p>Company:
* @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);
}
}
3. pdfbox-用来抽取pdf文件
但是pdfbox对中文支持还不好,先下载pdfbox: http://www.
下面是一个如何使用pdfbox抽取pdf文件的例子:
Java代码
import org.pdfbox.pdmodel.PDdocument.
import org.pdfbox.pdfparser.PDFParser;
import *;
import org.pdfbox.util.PDFTextStripper;
import java.util.Date;
/**
* <p>Title: pdf extraction</p>
* <p>Description: email:chris@
* <p>Copyright: Matrix Copyright (c) 2003</p>
* <p>Company:
* @author chris
* @version 1.0,who use this example pls remain the declare
*/
public class PdfExtracter{
public PdfExtracter(){
}
public String GetTextFromPdf(String filename) throws Exception
{
String temp=null;
PDdocument.nbsppdfdocument.null;
FileInputStream is=new FileInputStream(filename);
PDFParser parser = new PDFParser( is );
parser.parse();
pdfdocument.nbsp= parser.getPDdocument.);
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter( out );
PDFTextStripper stripper = new PDFTextStripper();
stripper.writeText(pdfdocument.getdocument.), writer );
writer.close();
byte[] contents = out.toByteArray();
String ts=new String(contents);
System.out.println("the string length is"+contents.length+"\n");
return ts;
}
public static void main(String args[])
{
PdfExtracter pf=new PdfExtracter();
PDdocument.nbsppdfdocument.nbsp= null;
try{
String ts=pf.GetTextFromPdf("c:\\a.pdf");
System.out.println(ts);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
import org.pdfbox.pdmodel.PDdocument.
import org.pdfbox.pdfparser.PDFParser;
import *;
import org.pdfbox.util.PDFTextStripper;
import java.util.Date;
/**
* <p>Title: pdf extraction</p>
* <p>Description: email:chris@
* <p>Copyright: Matrix Copyright (c) 2003</p>
* <p>Company:
* @author chris
* @version 1.0,who use this example pls remain the declare
*/
public class PdfExtracter{
public PdfExtracter(){
}
public String GetTextFromPdf(String filename) throws Exception
{
String temp=null;
PDdocument.nbsppdfdocument.null;
FileInputStream is=new FileInputStream(filename);
PDFParser parser = new PDFParser( is );
parser.parse();
pdfdocument.nbsp= parser.getPDdocument.);
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter( out );
PDFTextStripper stripper = new PDFTextStripper();
stripper.writeText(pdfdocument.getdocument.), writer );
writer.close();
byte[] contents = out.toByteArray();