1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > 使用jacob调用Windows的com对象 转换Office文件为pdf html等【HTML】

使用jacob调用Windows的com对象 转换Office文件为pdf html等【HTML】

时间:2019-02-18 01:18:33

相关推荐

使用jacob调用Windows的com对象 转换Office文件为pdf html等【HTML】

web前端|html教程

使用jacob调用Windows的com对象,转换Office文件为pdf、html等

web前端-html教程

1、介绍

健康问答网源码,vscode php高亮,怎样制作u盘启动并ubuntu,如何显示tomcat,sqlite如何添加公式,数据采集插件,外包项目前端框架学习,爬虫以及tornado课程介绍,php 判断是否存在文件,seo公司架构,网站素材按钮,百度网页特效,阿里大于短信模板变量lzw

最新修复优化版币圈30秒源码,ubuntu修改显示语言,文书网 爬虫软件,php 读取pdf,云南seo基础lzw

Jacob 是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁。使用Jacob自带的DLL动态链接库,并通过JNI的方式实现了在Java平台上对COM程序的调用。至于什么是COM组件,大家自己Google吧。

问道辅助源码,vscode怎样使用浏览器,ubuntu git环境,手机app访问tomcat,爬虫分类算法,php登录界面设计,揭阳seo网络营销更优惠lzw

2、安装和配置

Jacob是一个开源软件,它的官方站点是: /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 .ComThread;import .Dispatch;import .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)); //不可见打开wordapp.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)); //不可见打开wordapp.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 .Variant; import .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类

: 其它类和元素

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桥,进而无法解析了。

部分内容参考: /vulcans/archive//09/08/1562588.html

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。