`
dingqijie
  • 浏览: 101400 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论
阅读更多
   最近因为项目需要,特地研究了两天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();

  • 大小: 19.4 KB
分享到:
评论
18 楼 sks8058 2018-05-29  
博主你好,请问怎么获取文本框的宽高和位置?谢谢
17 楼 lt26w 2014-07-01  
楼主,请教有没有办法高亮字体啊?
16 楼 cbxvsli 2011-09-22  
楼主,请教个问题:
   jacob如何将很多附件word中的内容copy到一个word中,不知道如何实现。用流的话附件中的table和image都没有了,只能copy文本,你是怎么处理copy问题的?
15 楼 zhangyongfeng 2010-07-12  
非常感谢楼主
14 楼 lyd967 2009-12-17  
楼主您好,,,,不知道您知不知道要在EXCEL中插入图片要怎么写呀。。。如有任何见教麻烦发邮件给我lytlyd@163.com
13 楼 逆风的香1314 2009-10-26  
请问如何利用Jacob生成word时自动添加页码呢?
格式如:
1/3
2/3
3/3

如有见教,请发邮件:wangxiaojs@gmai.co中 多谢
12 楼 dingqijie 2009-05-31  
我感觉你应该是jacob.dll配置问题,你可以测试下面三种方式:
一、将jacob.dll拷贝到windows/system32下
二、如果通过上述配置还不能正确运用该包,则将jacob.dll放入 Java\jdk\jre\bin目录下,
三、如果不行,将jacob.dll放到Java\jdk\jre\lib\ext目录下
jre在运行中要调用操作系统中的word库来执行相应的word命令。所以需要jacob.dll,不过有一些系统的补丁已经包含了这功能,所以我的系统运行就不需要这么配置。
11 楼 liyanhui 2009-05-31  
把你给我的内容粘贴过去,经过eclipse错误提示改成下面的样子:
                for(int i=1;i<=rowsCount;i++) 
                { 
                for(int m=1;m<=colsCount;m++) 
                { 
                Dispatch cell=(Dispatch) Dispatch.call(table,"Cell",new Variant(i),new Variant(m)).getDispatch(); //获得表格的操作句柄 
                Dispatch.call(cell, "Select"); //获得selection操作句柄 
               [color=red] Dispatch.put(selection, i*m,"Text" ); [/color]//对表格内容进行插入; 
                Dispatch.call(selection,"MoveRight"); //将光标右移; 
                } 
                } 
                [color=red]Dispatch.call(selection, "EndKey"); [/color]//调用endkey功能键。即将光标移到文档最后。

我在网上查了一下怕版本不稳定,我改用的是jacobBin_17和
jdk1.5,并把jacobBin_18的dll文件拷到system32下面
还是报
java.lang.UnsatisfiedLinkError: createInstance
	at com.jacob.com.Dispatch.createInstance(Native Method)
	at com.jacob.com.Dispatch.<init>(Dispatch.java)
	at com.jacob.activeX.ActiveXComponent.<init>(ActiveXComponent.java)
	at com.JacobToWordTest.main(JacobToWordTest.java:20)
Exception in thread "main" 

版本不支持 ,实在想不明白??????????
10 楼 liyanhui 2009-05-31  
[img][/img]我怀疑我是的jdk或jacob的版本不对,我应用你上面的内容,可是Dispatch.put()方法里的参数类型却和你给的不一样。如图1.bmp,你给的是:Dispatch.put(selection, "Text", i * m); // 对表格内容进行插入;在我工程里面有错误,提示改为Dispatch.put(selection,i * m, "Text"); // 对表格内容进行插入;
第二个问题是:
Dispatch.call(selection, "EndKey", 6); // 调用endkey功能键。即将光标移到文档最后
在我工程里提示我改为:如图2.jpg
苦恼啊!!!
期待楼主。。。。。
9 楼 dingqijie 2009-05-27  
那个问题不是版本的问题,是你的使用方式不对,
不能够使用Dispatch.put(selection, "7","222");
selection中根本就没有“7”这个属性啊,你怎么可以使用使用put赋值呢?
对表格的操作如下,添加了注释,有什么问题的话可以继续讨论:
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"); //获得selection操作句柄
        Dispatch.put(selection, "Text",i*m ); //对表格内容进行插入;
        Dispatch.call(selection,"MoveRight"); //将光标右移;
        }
        }
        Dispatch.call(selection, "EndKey",6); //调用endkey功能键。即将光标移到文档最后。
8 楼 liyanhui 2009-05-26  
liyanhui 写道

不知道楼主用的是jacob那个版本啊,我用的是jacob_1.11,但是却报错: com.jacob.com.ComFailException: Can't map name to dispid: 7 at com.jacob.com.Dispatch.invokev(Native Method) at com.jacob.com.Dispatch.invokev(Dispatch.java:886) at com.jacob.com.Dispatch.invoke(Dispatch.java:565) at com.jacob.com.Dispatch.put(Dispatch.java:844) at com.JacobToWordTest.main(JacobToWordTest.java:69) Exception in thread "main" 不能写入数据和图片

                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++) 
                { 
                	System.out.println("000 = " + i);
                for(int m=1;m<=colsCount;m++) 
                { 
                	System.out.println("1111m = " + m);
                Dispatch cell=Dispatch.call(table,"Cell",new Variant(i),new Variant(m)).getDispatch(); 
                Dispatch.call(cell, "Select"); 
                [color=red]Dispatch.put(selection, "7","222");[/color]

执行到红色的部分报错,所以不能往表格里写入数据
7 楼 liyanhui 2009-05-26  
不知道楼主用的是jacob那个版本啊,我用的是jacob_1.11,但是却报错:
com.jacob.com.ComFailException: Can't map name to dispid: 7
at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.com.Dispatch.invokev(Dispatch.java:886)
at com.jacob.com.Dispatch.invoke(Dispatch.java:565)
at com.jacob.com.Dispatch.put(Dispatch.java:844)
at com.JacobToWordTest.main(JacobToWordTest.java:69)
Exception in thread "main"

不能写入数据和图片
6 楼 dingqijie 2009-05-26  
liyanhui 写道

我现在正需要把页面的table内容以表格的方式写入到word中,不知道楼主的东西是否适合,不过先看看 ,支持!!!!

大家相互学习,碰到什么问题可以拿出来一起讨论提高
5 楼 liyanhui 2009-05-26  
我现在正需要把页面的table内容以表格的方式写入到word中,不知道楼主的东西是否适合,不过先看看 ,支持!!!!
4 楼 dingqijie 2009-03-26  
前段时间没来,今天刚看到问题:
Dispatch shapes=Dispatch.get(document, "Shapes").toDispatch();
        Dispatch newTable = Dispatch.call(shapes, "AddTextbox",new Variant(1),new Variant(100),new Variant(100),new Variant(100),new Variant(100)).toDispatch();
这个可以满足你的要求。
3 楼 hb640101297 2009-03-10  
请问楼主我怎么用jacob创建一个文本框呢?很想知道,如果你知道的话请发我的邮箱吧,谢谢


我的邮箱:dave640101297@126.com
2 楼 dingqijie 2009-03-06  
互相交流。。。
1 楼 tataky 2009-03-06  
学习学习 提高提高

相关推荐

    jacob-1.18.zip + jacob-1.19.zip + jacob-1.20.zip 最新版本(包含dll文件)

    1.将jacob-1.18.zip下载解压,在文件夹中找到jacob-1.18-x64.dll,jacob-1.18-x86.dll,jacob.jar 2.通过 pom.xml 新增 &lt;groupId&gt;com.hynnet&lt;/groupId&gt; &lt;artifactId&gt;jacob &lt;version&gt;1.18 3. 如果是32位系统...

    jacob1.18源码和jar包

    JACOB里的总共有两个包: com jacob activeX:JACOB可以通过它调度(Dispatch)activeX 控件 com jacob com:JACOB通过它调用系统DLL (activeX是由微软公司推出的用于Internet的技术 以前曾经被称为OLE 和OCX...

    jacob.jar1.19及使用方法

    jacob.jar1.19及使用方法 解压后获得3个文件 jacob.jar jacob-1.17-x64.dll jacob-1.17-x86.dll jacob.jar 放到项目的lib目录 win7 jacob-1.17-x64.dll 放到C:\Windows\System32 jacob-1.17-x86.dll 放到C:\...

    jacob-1.18.jar/dll maven

    jacob-1.18的资源包还有 jacob-1.18-x64.dll和jacob-1.18-x86.dll. 把对应的windows版本扔到 C:\Windows\System32 并添加环境变量大到path中 “%SYSTEMROOT%\System32\jacob-1.18-x86.dll;”或者 “%SYSTEMROOT%\...

    使用JACOB工具调用COM组件

    本书使用的是jacob1.7,从如下网址可下载得到: http://danadler.com/jacob/  但是使用JACOB可以先编写COM组件,再在Java中调用,这在许多遗留在系统中COM组件是常用的,因此,使用JACOB对于系统的集成是比较实用的...

    jacob.jar jacob-1.19-x64.dll jacob-1.19-x86.dll

    jacob-1.19-x64.dll jacob-1.19-x86.dll jacob.jar LICENSE.TXT BuildingJacobFromSource.html EventCallbacks.html JacobComLifetime.html JacobThreading.html ReleaseNotes.html UsingJacob.html allclasses-...

    jacob操作word(超详细)

    1、把jacob.dll文件,复制到 windows\system32 目录下。(注:我用的时候这个方法不能运行) 2、 把jacob.dll放入 Java\jdk1.5.0_06\jre\bin目录下.把jacob.jar放入 Java\jdk1.5.0_0\jre\lib\ext 目录下.可以正常...

    jacob-1.18工具包.zip

    jacob-1.18 包含jacob-1.18-x64.dll jacob-1.18-x86.dll 亲测可用: 下面为转pdf使用方法 package com.pdf.doctopdf.pdf; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import ...

    jacob-1.17-M2

    jacob-1.17-M2jacob-1.17-M2jacob-1.17-M2jacob-1.17-M2jacob-1.17-M2jacob-1.17-M2jacob-1.17-M2jacob-1.17-M2jacob-1.17-M2jacob-1.17-M2jacob-1.17-M2jacob-1.17-M2jacob-1.17-M2jacob-1.17-M2jacob-1.17-M2jacob...

    com.jacob.jacob_1.10jar包下载.zip

    com.jacob.jacob_1.10jar包下载

    jacob 1.20 jacob-1.20-x64.dll jacob-1.20-x86.dll

    jacob.jar1.20及使用方法 解压后获得3个文件 jacob.jar jacob-1.20-x64.dll jacob-1.20-x86.dll 包含(32位,64位)以及文档 jacob(java com bradge) 通过调用MS OFFICE可以快捷的将word excel ppt等转换为pdf

    jacob1.7+1.9

    jacob1.7的jar文件 + jacob1.9的dll文件 jdk版本:1.6.0_27(目测1.6跟1.5都是可以的→。→) 组合理由引用:“为什么又有1.7又有1.9呢,是因为1.7的jacob.dll不好用,1.9的jacob.dll配合1.7的jacob.jar就好用了,...

    jacob.jar资源下载

    jacob.jar资源下载 “JACOB一个Java-COM中间件.通过这个组件你可以在Java应用程序中调用COM组件和Win32 libraries。” 首先下载Jacob包,JDK1.5以上需要使用Jacob1.9版本(JDK1.6尚未测试),与先前的Jacob1.7差别...

    最新版jacob 1.20.jar jacob-1.20-x64.dll jacob-1.20-x86.dll

    2020最新版jacob 1.20.jar jacob-1.20-x64.dll jacob-1.20-x86.dll

    jacob.jar以及jacob.dll文件

    java操作office的jar包,其中jacob.jar放在项目jar文件夹中,jacob.dll放在C:\Windows\System32文件夹下

    jacob1.19(包含jacob-1.19-x64.dll和jacob-1.19-x86.dll)

    jacob1.19(包含jacob-1.19-x64.dll和jacob-1.19-x86.dll)

    jacob(包含jacob-1.19-x64.dll和jacob-1.19-x86.dll)

    jacob1.19(包含jacob-1.19-x64.dll和jacob-1.19-x86.dll)

    jacob语音生成文件,jacob x64.dll和jacob.jar 为1.9

    jacob 1.9语音生成文件,jacob x64.dll和jacob.jar 版本为1.9。 jacob.dll放入 C:\Program Files\Java\jdk1.8.0_121\jre\bin jacob.jar通过idea配置到项目dependencies

    jacob安装包操作office的jacob安装包

    jacob操作office工具的安装包,分操作系统64、32位 复制到 C:\Windows\SysWOW64 、 C:\Windows\System32 以及jdk、jre/bin下各

    jacob-1.14.3.jar、jacob-1.14.3-x64.dll

    java-COM中间件,压缩包里包含jacob-1.14.3.jar、jacob-1.14.3-x64.dll

Global site tag (gtag.js) - Google Analytics