转载的,先保存起来以后研究
Java调用ocx控件以及dll和word文档
最近因为项目需要,特地研究了两天jacob操作office的一个java插件,因时间缘故,只研究了操作word,特此共享:
本人喜欢通过代码学习东西,所以,直接讲操作的代码拷贝出来,供大家分享。
public static void main(String args[]) {
ComThread.InitSTA();// 初始化com的线程,非常重要!!使用结束后要调用 realease方法
/**
完成初始化工作
*/
ActiveXComponent objWord = new ActiveXComponent("Word.Application");// Instantiate objWord and Declare word object
Dispatch wordObject = (Dispatch) objWord.getObject();// Assign a local word object
Dispatch.put((Dispatch) wordObject, "Visible", new Variant(true));// Variant(true)表示word应用程序可见
Dispatch documents = objWord.getProperty("Documents").toDispatch(); // documents表示word的所有文档窗口,(word是多文档应用程序)
Dispatch document = Dispatch.call(documents, "Add").toDispatch(); // 使用Add命令创建一个新文档,用Open命令可以打开一个现有文档
/**
*开始写word的工作,包括标题,正文(段落一、段落二)
*/
Dispatch wordContent = Dispatch.get(document, "Content").toDispatch(); // 取得word文件的内容
Dispatch selection = Dispatch.get(wordObject, "Selection").toDispatch();
Dispatch paragraphFormat=Dispatch.get(selection,"ParagraphFormat").getDispatch();
Dispatch font = Dispatch.get(selection, "Font").toDispatch(); // 字型格式化需要的对象
Dispatch.put(paragraphFormat, "Alignment", "1"); // 设置标题的对齐方式(1:置中 2:靠右 3:靠左)
Dispatch.put(font, "Bold", "1"); // 字型租体
Dispatch.put(font, "Color", "1,0,0,0"); // 字型颜色(1,0,0,0=>红色 1,1,0,0=>棕色)
Dispatch.put(font, "Italic", "1"); //字型斜体
Dispatch.call(selection, "TypeText", "标题"); // 写入标题内容
Dispatch.call(selection, "TypeParagraph"); // 空一行段落
Dispatch.put(paragraphFormat, "Alignment", "3"); // 设置正文的对齐方式(1:置中 2:靠右 3:靠左)
Dispatch.put(selection, "Text", " 段落one");
Dispatch.call(selection,"MoveRight");
Dispatch.call(selection, "TypeParagraph"); // 空一行段落
Dispatch.put(selection, "Text", " 段落two");
Dispatch.call(selection,"MoveRight");
/**
* 写一个新的table
*/
Dispatch tables = Dispatch.get(wordContent, "Tables").toDispatch();
Dispatch tablerange = Dispatch.get(selection, "Range").toDispatch();
Dispatch newTable = Dispatch.call(tables, "Add", tablerange,
new Variant(2), new Variant(3)).toDispatch();
/**
* 往表格中填写内容。
*/
Dispatch newtables = Dispatch.get(wordContent, "Tables").toDispatch();
Dispatch table = Dispatch.call(newtables, "Item", new Variant(1)).toDispatch(); // 要填充的表格
Dispatch cols = Dispatch.get(table, "Columns").toDispatch(); // 表格的所有行
Dispatch rows = Dispatch.get(table, "Rows").toDispatch(); // 表格的所有列
int colsCount=Dispatch.get(cols, "Count").toInt();
int rowsCount=Dispatch.get(rows,"Count").toInt();
Dispatch col = Dispatch.get(cols, "First").toDispatch();
for(int i=1;i<=rowsCount;i++)
{
for(int m=1;m<=colsCount;m++)
{
Dispatch cell=Dispatch.call(table,"Cell",new Variant(i),new Variant(m)).getDispatch();
Dispatch.call(cell, "Select");
Dispatch.put(selection, "Text",i*m );
Dispatch.call(selection,"MoveRight");
}
}
Dispatch.call(selection, "EndKey",6);
/**
* 开始读word的工作
*/
Dispatch paragraphs = Dispatch.get(wordContent, "Paragraphs").toDispatch(); // 所有段落
int paragraphCount = Dispatch.get(paragraphs, "Count").toInt(); // 一共的段落数
for(int i =1;i<=paragraphCount;i++)
{
Dispatch paragraph = Dispatch.call(paragraphs, "Item", new Variant(i)).toDispatch();
Dispatch range = Dispatch.call(paragraph,"Range").toDispatch();
String ptext = Dispatch.get(range, "text").getString();
System.out.println(ptext);
}
/**
*加入图片
*/
String picturePath="E:\\照片\\苏歆然\\2009-01-08-00.bmp";//图片的路径
Dispatch.call(Dispatch.get(selection, "InLineShapes").toDispatch(),
"AddPicture", picturePath);
/**
* 关闭文档
*/
Dispatch.call(document, "SaveAs", new Variant("d://abc1.doc")); // 保存一个新文档
ComThread.Release();//释放com线程。根据jacob的帮助文档,com的线程回收不由java的垃圾回收器处理
}
生成的word 的格式如下:
补充:jacob操作powerpoint
ActiveXComponent ppt = new ActiveXComponent("PowerPoint.Application");
Dispatch pptObject=ppt.getObject();
Dispatch.put((Dispatch) pptObject, "Visible", new Variant(true));
// 设置程序界面是否可见
ActiveXComponent presentations = ppt.getPropertyAsComponent("Presentations");
// 生成一个新的ppt 对象
ActiveXComponent presentation =presentations.invokeGetComponent("Add", new Variant(1));
Dispatch windows = presentation.getProperty("Windows").toDispatch();
Dispatch window = Dispatch.call(windows, "Item", new Variant(1)).toDispatch();
Dispatch selection = Dispatch.get(window, "Selection").toDispatch();
ActiveXComponent slides = presentation.getPropertyAsComponent("Slides");
//添加第一张幻灯片;
slides.invoke("Add", new Variant(1), new Variant(1));
Dispatch slideRange=Dispatch.get(selection, "SlideRange").getDispatch();
Dispatch shapes=Dispatch.get(slideRange, "Shapes").getDispatch();
Dispatch shape1 = Dispatch.call(shapes, "Item", new Variant(2)).toDispatch();
Dispatch.call(shape1, "Select");
Dispatch shapeRange=Dispatch.get(selection, "ShapeRange").getDispatch();
Dispatch textFrame=Dispatch.get(shapeRange, "TextFrame").getDispatch();
Dispatch textRange=Dispatch.get(textFrame, "TextRange").getDispatch();
Dispatch.call(textRange, "Select");
Dispatch.put(textRange,"Text","测试");
//添加第二张幻灯片;
slides.invoke("Add", new Variant(2), new Variant(1));
// powerpoint幻灯展示设置对象
ActiveXComponent setting = presentation.getPropertyAsComponent("SlideShowSettings");
setting.invoke("Run");
//保存ppt
presentation.invoke("SaveAs", new Variant(PPT_FILE));
// 释放控制线程
ComThread.Release();
二:Java调用ocx控件以及dll
通过Java调用OCX控件有几种方法,JNI、JACOB、Jawin等
1.JNI 最直接的方式,也是最麻烦的方式,需要自己完成所有的工作,不推荐。
2.Jawin 尝试了一下,效果不错,但相对来说,其编程风格更贴近Windows,离Java有点远 。
3.Jacob 使用Jacob非常方便,Java编程风格,需要了解的知识比较少。
Jacob的使用方法:
首先--
1.在工程中导入 jacob.jar 这个包。
2.把 jacob.dll 拷贝到 jdk\bin或者C:\WINDOWS\system32目录下。
以上的两个文件您可以到网上http://danadler.com/jacob/ 下载的1.7的版本
jacob.jar 与 jacob.dll 版本一致。
3.注册ocx控件
l 先把***.ocx控件放入c:\windows\system32 文件夹下
l 在cmd命令行下,输入regsvr32 ***.ocx命令进行注册
Java代码:
1.初始化
ActiveXComponent com = new ActiveXComponent("组件的ProgID") ;
Dispatch disp = com.getObject();
2.调用控件里面的方面
2.1调用无参的方法,并返回一个short值
Dispatch.call(disp, "Init").getShort();
2.2调用有一个参数的方法,并返回一个boolean值
Dispatch.call(disp,"Method",new Variant(args)).getBoolean();
调用多个参数依次类推,注意在传递参数前,将Java中的参数转换成Variant。
bug解决:
1.java.lang.UnsatisfiedLinkError: no jacob in java.library.path
原因是:没有把 jacob.dll 拷贝到 jdk\bin或者C:\WINDOWS\system32目录下
2.Exception in thread "main" com.jacob.com.ComFailException: Can't get object clsid from progid
原因是:regsvr32 ***.ocx命令进行注册后 ,获取的progid有误;
3.
Exception in thread "main" com.jacob.com.ComFailException: A COM exception has been encountered:
At Invoke of: LoadFile
Description: 灾难性故障
at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.com.Dispatch.invokev(Dispatch.java:858)
at com.jacob.com.Dispatch.callN(Dispatch.java:455)
at com.jacob.com.Dispatch.call(Dispatch.java:544)
at com.lv.office.AIPToTest.jacob(AIPToTest.java:24)
at com.lv.office.AIPToTest.main(AIPToTest.java:14)
原因是:
在OCX控件中,重写COleControl.IsInvokeAllowed (DISPID)方法,即在控件的CMyNameCtrl中增加一个方法
BOOL CMyNameCtrl::IsInvokeAllowed (DISPID)
{
return TRUE;
}
用这个方法的理由是:
这个错误是由ActiveX结构设计造成的。
在Ole4.0版本之前,外部程序是可以直接调用OCX中方法的。Ole4.0之后,每次调用控件中的方法,系统会自动检查是否允许调用,即运行COleControl.IsInvokeAllowed (DISPID)
该方法检查控件是否正确的初始化或者是否通过持久存储接口正确加载,如果两个条件有一个满足,即返回TRUE,否则返回FALSE。
当控件在MFC中使用时,很多细节,如初始化,都被过滤了,这样,大多数用户都不会遇到这个问题。但是,当我们从C、C++的dll中调用控件时,不满足上述条件,该方法返回FALSE,这时候再调用任何控件方法,都会出现上述异常。
分享到:
相关推荐
java调用ocx控件,jacob,jar和dll文件
java调用dll和ocx的开源控件,为当前最好用的控件,dll和ocx可是VB或VC开发的控件。
dll文件封装成OCX控件,当然其中也有对dll的调用,其实是c++制作OCX的文档
U800ocx.html 调用ocx控件的html文件 U800ocx - firefox.html 支持firefox的调用ocx控件的html文件 TypeLibraryExplorer.rar ocx文件查看器 FFOCX.zip 让FireFox支持OCX插件的安装包
海康威视组态王OCX控件支持最新版本的海康录像机,具备登录、退出、播放、停止播放,抓BMP图片和存储路径设置等功能。适用于WIN7,WIN10目前没有测试通过。可以作为组态王的OCX控件。NETVIDEO控件包:...
chrome调用ocx完美解决方案,很详细。
swing调用OCX或dll的方法有很多,其中包括:jcob、comfyj、jawin、jni、jna等等很多方式,但是如果要显示dll或ocx的控件在swing界面上,就有点麻烦了,就像ocx在IE中显示的对象一样。最后通过SWT中嵌入OCX,然后把...
自动实现自动实现DLL(OCX)控件注册,通过几种方法实现控件的系统注册。
这是使用VB 设计的注册ActiveX控件软件, 包含VB 调用系统命令的使用, 并使用了VB获取系统路径, 调用API函数, 复制文件等高级功能。 本资源还包含了一些VB常用的ActiveX控件, 如Flash8.ocx, comctl32.ocx, mci32.ocx,...
VB实现对DLL/OCX控件的提取功能,分析出OCXs, DLLs, TLBs文件的结构内容复制到一个指定的文件夹中,方便您制作安装程序时用。 同时它还有一些设置选项:比如改变支撑文件名、批量自动重命名、分析完毕自动打开支撑...
通过调用第三方库(lib、dll、.h)文件,封装为ocx控件,该工程主要实现了视频监控中得多屏显示
高拍仪控件集成,即各类应用软件系统通过集成高拍仪控件,解决在java、.net、php等软件开发中遇到的高拍仪编程操作问题,快速在asp.net、jsp、asp等web项目中调用高拍仪设备,使得用户办公流程更便捷、顺畅。...
如同书名一样,是用来介绍ACTIVEX控件动态调用DLL函数的方法,希望对大家有所帮助
将DLL做成RES编译进工程,运行时释放资料保存到系统TEMP目录下调用,WIN32模式下调用一切正常,用Delphi自带的WebBrowser调用也正常,但在IE下调用DLL提示无法加载,望高手解决,谢谢。
vs2008 环境下C++代码调用vb6.0生成的Activex dll。
通过bat脚本修改注册表,可以一键完成:添加浏览器受信任站点,设置浏览器兼容性视图,修改浏览器中安全站点自定义级别中关于ActiveX的选项为启用,关闭IE浏览器数据执行保护及其他安全设置,自动注册ocx及dll控件
我学习ocx自己写的代码,包含了ocx调用dll控件方法,ocx利用ie调试的示例脚本.htm,ocx中使用vector实现电子表格的方法,.cab压缩文件生成脚本.inf,注册和反注册脚本;MakeCAB工具集
不用OLE View工具,也可以获得com组件(ocx、dll)的ClassID、... 本资源的源码中还含有一个特别重要的模块,用VB动态调用标准动态库(dll),这是很多VB编程者都希望得到的。 本资源含所有源码。(QQ:984742494)