准备工作 You need to have two files: jacob.jarand jacob.dll. You put the former in your classpath and the latter in c:windowssystem32 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 at its beginning. 你需要由两个文件:jacob.jar和 jacob.dll。需要把jacob.jar放到你工程的classpath中并且把jacob.dll放到c:windowssystem32目录下或者其他相应的目录下。我在win98和win xp下面做过测试,两者都是使用的office97。 让我们开始吧 First, I will create some variables; you can change them almost arbitrarily. They are pretty self explaining. 首先,我需要新建一些变量;你可以按照自己的意愿随意修改。看变量的名称大体上都能知道具体的意思。
sOldTextholds the label that I will search and replace. tVisibleis only true for 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 base variables. 现在,我们将打开word并且根据上面定义的基础变量来读取文档的内容。
Run this. It should open word, but dont do something cool. 执行上面代码,将会打开一个word,但是上面也不做。 oDocumentsholds the list of documents. oDocumentholds our specific document file_in.doc. oSelectionand oFindare objects we need for the next step, selecting and inserting. oDocuments保存了文档的列表(因为是多文档应用程序)。oDocument被认为是指定的文件file_in.doc. oSelection和 oFind是我们要进行下一步操作的对象,主要用来进行搜索和插入操作。
Now we search for sOldText, execute the search (which results in the label being selected inside Word), and replace that selection with the new text (which, in turn, is also selected). 现在我们搜索sOldText内容,执行搜索语句,并且使用新的文本替代选中的项目。 So next, we leave that select stuff. 因此下一步,我们离开选择的东西。
We move the cursor down, effectively leaving the selection (yes, it works just 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 the next-to-be-typed text). 现在我们希望格式化文本。我们一直采用的方式是先选中文本,然后进行下一步操作,下面是具体的使用方法。
Now the selected text (the " So we got ... BR. ") is both bold and italic.
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 be useful for you. Using the MoveDownand 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 one worked for me. 在Jacob邮件列表里面有好多关于保存和关于的建议,但是这个是我使用的。
Dont ask me why. It just works. 不要问我为什么,它确实是有效的。
This is straigthforward. No sweat. 插入图片 Yes its possible to embed images pretty easy. 使用Jacob来插入图片是件很容易的事情。
Well, it just works the way shown by the mailing list. Dont ask me about the image format (text flow and such) though. Better, if you know it, mail me. |