您的位置:首页 > 编程语言 > Java开发

Java调用ocx控件以及dll和word文档

2014-05-05 15:31 627 查看
http://hpfgc.blog.163.com/blog/static/14799557201011811486481/

最近因为项目需要,特地研究了两天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,这时候再调用任何控件方法,都会出现上述异常。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: