java操作word (jacob)
生活随笔
收集整理的這篇文章主要介紹了
java操作word (jacob)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
什么是jacob?
jacob是一個(gè)開源項(xiàng)目它允許在java中調(diào)用com接口自動(dòng)組件,它使用JNI(本地調(diào)用程序)來進(jìn)行本地調(diào)用COM庫。它可運(yùn)行在x86和支持32位和64位Java虛擬機(jī)?X64的環(huán)境(摘除自百度詞條)。
準(zhǔn)備工作:
需要的jar包:jacob.jar;此外還需要將jacob.dll放在在windows/system32目錄下,否則會(huì)報(bào)錯(cuò)Could not initialize class com.jacob.activeX.ActiveXComponent。
實(shí)例
需要導(dǎo)入的包
import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Variant;一、word轉(zhuǎn)pdf
public static void wordToPdf(String source,String toFile) {//該方法只可以操作2003版的word(據(jù)說jacob只能這樣,沒親自試過)//txt格式的文件也可以用該方法轉(zhuǎn)換成pdf(親測(cè)可以成功,ps:偶然發(fā)現(xiàn)的)
String filename = source;String toFilename = toFile;// System.out.println("啟動(dòng)Word");long start = System.currentTimeMillis();ActiveXComponent app = null;try {//啟動(dòng)word應(yīng)用程序(Microsoft Office Word 2003)app = new ActiveXComponent("Word.Application"); 設(shè)置word應(yīng)用程序不可見 app.setProperty("Visible", false);// documents表示word程序的所有文檔窗口,(word是多文檔應(yīng)用程序)Dispatch docs = app.getProperty("Documents").toDispatch();//System.out.println("打開文檔" + filename);//打開要轉(zhuǎn)換的word文件 ,作為pdf格式保存到臨時(shí)文件Dispatch doc = Dispatch.call(docs,// "Open", // filename,// FileNamefalse,// ConfirmConversionstrue // ReadOnly ).toDispatch();//System.out.println("轉(zhuǎn)換文檔到PDF: " + toFilename);File tofile = new File(toFilename);if (tofile.exists()) {tofile.delete();}Dispatch.call(doc,// "SaveAs", // toFilename, // FileName wdFormatPDF);// 關(guān)閉word文件 Dispatch.call(doc, "Close", false);long end = System.currentTimeMillis();System.out.println("轉(zhuǎn)換完成..用時(shí):" + (end - start) + "ms.");} catch (Exception e) {System.out.println("========Error:文檔轉(zhuǎn)換失敗:" + e.getMessage());} finally {if (app != null)//關(guān)閉word應(yīng)用程序 app.invoke("Quit", wdDoNotSaveChanges);}}
二、word轉(zhuǎn)txt
public static void word2Txt(String inputFIle, String outputFile){boolean flag = false;// 打開Word應(yīng)用程序ActiveXComponent app = new ActiveXComponent("Word.Application");try {// 設(shè)置word不可見app.setProperty("Visible", new Variant(false));// 打開word文件Dispatch doc1 = app.getProperty("Documents").toDispatch();Dispatch doc2 = Dispatch.invoke( doc1, "Open", Dispatch.Method,new Object[] { inputFIle, new Variant(false), new Variant(true) }, new int[1]).toDispatch();// 作為txt格式保存到臨時(shí)文件Dispatch.invoke(doc2, "SaveAs", Dispatch.Method, new Object[] {outputFile, new Variant(7) }, new int[1]);// 關(guān)閉wordVariant f = new Variant(false);Dispatch.call(doc2, "Close", f);flag = true;} catch (Exception e) {e.printStackTrace();} finally {app.invoke("Quit", new Variant[] {});}}三、讀取word內(nèi)容(先將word轉(zhuǎn)換成txt,再讀取txt文件)
public static String readWord(String path) {String pathTo = path.replace("doc","txt");word2Txt(path,pathTo);StringBuffer stringBuffer = new StringBuffer();File file = new File(pathTo);try {FileInputStream fis = new FileInputStream(file);byte[] buffer = new byte[1000];int len;try {while(-1 != (len=fis.read(buffer))){String temp = new String(buffer,0,len,"gb2312");stringBuffer.append(temp);}// System.out.println(stringBuffer.toString()); } catch (IOException e) {// TODO Auto-generated catch block e.printStackTrace();}} catch (FileNotFoundException e) {// TODO Auto-generated catch block e.printStackTrace();}if(file.isFile())file.delete();return stringBuffer.toString();}?
轉(zhuǎn)載于:https://www.cnblogs.com/vitosun/p/4005332.html
總結(jié)
以上是生活随笔為你收集整理的java操作word (jacob)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CSS position属性absolu
- 下一篇: codeblocks如何watch指针