首页 > 代码库 > java操作word (jacob)

java操作word (jacob)

什么是jacob?

jacob是一个开源项目它允许在java中调用com接口自动组件,它使用JNI(本地调用程序)来进行本地调用COM库。它可运行在x86和支持32位和64位Java虚拟机 X64的环境(摘除自百度词条)。

准备工作:

需要的jar包:jacob.jar;此外还需要将jacob.dll放在在windows/system32目录下,否则会报错Could not initialize class com.jacob.activeX.ActiveXComponent。

实例

需要导入的包

import com.jacob.activeX.ActiveXComponent;import com.jacob.com.Dispatch;import com.jacob.com.Variant;

一、word转pdf

public static void wordToPdf(String source,String toFile)     {     //该方法只可以操作2003版的word(据说jacob只能这样,没亲自试过)
     //txt格式的文件也可以用该方法转换成pdf(亲测可以成功,ps:偶然发现的)
String filename
= source; String toFilename = toFile; // System.out.println("启动Word"); long start = System.currentTimeMillis(); ActiveXComponent app = null; try { //启动word应用程序(Microsoft Office Word 2003) app = new ActiveXComponent("Word.Application"); //// 设置word应用程序不可见 app.setProperty("Visible", false); // documents表示word程序的所有文档窗口,(word是多文档应用程序) Dispatch docs = app.getProperty("Documents").toDispatch(); //System.out.println("打开文档" + filename); //打开要转换的word文件 ,作为pdf格式保存到临时文件 Dispatch doc = Dispatch.call(docs,// "Open", // filename,// FileName false,// ConfirmConversions true // ReadOnly ).toDispatch(); //System.out.println("转换文档到PDF: " + toFilename); File tofile = new File(toFilename); if (tofile.exists()) { tofile.delete(); } Dispatch.call(doc,// "SaveAs", // toFilename, // FileName wdFormatPDF); // 关闭word文件 Dispatch.call(doc, "Close", false); long end = System.currentTimeMillis(); System.out.println("转换完成..用时:" + (end - start) + "ms."); } catch (Exception e) { System.out.println("========Error:文档转换失败:" + e.getMessage()); } finally { if (app != null) //关闭word应用程序 app.invoke("Quit", wdDoNotSaveChanges); } }

二、word转txt

public static void word2Txt(String inputFIle, String outputFile)       {           boolean flag = false;// 打开Word应用程序           ActiveXComponent app = new ActiveXComponent("Word.Application");          try {             // 设置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格式保存到临时文件         Dispatch.invoke(doc2, "SaveAs", Dispatch.Method, new Object[] {               outputFile, new Variant(7) }, new int[1]);         // 关闭word         Variant f = new Variant(false);         Dispatch.call(doc2, "Close", f);         flag = true;      } catch (Exception e) {         e.printStackTrace();      } finally {         app.invoke("Quit", new Variant[] {});      }   }   

三、读取word内容(先将word转换成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();    }

 

java操作word (jacob)