0005-这个写的不错,这在做关于这个功能的项目

2023-10-18 02:32
文章标签 项目 功能 不错 0005

本文主要是介绍0005-这个写的不错,这在做关于这个功能的项目,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

使用Jacob开源插件操作Microsoft Word

使用Jacob开源插件操作Microsoft Word

主题

     最近项目中要进行公文的操作,所有就想到了使用java来操作word,找了一些资料,都是比较零散、杂乱的东西,今早向Google大神求救,终于发现了一位热心的国外同鞋的文章,收获颇多,因此将此文翻译出来,供大家参考,本文英语水平很烂,但是为了学习同鞋你一定要hold住。

"Am I the only person on this planet who wants to write MS Word fileswith Java?"
- Me, after researching Jacob, POI, WordBean and others, all to noinformational avail.

“难道我是这个星球上唯一的一个愿意使用java操作Microsoft Word文件的人吗?”

-我在研究完jacob,POI,WordBean和其他开源插件后发现没有好的帮助信息可以供我使用。

Jacob介绍

Jacob is a Java/COM bridge provided by DanAdlerunder a semi GPL license (may not be used ina commercial product targetted at java developers, e.g. virtual machines,debuggers. The chance that you are not allowed to use it is very slim).

     Jacob是在半GPL许可证约束下的java/COM桥接软件,作者是Dan Adler.注:半GPL许可证是使用者不允许将软件应用于java开发的商业化软件中,比如虚拟机、调试器等。但是你不被允许使用的机会是很小的。

There is no documentation available concerning the practical use of anyMicrosoft applications; it is, so Adler, intended as a generic Java/COM bridgeand not some MS Office API. However, M. Bigatti made a FAQ, which IMHO is not too useful when it comes to MS Word; and there isa Jacob Mailing List, where I got most of my information, even if it was tedious work.

     这里没有任何关于微软产品的实际使用的说明;因此,作者的想法是想它作为一个通用的java/COM桥组件,而不是作为一个操作word的API。然而,M. Bigatti提出了一个问题,恕我直言它对于解决word的问题是不是太有用了;并且有一个Jacob邮件列表,在哪里我得到了大部分的信息,尽管这个工作单调和乏味。

Now, this is a tutorial entirely dedicated to the handling ofMicrosoft Word with Jacob. If you want Excel stuff, I would rather recommend POI, hosted at the ApacheFoundation; they have good excel support, but only wordscratchpad stuff. If you just need to insert some unformatted text, an easiersolution is the WordBean by Müller&Stein.

      现在,这是一个完全致力于使用Jacob对Word进行操作的指导材料。如果你希望使用Excel,那么我宁愿推荐你使用POI,由阿帕奇基金会维护的项目;POI由对Excel由很好的支持,但是在Word方面还有点欠缺。如果你只想插入一些没有格式的文字,那么可以使用一些简单点的解决方案,比如使用Müller&Stein编写的WordBean软件。

A good alternative to using Jacob may be Jawin, which follows exactly the same goal, namely dispatching calls to COMobjects.

     还有一个Jacob的替代品是Jawin,这个遵循着与Jacob一样的目标,也是通过调用COM组件的方式调用的。

作者

This document is far from complete; I am always open for suggestions, tipsand any enhancements. If you know something, please tell me. The absence ofanother site like this, in contrast to all the questions on JDC Search andJacob Mailing list, imply that my page will be of some usability. My mailadress is kain at the abovedomain.

     这篇文档要完成还要花费一段时间;我一直是采纳大家的意见、建议和一些改进方法。如果你知道关于这个方面的东西,请告诉我。在对比了JDC搜索和Jacob邮件列表里面的所有问题后,在喜欢这个网页的情况下我的文档还是有一定的可用性的。在上述域中我的邮箱地址是Kain.

Update 2006-04: I had a niceemail exchange with a guy named Jean Helou; he summarised his experiences withJacob in a wikidocumentation: it contains a section on macros, and isbased on ms word xp. Also, he provided me with a link to the useful ms officeobject model documentation.

Update 2006-04:我与一个叫Jean Helou的家伙又一段精彩的邮件交流;他在维基百科上面总结了他对Jacob的使用经验:是在基于XP系统中的Word关于宏的使用。他还提供给我一个关于微软对象模型文档的使用连接。

Update 2006-08: A nice girlnamed Kathrin Eichler emailed me a section on hyperlinks; it is included below.She is using Office XP. Thanks Kathrin!

Update 2006-08:一位叫Kathrin Eichler的美女发给我了一份关于超链接使用的邮件;在下面有介绍。她使用的是xp版的office软件,感谢她。

准备工作

You need to have two files: jacob.jar and jacob.dll. You put the former in your classpath and the latter inc:\windows\system32 or your equivalent. I tested jacob both win 98 and win xp,both with ms office 97.

Then, I assume you create a new java class, make a new main(String[] asArgs)method and are atits beginning.

你需要由两个文件:jacob.jar 和 jacob.dll。需要把jacob.jar放到你工程的classpath中并且把jacob.dll放到c:\windows\system32目录下或者其他相应的目录下。我在win98和win xp下面做过测试,两者都是使用的office97。

让我们开始吧

First, I will create some variables; you can change them almostarbitrarily. They are pretty self explaining.

首先,我需要新建一些变量;你可以按照自己的意愿随意修改。看变量的名称大体上都能知道具体的意思。

String sDir = "c:\\java\\jacob\\";

String sInputDoc =sDir + "file_in.doc";

String sOutputDoc =sDir + "file_out.doc";

String sOldText ="[label:import:1]";

String sNewText ="I am some horriblylong sentence, so long that [insert bullshit here]";

boolean tVisible =true;

boolean tSaveOnExit =false;

sOldText holds the label that I will search and replace. tVisible is only truefor debugging purposes, to see whats going on. tSaveOnExitis false since I save explicitly.

sOldText表示我要进行搜索和替换的文本。tVisible只有在调试的时候才设置为true,这样你可以看见具体的操作过程。tSaveOnExit为false直到我明确的进行保存操作。

Now, we will open word and read the document as well as some basevariables.

现在,我们将打开word并且根据上面定义的基础变量来读取文档的内容。

    ActiveXComponent oWord = new ActiveXComponent("Word.Application");

    oWord.setProperty("Visible",new Variant(tVisible));

    Object oDocuments =oWord.getProperty("Documents").toDispatch();

    Object oDocument =Dispatch.call(oDocuments, "Open", sInputDoc).toDispatch();

    Object oSelection =oWord.getProperty("Selection").toDispatch();

    Object oFind = oWord.call(oSelection,"Find").toDispatch();

Run this. It should open word, but dont do something cool.

执行上面代码,将会打开一个word,但是上面也不做。

oDocuments holds the list of documents. oDocument holds our specific document file_in.doc. oSelection and oFindare objects weneed for the next step, selecting and inserting.

oDocuments保存了文档的列表(因为是多文档应用程序)。oDocument被认为是指定的文件file_in.doc. oSelection 和 oFind是我们要进行下一步操作的对象,主要用来进行搜索和插入操作。

 Dispatch.put(oFind, "Text", sOldText);

    Dispatch.call(oFind, "Execute");

    Dispatch.put(oSelection, "Text", sNewText);

Now we search for sOldText, execute the search (which results in the label being selected insideWord), and replace that selection with the new text (which, in turn, is alsoselected).

现在我们搜索sOldText内容,执行搜索语句,并且使用新的文本替代选中的项目。

So next, we leave that select stuff.

因此下一步,我们离开选择的东西。

Dispatch.call(oSelection,"MoveDown");

Dispatch.put(oSelection,"Text", "\nSo we got the next line including BR.\n");

We move the cursor down, effectively leaving the selection (yes, it worksjust like a VB macro inside Word; works also with MoveUp, MoveLeft, MoveRight).Then, we insert other text.

我们将光标向下移动,有效的离开选中的项目,然后开始插入其他文本。(它工作起来类似于嵌入到word中的VB宏;使用起来有MoveUp, MoveLeft, MoveRight功能)

Now we want to format text. Since we always operate with selected text(the whole "TypeText" directive mentioned at the mailing list didnt quite work for me),we make the format afterwards (unto the selected text, not unto thenext-to-be-typed text).

现在我们希望格式化文本。我们一直采用的方式是先选中文本,然后进行下一步操作,下面是具体的使用方法。

Object oFont =Dispatch.get(oSelection, "Font").toDispatch();

    Dispatch.put(oFont, "Bold", "1");

    Dispatch.put(oFont, "Italic", "1");

    Dispatch.put(oFont, "Underline", "0");

Now the selected text (the "\nSo we got ... BR.\n") is both bold and italic.

Object oAlign =Dispatch.get(oSelection, "ParagraphFormat").toDispatch();

    Dispatch.put(oAlign, "Alignment", "3");

And now the alignment is block (0 - Left, 1 - Center, 2 - Right, 3 -Block; at least I hope so ;-). For now, this is the minimal thing that can beuseful for you. Using the MoveDown and Textdirectives you can do the basics.

现在对其是按照快的方式的(0代表左对齐,1是居中,2是右对齐,3是快对齐)。现在这是对你有所帮助而要做的最少的事情,使用MoveDown和Text直接做你想做的事情。

保存和关闭

Well, there were a lot of suggestions on the mailing list, but that oneworked for me.

在Jacob邮件列表里面有好多关于保存和关于的建议,但是这个是我使用的。

 Object oWordBasic = Dispatch.call(oWord,"WordBasic").getDispatch();

    Dispatch.call(oWordBasic, "FileSaveAs", sOutputDoc);

Dont ask me why. It just works.

不要问我为什么,它确实是有效的。

Dispatch.call(oDocument,"Close",newVariant(tSaveOnExit));

    oWord.invoke("Quit",new Variant[0]);

This is straigthforward. No sweat.

插入图片

Yes its possible to embed images pretty easy.

使用Jacob来插入图片是件很容易的事情。

 String sImgFile = sDir + "image.png";

    Dispatch.call(oSelection, "MoveDown");

    Object oImage = Dispatch.get(oSelection,"InLineShapes").toDispatch();

    Dispatch.call(oImage, "AddPicture", sImgFile);

Well, it just works the way shown by the mailing list. Dont ask me aboutthe image format (text flow and such) though. Better, if you know it, mail me.

超链接的使用

Hyperlinks are also pretty straightforward (courtesy Kathrin Eichler,under Office XP):

超链接也可以非常完美的直接使用。

String sHyperlink ="http://www.google.com";

    Dispatch.put(oSelection, "Text", "Text for the link to Google");

    Object oRange = Dispatch.call(oSelection,"Range");

    Object oLink = Dispatch.get(oDocument,"Hyperlinks").toDispatch();

    Dispatch.call(oLink, "Add", oRange, sHyperlink);

I have not tried that personally yet (under Office 97), so your mileagemay vary.

表格

Holy slimily, I got no idea yet. Am researching VB code and stuff. I gotsome suggestions from the mailing list how to add a row to a table and how tonavigate a table, but creating... Well, still to come.

到现在都没有什么好的注意。我研究了VB代码和其工作原理。我从邮件列表那里得到了一些建议,怎么去添加一行和越过一个表格,但是对于创建操作。。。。还是需要进一步研究啊。

列表/枚举

The VB code looks like shit. I have no idea how to work here. Still tocome.

VB代码看起来像一坨屎。到现在都没有号的想法去实现怎么操作,还需要努力。

总结

 


这篇关于0005-这个写的不错,这在做关于这个功能的项目的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/229610

相关文章

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

Qt使用QSqlDatabase连接MySQL实现增删改查功能

《Qt使用QSqlDatabase连接MySQL实现增删改查功能》这篇文章主要为大家详细介绍了Qt如何使用QSqlDatabase连接MySQL实现增删改查功能,文中的示例代码讲解详细,感兴趣的小伙伴... 目录一、创建数据表二、连接mysql数据库三、封装成一个完整的轻量级 ORM 风格类3.1 表结构

深度解析Java项目中包和包之间的联系

《深度解析Java项目中包和包之间的联系》文章浏览阅读850次,点赞13次,收藏8次。本文详细介绍了Java分层架构中的几个关键包:DTO、Controller、Service和Mapper。_jav... 目录前言一、各大包1.DTO1.1、DTO的核心用途1.2. DTO与实体类(Entity)的区别1

mysql表操作与查询功能详解

《mysql表操作与查询功能详解》本文系统讲解MySQL表操作与查询,涵盖创建、修改、复制表语法,基本查询结构及WHERE、GROUPBY等子句,本文结合实例代码给大家介绍的非常详细,感兴趣的朋友跟随... 目录01.表的操作1.1表操作概览1.2创建表1.3修改表1.4复制表02.基本查询操作2.1 SE

Golang如何用gorm实现分页的功能

《Golang如何用gorm实现分页的功能》:本文主要介绍Golang如何用gorm实现分页的功能方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录背景go库下载初始化数据【1】建表【2】插入数据【3】查看数据4、代码示例【1】gorm结构体定义【2】分页结构体

如何在Spring Boot项目中集成MQTT协议

《如何在SpringBoot项目中集成MQTT协议》本文介绍在SpringBoot中集成MQTT的步骤,包括安装Broker、添加EclipsePaho依赖、配置连接参数、实现消息发布订阅、测试接口... 目录1. 准备工作2. 引入依赖3. 配置MQTT连接4. 创建MQTT配置类5. 实现消息发布与订阅

springboot项目打jar制作成镜像并指定配置文件位置方式

《springboot项目打jar制作成镜像并指定配置文件位置方式》:本文主要介绍springboot项目打jar制作成镜像并指定配置文件位置方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录一、上传jar到服务器二、编写dockerfile三、新建对应配置文件所存放的数据卷目录四、将配置文

怎么用idea创建一个SpringBoot项目

《怎么用idea创建一个SpringBoot项目》本文介绍了在IDEA中创建SpringBoot项目的步骤,包括环境准备(JDK1.8+、Maven3.2.5+)、使用SpringInitializr... 目录如何在idea中创建一个SpringBoot项目环境准备1.1打开IDEA,点击New新建一个项

Java Web实现类似Excel表格锁定功能实战教程

《JavaWeb实现类似Excel表格锁定功能实战教程》本文将详细介绍通过创建特定div元素并利用CSS布局和JavaScript事件监听来实现类似Excel的锁定行和列效果的方法,感兴趣的朋友跟随... 目录1. 模拟Excel表格锁定功能2. 创建3个div元素实现表格锁定2.1 div元素布局设计2.

HTML5实现的移动端购物车自动结算功能示例代码

《HTML5实现的移动端购物车自动结算功能示例代码》本文介绍HTML5实现移动端购物车自动结算,通过WebStorage、事件监听、DOM操作等技术,确保实时更新与数据同步,优化性能及无障碍性,提升用... 目录1. 移动端购物车自动结算概述2. 数据存储与状态保存机制2.1 浏览器端的数据存储方式2.1.