標籤:excel imp ace 不能 files sys apache 中文支援 lag
轉自:https://www.ibm.com/developerworks/cn/java/l-java-tips/ 感謝作者發布的文章 用 jacob
其實 jacob 是一個 bridage,串連 java 和 com 或者 win32 函數的一個中介軟體,jacob 並不能直接抽取 word,excel 等檔案,需要自己寫 dll 哦,不過已經有為你寫好的了,就是 jacob 的作者一併提供了。
jacob jar 與 dll 檔案下載: http://www.matrix.org.cn/down_view.asp?id=13
下載了 jacob 並放到指定的路徑之後 (dll 放到 path,jar 檔案放到 classpath),就可以寫你自己的抽取程式了,下面是一個簡單的例子:
import java.io.File; import com.jacob.com.*; import com.jacob.activeX.*; /** * Title: pdf extraction * Description: email:[email protected] * Copyright: Matrix Copyright (c) 2003 * Company: Matrix.org.cn * @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[] {}); } }}
用 apache 的 poi 來抽取 word,excel。
poi 是 apache 的一個項目,不過就算用 poi 你可能都覺得很煩,不過不要緊,這裡提供了更加簡單的一個介面給你:
下載經過封裝後的 poi 包: http://www.matrix.org.cn/down_view.asp?id=14
下載之後,放到你的 classpath 就可以了,下面是如何使用它的一個例子:
import java.io.*;import org.textmining.text.extraction.WordExtractor;/*** <p>Title: word extraction</p>* <p>Description: email:[email protected]</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);}}
pdfbox- 用來抽取 pdf 檔案
但是 pdfbox 對中文支援還不好,先下載 pdfbox: http://www.matrix.org.cn/down_view.asp?id=12
下面是一個如何使用 pdfbox 抽取 pdf 檔案的例子:
import org.pdfbox.pdmodel.PDdocument .import org.pdfbox.pdfparser.PDFParser;import java.io.*;import org.pdfbox.util.PDFTextStripper;import java.util.Date;/*** <p>Title: pdf extraction</p>* <p>Description: email:[email protected]</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 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(); }}}
抽取支援中文的 pdf 檔案-xpdf
xpdf 是一個開源項目,我們可以調用他的本地方法來實現抽取中文 pdf 檔案。
下載 xpdf 函數包: http://www.matrix.org.cn/down_view.asp?id=15
同時需要下載支援中文的補丁包: http://www.matrix.org.cn/down_view.asp?id=16
按照 readme 放好中文的 patch,就可以開始寫調用本地方法的 java 程式了
下面是一個如何調用的例子:
import java.io.*;/*** <p>Title: pdf extraction</p>* <p>Description: email:[email protected]</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 PdfWin { public PdfWin() { } public static void main(String args[]) throws Exception { String PATH_TO_XPDF="C:\\Program Files\\xpdf\\pdftotext.exe"; String filename="c:\\a.pdf"; String[] cmd = new String[] { PATH_TO_XPDF, "-enc", "UTF-8", "-q", filename, "-"}; Process p = Runtime.getRuntime().exec(cmd); BufferedInputStream bis = new BufferedInputStream(p.getInputStream()); InputStreamReader reader = new InputStreamReader(bis, "UTF-8"); StringWriter out = new StringWriter(); char [] buf = new char[10000]; int len; while((len = reader.read(buf))>= 0) { //out.write(buf, 0, len); System.out.println("the length is"+len); } reader.close(); String ts=new String(buf); System.out.println("the str is"+ts); }}
java 抽取 word,pdf 的四種武器