使用jacob调用Windows的com对象,转换Office文件为pdf、html等_html/css_WEB-ITnose
1、介绍
2、安装和配置
Jacob是一个开源软件,它的官方站点是: http://danadler.com/jacob/ 大家可以到上面下载源代码研究,也可以直接下载编译后的二进制文件。
下载包jacob_x.x.zip,解压后有几个文件:jacob.jar、jacob-x.x-M2-x86.dll
把jacob-x.x-M2-x86.dll拷贝到%JAVA_HOME% 下的 bin 目录下,其中,%JAVA_HOME%就是JDK的安装目录。接着直接在java IDE中引用jacob.jar就可以使用了。
3、转换word为pdf、html、txt 的示例
package com.shanhy.demo.windowsoffice;import java.io.File;import com.jacob.activeX.ActiveXComponent;import com.jacob.com.ComThread;import com.jacob.com.Dispatch;import com.jacob.com.Variant;/** * * 将jacob.dll放入JDK的bin目录下 * 把jacob.jar放入项目的buildPath中(web项目放到WEB-INF\lib目录下) * * @author 单红宇 * */public class ConvertToPdf { // WORD 转换文档格式参数值:17为pdf,8为html,2为txt(支持的格式不限与此,其他格式暂为列出) static final int wdFormatPDF = 17;// PDF 格式 static final int wdFormatHTML = 8;// HTML 格式 static final int wdFormatTXT = 2;// TXT 格式 /** * Word文档转换 * * @param fromfileName * @param toFileName * @author SHANHY */ public void wordConvert(String fromfileName, String toFileName) { System.out.println("启动Word..."); ComThread.InitSTA(); long start = System.currentTimeMillis(); ActiveXComponent app = null; Dispatch doc = null; try { app = new ActiveXComponent("Word.Application");//创建一个word对象 app.setProperty("Visible", new Variant(false)); //不可见打开word app.setProperty("AutomationSecurity", new Variant(3)); //禁用宏 Dispatch docs = app.getProperty("Documents").toDispatch();//获取文挡属性 System.out.println("打开文档 >>> " + fromfileName); //Object[]第三个参数是表示“是否只读方式打开” doc = Dispatch.invoke(docs, "Open", Dispatch.Method, new Object[] { fromfileName, new Variant(false), new Variant(true) }, new int[1]).toDispatch(); File tofile = new File(toFileName); if (tofile.exists()) { tofile.delete(); } int formatValue = -1; if(toFileName.toLowerCase().endsWith(".pdf")){ formatValue = wdFormatPDF; }else if(toFileName.toLowerCase().endsWith(".html")){ formatValue = wdFormatHTML; }else if(toFileName.toLowerCase().endsWith(".txt")){ formatValue = wdFormatTXT; }else{ formatValue = -1; } if(formatValue != -1){ System.out.println("转换文档 ["+fromfileName+"] >>> ["+toFileName+"]"); Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { toFileName, new Variant(formatValue) }, new int[1]); }else{ System.out.println("转换文件到目标文档不被支持!["+fromfileName+"] >>> ["+toFileName+"]"); } long end = System.currentTimeMillis(); System.out.println("用时:" + (end - start) + "ms."); } catch (Exception e) { e.printStackTrace(); System.out.println("========Error:文档转换失败:" + e.getMessage()); } finally { Dispatch.call(doc, "Close", false); System.out.println("关闭文档"); if (app != null) app.invoke("Quit", new Variant[] {}); } // 如果没有这句话,winword.exe进程将不会关闭 ComThread.Release(); ComThread.quitMainSTA(); } /** * PPT(PowerPoint)文档转换 * * @param fromfileName * @param toFileName * @author SHANHY */ public void pptConvert(String fromfileName, String toFileName) { System.out.println("启动PPT..."); ComThread.InitSTA(); long start = System.currentTimeMillis(); ActiveXComponent app = null; Dispatch doc = null; try { app = new ActiveXComponent("Word.Application");//创建一个word对象 app.setProperty("Visible", new Variant(false)); //不可见打开word app.setProperty("AutomationSecurity", new Variant(3)); //禁用宏 Dispatch docs = app.getProperty("Documents").toDispatch();//获取文挡属性 System.out.println("打开文档 >>> " + fromfileName); //Object[]第三个参数是表示“是否只读方式打开” doc = Dispatch.invoke(docs, "Open", Dispatch.Method, new Object[] { fromfileName, new Variant(false), new Variant(true) }, new int[1]).toDispatch(); File tofile = new File(toFileName); if (tofile.exists()) { tofile.delete(); } int formatValue = -1; if(toFileName.toLowerCase().endsWith(".pdf")){ formatValue = wdFormatPDF; }else if(toFileName.toLowerCase().endsWith(".html")){ formatValue = wdFormatHTML; }else if(toFileName.toLowerCase().endsWith(".txt")){ formatValue = wdFormatTXT; }else{ formatValue = -1; } if(formatValue != -1){ System.out.println("转换文档 ["+fromfileName+"] >>> ["+toFileName+"]"); Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { toFileName, new Variant(formatValue) }, new int[1]); }else{ System.out.println("转换文件到目标文档不被支持!["+fromfileName+"] >>> ["+toFileName+"]"); } long end = System.currentTimeMillis(); System.out.println("用时:" + (end - start) + "ms."); } catch (Exception e) { e.printStackTrace(); System.out.println("========Error:文档转换失败:" + e.getMessage()); } finally { Dispatch.call(doc, "Close", false); System.out.println("关闭文档"); if (app != null) app.invoke("Quit", new Variant[] {}); } // 如果没有这句话,winword.exe进程将不会关闭 ComThread.Release(); ComThread.quitMainSTA(); } public static void main(String[] args) { ConvertToPdf d = new ConvertToPdf(); d.wordConvert("g:\\test.docx", "g:\\test.pdf"); }}
读、写Word的简单示例
import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Variant; import com.jacob.com.Dispatch; public class Word { String strDir = "c:jacob_1.9"; String strInputDoc = strDir + "file_in.doc"; String strOutputDoc = strDir + "file_out.doc"; String strOldText = "[label:import:1]"; String strNewText = "I am some horribly long sentence, so long that [insert anything]"; boolean isVisible = true; boolean isSaveOnExit = true; public Word() { ActiveXComponent oWord = new ActiveXComponent("Word.Application"); oWord.setProperty("Visible", new Variant(isVisible)); Dispatch oDocuments = oWord.getProperty("Documents").toDispatch(); Dispatch oDocument = Dispatch.call(oDocuments, "Open", strInputDoc). toDispatch(); Dispatch oSelection = oWord.getProperty("Selection").toDispatch(); Dispatch oFind = oWord.call(oSelection, "Find").toDispatch(); Dispatch.put(oFind, "Text", strOldText); Dispatch.call(oFind, "Execute"); Dispatch.put(oSelection, "Text", strNewText); Dispatch.call(oSelection, "MoveDown"); Dispatch.put(oSelection, "Text", "nSo we got the next line including BR.n"); Dispatch oFont = Dispatch.get(oSelection, "Font").toDispatch(); Dispatch.put(oFont, "Bold", "1"); Dispatch.put(oFont, "Italic", "1"); Dispatch.put(oFont, "Underline", "0"); Dispatch oAlign = Dispatch.get(oSelection, "ParagraphFormat"). toDispatch(); Dispatch.put(oAlign, "Alignment", "3"); Dispatch oWordBasic = (Dispatch) Dispatch.call(oWord, "WordBasic"). getDispatch(); Dispatch.call(oWordBasic, "FileSaveAs", strOutputDoc); Dispatch.call(oDocument, "Close", new Variant(isSaveOnExit)); oWord.invoke("Quit", new Variant[0]); } public static void main(String[] args) { Word word = new Word(); } }
4、jacob.jar的结构
jacob包括两个部分:
com.jacob.activeX: ActiveXComponent类
com.jacob.com: 其它类和元素
5、Jacob类
Jacob的结构很简单,包含以下几个类:
ActiveXComponent Class:封装了Dispatch对象,用于创建一个封装了COM组件对象的Java Object
Dispatch Class:用于指向封装后的MS数据结构。常用的方法有call,subcall,get,invoke…后面会介绍使用方法。
Variant Class:用于映射COM的Variant数据类型。提供Java和COM的数据交换。
ComException Class:异常类
6、Jacob方法
用于访问COM/DLL对象的方法,读取、修改COM/DLL对象的属性。
call method:属于Dispatch类。用于访问COM/DLL对象的方法。方法进行了重载,方便不同场合调用。返回一个Variant类型的值。
callSub method:使用方法和call一样,不过它不返回值。
get method:读取COM对象的属性值,返回一个Variant类型值。
put method:设置COM对象的属性值。
invoke method:call的另一种用法,更复杂一些。
invokesub method:subcall的另一种用法
getProperty method:属于ActiveXComponent类,读取属性值,返回一个Variant类型值。
setProperty method:属于ActiveXComponent类,设置属性值。
要注意一点:在使用Jacob时,很重要的一点是,用户必须安装有Office的应用程序。否则也就无法建立Java-COM桥,进而无法解析了。
部分内容参考: http://www.cnblogs.com/vulcans/archive/2009/09/08/1562588.html

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











이 기사는 HTML & lt; Progress & Gt에 대해 설명합니다. 요소, 그 목적, 스타일 및 & lt; meter & gt의 차이; 요소. 주요 초점은 & lt; progress & gt; 작업 완료 및 & lt; meter & gt; Stati의 경우

이 기사는 HTML & LT; Datalist & GT에 대해 논의합니다. 자동 완성 제안을 제공하고, 사용자 경험을 향상시키고, 오류를 줄임으로써 양식을 향상시키는 요소. 문자 수 : 159

이 기사는 HTML & lt; meter & gt에 대해 설명합니다. 범위 내에 스칼라 또는 분수 값을 표시하는 데 사용되는 요소 및 웹 개발의 일반적인 응용 프로그램. & lt; meter & gt; & lt; Progress & Gt; 그리고 Ex

기사는 HTML5 크로스 브라우저 호환성을 보장하기위한 모범 사례에 대해 논의하고 기능 감지, 점진적 향상 및 테스트 방법에 중점을 둡니다.

이 기사는 html5 & lt; time & gt; 시맨틱 날짜/시간 표현 요소. 인간이 읽을 수있는 텍스트와 함께 기계 가독성 (ISO 8601 형식)에 대한 DateTime 속성의 중요성을 강조하여 Accessibilit를 향상시킵니다.

이 기사에서는 브라우저에서 직접 사용자 입력을 검증하기 위해 필요한, Pattern, Min, Max 및 Length 한계와 같은 HTML5 양식 검증 속성을 사용하는 것에 대해 설명합니다.

이 기사는 모바일 장치의 반응 형 웹 디자인에 필수적인 Viewport Meta Tag에 대해 설명합니다. 적절한 사용이 최적의 컨텐츠 스케일링 및 사용자 상호 작용을 보장하는 방법을 설명하는 반면, 오용은 설계 및 접근성 문제로 이어질 수 있습니다.

이 기사는 & lt; iframe & gt; 외부 컨텐츠를 웹 페이지, 공통 용도, 보안 위험 및 객체 태그 및 API와 같은 대안을 포함시키는 태그의 목적.
