成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Jacob開源插件操作Microsoft Word

開發 后端
Jacob為一種Java程序,它允許在java中調用com接口自動組件,它使用JNI(本地調用程序)來進行本地調用COM庫。它可運行在x86和支持32位和64位Java虛擬機 X64的環境。

  使用Jacob開源插件操作Microsoft Word

  主題

  最近項目中要進行公文的操作,所有就想到了使用Java來操作word,找了一些資料,都是比較零散、雜亂的東西,今早向Google大神求救,終于發現了一位熱心的國外同學的文章,收獲頗多,因此將此文翻譯出來,供大家參考,本文英語水平很爛,但是為了學習同鞋你一定要hold住。

  "Am I the only person on this planet who wants to write MS Word files with Java?"

  - Me, after researching Jacob, POI, WordBean and others, all to no informational avail.

  “難道我是這個星球上唯一的一個愿意使用Java操作Microsoft Word文件的人嗎?”

  -我在研究Jacob,POI,WordBean和其他開源插件后發現沒有好的幫助信息可以供我使用。

  Jacob介紹

  Jacob is a Java/COM bridge provided by Dan Adlerunder a semi GPL license (may not be used in a 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 any Microsoft applications; it is, so Adler, intended as a generic Java/COM bridge and 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 is a 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 of Microsoft Word with Jacob. If you want Excel stuff, I would rather recommend POI, hosted at the Apache Foundationthey have good excel support, but only word scratchpad stuff. If you just need to insert some unformatted text, an easier solution is the WordBean by Mller&Stein.

  現在,這是一個完全致力于使用Jacob對Word進行操作的指導材料。如果你希望使用Excel,那么我寧愿推薦你使用POI,由阿帕奇基金會維護的項目;POI由對Excel由很好的支持,但是在Word方面還有點欠缺。如果你只想插入一些沒有格式的文字,那么可以使用一些簡單點的解決方案,比如使用Mller&Stein編寫的WordBean軟件。

  A good alternative to using Jacobmay be Jawin, which follows exactly the same goal, namely dispatching calls to COM objects.

  還有一個Jacob的替代品是Jawin,這個遵循著與Jacob一樣的目標,也是通過調用COM組件的方式調用的。

  作者

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

  這篇文檔要完成還要花費一段時間;我一直是采納大家的意見、建議和一些改進方法。如果你知道關于這個方面的東西,請告訴我。在對比了JDC搜索和Jacob郵件列表里面的所有問題后,在喜歡這個網頁的情況下我的文檔還是有一定的可用性的。在上述域中我的郵箱地址是Kain.

  Update 2006-04: I had a nice email exchange with a guy named Jean Helou; he summarised his experiences with Jacob in a wiki documentation: it contains a section on macros, and is based on ms word xp. Also, he provided me with a link to the useful ms office object model documentation.

  Update 2006-04:我與一個叫Jean Helou的家伙又一段精彩的郵件交流;他在維基百科上面總結了他對Jacob的使用經驗:是在基于XP系統中的Word關于宏的使用。他還提供給我一個關于微軟對象模型文檔的使用連接。

  Update 2006-08: A nice girl named 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.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.

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

  1.   String sDir = "c:\java\jacob\";  
  2.   String sInputDoc = sDir + "file_in.doc";  
  3.   String sOutputDoc = sDir + "file_out.doc";  
  4.   String sOldText = "[label:import:1]";  
  5.   String sNewText = "I am some horribly long sentence, so long that [insert bullshit here]";  
  6.   booleantVisible = true;  
  7.   booleantSaveOnExit = false

  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并且根據上面定義的基礎變量來讀取文檔的內容。

 

  1.   ActiveXComponent oWord = newActiveXComponent("Word.Application");  
  2.   oWord.setProperty("Visible", newVariant(tVisible));  
  3.   Object oDocuments = oWord.getProperty("Documents").toDispatch();  
  4.   Object oDocument = Dispatch.call(oDocuments, "Open", sInputDoc).toDispatch();  
  5.   Object oSelection = oWord.getProperty("Selection").toDispatch();  
  6.   Object oFind = oWord.call(oSelection, "Find").toDispatch(); 

 

  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是我們要進行下一步操作的對象,主要用來進行搜索和插入操作。

  1.   Dispatch.put(oFind, "Text", sOldText);  
  2.   Dispatch.call(oFind, "Execute");  
  3.   Dispatch.put(oSelection, "Text", sNewText); 

 

  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.

  因此下一步,我們離開選擇的東西。

  1.   Dispatch.call(oSelection, "MoveDown");  
  2.   Dispatch.put(oSelection, "Text"" So we got the next line including BR. "); 

   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).

  現在我們希望格式化文本。我們一直采用的方式是先選中文本,然后進行下一步操作,下面是具體的使用方法。

  1.   Object oFont = Dispatch.get(oSelection, "Font").toDispatch();  
  2.   Dispatch.put(oFont, "Bold""1");  
  3.   Dispatch.put(oFont, "Italic""1");  
  4.   Dispatch.put(oFont, "Underline""0"); 

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

 

  1.   Object oAlign = Dispatch.get(oSelection, "ParagraphFormat").toDispatch();  
  2.   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 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郵件列表里面有好多關于保存和關于的建議,但是這個是我使用的。

  1.   Object oWordBasic = Dispatch.call(oWord, "WordBasic").getDispatch();  
  2.   Dispatch.call(oWordBasic, "FileSaveAs", sOutputDoc); 

  Dont ask me why. It just works.

  不要問我為什么,它確實是有效的。

  1.   Dispatch.call(oDocument, "Close", newVariant(tSaveOnExit));  
  2.   oWord.invoke("Quit", newVariant[0]); 

   This is straigthforward. No sweat.

  插入圖片

  Yes its possible to embed images pretty easy.

  使用Jacob來插入圖片是件很容易的事情。

  1.   String sImgFile = sDir + "image.png";  
  2.   Dispatch.call(oSelection, "MoveDown");  
  3.   Object oImage = Dispatch.get(oSelection, "InLineShapes").toDispatch();  
  4.   Dispatch.call(oImage, "AddPicture", sImgFile); 

   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.

  超鏈接的使用

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

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

  1.   String sHyperlink = "http://www.google.com";  
  2.   Dispatch.put(oSelection, "Text""Text for the link to Google");  
  3.   Object oRange = Dispatch.call(oSelection, "Range");  
  4.   Object oLink = Dispatch.get(oDocument, "Hyperlinks").toDispatch();  
  5.   Dispatch.call(oLink, "Add", oRange, sHyperlink); 

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

  表格

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

  到現在都沒有什么好的注意。我研究了VB代碼和其工作原理。我從郵件列表那里得到了一些建議,怎么去添加一行和越過一個表格,但是對于創建操作。。。。還是需要進一步研究啊。

  列表/枚舉

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

  VB代碼看起來像一坨屎。到現在都沒有號的想法去實現怎么操作,還需要努力。

  總結

  上面是一個國外友人的文章,主要介紹了Jacob的使用方法,希望對有志于使用Java操作Word的童鞋有所幫助。

原文:http://www.cnblogs.com/rushoooooo/archive/2011/10/28/2227395.html

【編輯推薦】

  1. Ruby開發時間表公布 2.0將于2013年發布
  2. 使用Ruby和Twitter來進行數據挖掘
  3. Ruby開發者應該熟悉的10個工具
  4. Ruby的文檔系統RDoc 3.10發布
  5. JRuby和Java 7 我們可以期待什么
責任編輯:彭凡 來源: 博客園
相關推薦

2011-09-05 13:54:10

UbuntuMicrosoft W

2009-12-09 10:51:18

ibmdwJava

2015-01-23 11:23:03

云計算微軟Orleans

2021-02-22 07:37:26

Windows操作系統Microsoft W

2021-04-17 23:10:59

Python微軟Word

2009-08-25 18:16:34

.NET操作Word

2021-05-12 12:48:13

Microsoft開源項目Windows

2009-08-19 10:16:15

C#操作Word

2011-11-21 10:04:52

Java開源Google

2022-07-11 08:14:42

微軟開源

2009-08-19 10:42:08

C#操作Word表格

2022-06-19 14:30:00

Microsoft山寨產品商店

2014-03-26 09:38:17

2013-02-22 14:45:01

.NETC#Xcode

2012-05-29 15:22:03

JACOBJava

2009-08-19 11:13:49

C#操作Word

2009-08-19 11:34:06

C#操作Word

2009-08-19 09:42:52

C#操作Word書簽

2009-09-01 13:25:25

C#Word文檔替換

2009-09-01 11:21:02

C#讀取word內容
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲综合色丁香婷婷六月图片 | 国产人久久人人人人爽 | 国产精品视频免费看 | 精品九九 | 色噜噜亚洲男人的天堂 | 成人在线观看亚洲 | 国产亚洲精品区 | 精产嫩模国品一二三区 | 精品1区2区3区4区 | 九九免费观看视频 | 无码日韩精品一区二区免费 | 日韩乱码一二三 | 欧美一级毛片久久99精品蜜桃 | 中文字幕1区 | 日韩中字幕 | 黄色国产大片 | 男人的天堂亚洲 | 精品一区国产 | 久久久欧洲 | 欧美精品一区二区三区四区 在线 | 国产一区二区在线视频 | 国产精品1 | 精品综合久久 | 国产日韩在线观看一区 | 中文字幕不卡在线观看 | 日韩欧美在线播放 | 美女福利视频 | 亚洲国产一区二区三区 | 影音先锋中文字幕在线观看 | 成人h免费观看视频 | 日韩人体在线 | 少妇诱惑av | 亚洲国产精品成人综合久久久 | 91免费观看 | 伊人伊人伊人 | 中文字幕第十五页 | h在线免费观看 | 久久久久久久久久久成人 | 国产精品久久久久久亚洲调教 | 亚洲欧美日韩网站 | 亚洲第1页|