First of all, let's understand the concept of JACOB, which is the abbreviation of JAVA Bridge, and provides the function of automatic access. You can also access the components under the windows platform or the win system library through JNI function. This is the result of an open source project that began in 2000, and many users have made their own contributions to the project.
Jacob download address? group _ id = & amppackage_id=
I downloaded Jacob and Jacob's version here.
Download here is the latest release version of Jacob.
In addition, there are ways to operate word in java (personally, it is best for jacob to expand the Internet, except poi, which is almost completely realized by java technology):
(): Apache POI Java API accesses Microsoft format file (); Word processing is not strong enough to handle Excel functions, but they are all done by java without component support.
(): java word is a component (class library) that calls MS Office Word documents in java programs. This component provides a simple interface for java programs to call their services to operate Word documents (it seems that java technology is also used);
() It is best to use third-party controls to operate the functions of word in the web development language. Look at this SOAOFFICE. You can also write VBA in js.
Second, install Jacob.
Jacob's installation is very simple. We unzipped the downloaded jacob_ zip and found two files, jacob dll and jacob jar. If it is Jacob, it is jacob x dll (bit machine and jacob x dll). Jacob jar, on the other hand, can be placed directly in the system folder of the system, even without registration (or copied to the bin directory of jdk or jre, or the directory where the current test file is located, as long as it is in the java library path). Jacob jar can be set in the classpath, or an extension library can be set in the project of IDE development environment. This is how I use jacob x dll or copy it to %Tomcat %\bin directory, and copy jacob jar to %Tomcot %\Share\ When I use lib directory, I feel that putting it here is the final solution. When you put it anywhere, there is a problem. After I used it like this, I haven't had any problems because of different systems. Of course, you are working on a network project.
Pay attention to writing and installing word with jacob. If I installed word, I wouldn't need jacob.
Several configuration methods of jacob dll (seen on the internet)
: :
Copy the jacob dll file to the windows\system directory (note that this method doesn't work when I use it).
Put jacob dll in Java\jdk _ \jre\bin directory, and jacob jar in Java\jdk _ \jre\lib\ext directory to run normally.
Putting jacob dll in the \glc\src directory and jacob jar in the WEB INF\lib directory can also work normally.
Three uses (the following is a word operation class I rewritten. Remember to send me a copy when I hope interested friends improve)
//Note that the key to java word operation is to locate the operation object;
Import jacob activeX ActiveXComponent
Import and delivery;
Import variants;
/**
* jacob operates the MSword class.
* @ Author
*/
Public class WordBean {
// word document
Private dispatch documents;
// word runs the program object
Private ActiveXComponent word
//All word document collections
Private dispatch documents;
//Selected range or insertion point
Private dispatch option;
private boolean saveOnExit = true
The public WordBean () threw an exception {
if (word == null) {
Word = new ActiveXComponent( Word application);
Word setProperty (false));; //Invisible open word
word set property(automation security new Variant()); //Disable macros
}
if (documents == null)
Documents = word getProperty(Documents)to dispatch();
}
/**
* Set exit parameters.
*
* @param saveOnExit
* boolean true saves the file when exiting, false does not save the file when exiting.
*/
public void setSaveOnExit(boolean saveOnExit){
this saveOnExit = saveOnExit
}
/**
* create a new word document
*
*/
public void createNewDocument() {
Doc = Dispatch call (document addition) to dispatch
Selection = Dispatch get to dispatch ();
}
/**
* Open an existing document.
*
* @param docPath
*/
public void open document(String docPath){
close document();
doc = Dispatch call(documents Open docPath)to Dispatch();
Selection = Dispatch get to dispatch ();
}
/**
* Open the protected document as read-only.
* @param docPath file full name
* @param password
*/
Public void opendocumentonlyread (String Docpath String PWD) throws an exception {
close document();
// doc = Dispatch invoke (document opening scheduling method
//new Object[]{ docPath new Variant(false)new Variant(true)new Variant(true)pwd }
//new int[])to dispatch(); //Open the word file
doc = Dispatch callN(documents Open new Object[]{ docPath new Variant(false))
new Variant(true)new Variant(true)pwd new Variant(false)})to dispatch();
Selection = Dispatch get to dispatch ();
}
Public Void Open Document (String Docpath String PWD) throws an exception {
close document();
doc = Dispatch callN(documents Open new Object[]{ docPath new Variant(false))
new Variant(false)new Variant(true)pwd })to dispatch();
Selection = Dispatch get to dispatch ();
}
/**
* Move the selection or insertion point up.
*
* @param pos
* Distance moved
*/
public void moveUp(int pos) {
If (select == null)
Selection = Dispatch get to dispatch ();
for(int I =; I & ltposi++)
Dispatch the call (select Move Up);
}
/**
* Move the selection or insertion point down.
*
* @param pos
* Distance moved
*/
Public void move down (int pos) (
If (select == null)
Selection = Dispatch get to dispatch ();
for(int I =; I & ltposi++)
Dispatch the call (select Move Down);
}
/**
* Move the selection or insertion point to the left.
*
* @param pos
* Distance moved
*/
Public void move left (int pos) (
If (select == null)
Selection = Dispatch get to dispatch ();
for(int I =; I & ltposi++) {
Dispatch the call (choose to move to the left);
}
}
/**
* Move the selection or insertion point to the right.
*
* @param pos
* Distance moved
*/
Public void movement (int pos) (
If (select == null)
Selection = Dispatch get to dispatch ();
for(int I =; I & ltposi++)
Dispatch the call (select the mobility right);
}
/**
* Move the insertion point to the first position in the file.
*
*/
public void moveStart() {
If (select == null)
Selection = Dispatch get to dispatch ();
Dispatch the call (select home key new Variant()););
}
/**
* Find text from the selection point or insertion point.
*
* @param toFindText
* the text to find
* @return boolean true found and selected text false no text found.
*/
@SuppressWarnings (static access)
public boolean find(String to find text){
If (tofind text = = null || tofind text equals ())
Returns false
//Start the query from the selected location.
Dispatch find = word call to dispatch ();
//Set what to look for.
Dispatch put (find text to find text);
//Look ahead
Assign put (find forward true);
//Set the format
Dispatch put (format found to be true);
//Case matching
Assign put (find matchcase true);
//Whole word matching
Dispatch put (find matchwholeword true);
//Find and select
Returns the dispatch call (Find Execute) getBoolean ();
}
/**
* Set the selection as replacement text.
*
* @param toFindText
* find string
* @param newText
* Replace what?
* @ Return
*/
public boolean replace text(String to find text String new text){
If (! ToFindText)
Returns false
Dispatch put (select text new text);
Return true
}
/**
* global replacement text
*
* @param toFindText
* find string
* @param newText
* Replace what?
*/
public void replaceAllText(String to find text String new text){
while (find(toFindText)) {
Dispatch put (select text new text);
Dispatch the call (select the mobility right);
}
}
/**
* Inserts a string at the current insertion point
*
* @param newText
* New string to insert
*/
public void insert text(String new text){
Dispatch put (select text new text);
}
/**
*
* @param toFindText
* the string to find.
* @param imagePath
* image path
* @ Return
*/
public boolean replace image(String to find text String image path){
If (! ToFindText)
Returns false
Dispatch call (dispatch get (selection inline shapes) to dispatch ()
add picture image path);
Return true
}
/**
* replace pictures globally
*
* @param toFindText
* find string
* @param imagePath
* image path
*/
public void replaceAllImage(String to find text String image path){
while (find(toFindText)) {
Dispatch call (dispatch get (selection inline shapes) to dispatch ()
add picture image path);
Dispatch the call (select the mobility right);
}
}
/**
* Inserts a picture at the current insertion point.
*
* @param imagePath
* image path
*/
public void insert image(String image path){
Dispatch call (dispatch get (selection inline shapes) to dispatch ()
add picture image path);
}
/**
* merge cells
*
* @param tableIndex
* @param fstCellRowIdx
* @param fstCellColIdx
* @param secCellRowIdx
* @param secCellColIdx
*/
public void merge cell(int table index int fstCellRowIdx int fstCellColIdx
int secCellRowIdx int secCellColIdx){
//All forms
Dispatch table = Dispatch get(doc table) to dispatch ();
//Form to be filled in
Dispatch Table = Dispatch Call (new variable of table item (table index))
toDispatch();
Dispatch fstCell = Dispatch call (table cell
New variant (fstCellRowIdx) new variant (fstCellColIdx))
toDispatch();
Dispatch secCell = dispatch call (table cell
SecCellRowIdx, secCellColIdx)
toDispatch();
Scheduling call (FST cell merge sec cell);
}
/**
* Fill in the data in the specified cell.
*
* @param tableIndex
* @param cellRowIdx
* @param cellColIdx
* @param txt
*/
public void putxttocell(int table index int cellRowIdx int cellColIdx
String txt) {
//All forms
Dispatch table = Dispatch get(doc table) to dispatch ();
//Form to be filled in
Dispatch Table = Dispatch Call (new variable of table item (table index))
toDispatch();
Scheduling unit = scheduling call (table cell new variable (cellRowIdx))
A new variant (cellcolidx) to dispatch ();
Dispatch call (cell selection);
Dispatch put (select text txt);
}
/**
* get the specified cell data
*
* @param tableIndex
* @param cellRowIdx
* @param cellColIdx
* @ Return
*/
Common string gettxtfromcell (int table index int cellrowidx int cellcolidx) (
//All forms
Dispatch table = Dispatch get(doc table) to dispatch ();
//Form to be filled in
Dispatch Table = Dispatch Call (new variable of table item (table index))
toDispatch();
Scheduling unit = scheduling call (table cell new variable (cellRowIdx))
A new variant (cellcolidx) to dispatch ();
Dispatch call (cell selection);
string ret =;
Ret = Dispatch get (select text) tostring ();
Ret = ret substring (ret length());); //Delete the last carriage return;
Return to ret
}
/**
* copy the clipboard data in the current document.
* @param pos
*/
Public void pasteExcelSheet (string position) (
moveStart();
if (this find(pos)) {
Dispatch textRange = Dispatch get (selection range) to dispatch ();
Dispatch call (textRange paste);
}
}
/**
* Copy the table to the location specified in the current document.
*
* @param pos
* the location specified in the current document
* @param tableIndex
The location of the copied form in the word document.
*/
public void copy table(String pos int table index){
//All forms
Dispatch table = Dispatch get(doc table) to dispatch ();
//Form to be filled in
Dispatch Table = Dispatch Call (new variable of table item (table index))
toDispatch();
Scheduling range = Dispatch get (table range) to Dispatch ();
Dispatch call (range copy);
if (this find(pos)) {
Dispatch textRange = Dispatch get (selection range) to dispatch ();
Dispatch call (textRange paste);
}
}
/**
* Copy the table in another document at the location specified in the current document.
*
* @param anotherDocPath
* The disk path of another document
* @param tableIndex
* The location of the copied form in another document.
* @param pos
* the location specified in the current document
*/
public void copy tablefrom another doc(String another docpath int table index
String position) {
Dispatch Doc = empty;
Try {
Doc = dispatch call (document opens another document path)
toDispatch();
//All forms
Dispatch table = Dispatch get(doc table) to dispatch ();
//Form to be filled in
Dispatching table = Dispatching call (table entry
new Variant(tableIndex))to dispatch();
Scheduling range = Dispatch get (table range) to Dispatch ();
Dispatch call (range copy);
if (this find(pos)) {
Dispatch textRange = Dispatch get (selection range)
toDispatch();
Dispatch call (textRange paste);
}
Catch (exception e) {
e printStackTrace();
} Finally {
If (doc! = null) {
Dispatch the call (doc closes the new variable (saveOnExit));;
doc = null
}
Lishi Xinzhi/Article/program/Java/hx/20 13 1 1/26342