It is very troublesome to control office controls in Java.
With jacob, things have become much easier.
However, it is very troublesome to implement flexible control of word in Java.
Below we will introduce several common word objects and some typical processing procedures, and hope to help you.
(Note: jdk1.3.2 runs Jacob normally, and jdk1.4 has a problem)
/** Word object */
Private activexcomponent word = NULL;
/** Document Object */
Private dispatch documents = NULL;
/** The selection object is an important object */
Private dispatch vselection = NULL;
/** One Word document */
Private dispatch wordfile = NULL;
1. Initialization
WORD = new activexcomponent ("word. application ");
Documents = word. getproperty ("documents"). todispatch ();
(It is easy to put Jacob under winnt/system32)
2. open the file
Wordfile = Dispatch. Invoke (
Documents,
"Open ",
Dispatch. method,
New object [] {
Strfilename,
New variant (true), // whether to convert confirmconversions
New variant (false) // read-only
}, New int [1]). todispatch ();
Vselection = word. getproperty ("selection"). todispatch ();
In word, when the selected content is converted, it does not need to be retrieved back and forth like a Java object. This object is always valid.
3. Display word
Word. setproperty ("visible", new variant (visible ));
4. Set the word location
Dispatch activewindow = Dispatch. Get (word, "application"). todispatch ();
Dispatch. Put (activewindow, "windowstate", new variant (0 ));
Dispatch. Put (activewindow, "TOP", new variant (0 ));
Dispatch. Put (activewindow, "Left", new variant (0 ));
Dispatch. Put (activewindow, "height", new variant (600 ));
Dispatch. Put (activewindow, "width", new variant (800 ));
To exchange data in Java with the word, it is often used to make some special marks on the word, using the find and replace methods, this method is not very good.
I personally think it is more convenient to use the hyperlink mode.
There are several advantages:
1. hyperlink has three zones for developers to use.
Activedocument. hyperlinks. Add
Anchor: = selection. Range,
Address: = "location", // address (available) has a disadvantage
Subaddress: = "", // sub-location (available)
Screentip: = "", // on-screen prompt
Texttodisplay: = "show content" // The best thing to use
We recommend that you use texttodisplay.
Address is replaced with the absolute path when it is saved.
For example
"AA. BB. CC"
It may be replaced
C:/Documents ents and settings/Administrator/My Documents ents/AA. BB. CC
2. You can perform automatic locating.
Hyperlinks can be used to obtain all hyperlinks in the article.
You can also obtain the hyperlink of the specified range.
3. Free Layout
4. copy and paste
Add hyperlink:
Dispatch hyperlinks = Dispatch. Get (wordfile, "hyperlinks"). todispatch ();
Dispatch range = Dispatch. Get (vselection, "range"). todispatch ();
Dispatch H = Dispatch. Invoke (hyperlinks,
"Add", dispatch. method, new object []
{Range,
New variant ("Address "),
New variant ("subaddress "),
New variant ("{table. fieldname}"), // recommended data link
New variant ("name")}, // content displayed in Word
New int [4]). todispatch ();
Dispatch hrange = Dispatch. Get (H, "range"). todispatch ();
Dispatch. Call (hrange, "select ");
// Set the font and color
Dispatch font = Dispatch. Get (vselection, "font"). todispatch ();
Dispatch. Put (font, "underline", new variant (0 ));
Dispatch. Put (font, "color", new variant (0 ));
// Cancel the selection
Dispatch. Call (vselection, "moveright", new variant (1), new variant (1 ));
Hyperlink replacement content:
1. Obtain all hyperlinks.
// Select an object
Dispatch. Call (dobject, "select ");
// Obtain the hyperlink set
Dispatch hyperlinks = Dispatch. Get (vselection, "hyperlinks"). todispatch ();
// Obtain the number of hyperlinks
Int nhyperlink = Dispatch. Get (hyperlinks, "Count"). toint ();
// Obtain a hyperlink
Dispatch hyperlink = Dispatch. Invoke (hyperlinks, "item ",
Dispatch. method, new object [] {New INTEGER (I + 1 )},
New int [1]). todispatch ()));
2. Replace content
Dispatch. Put (hyperlink, "texttodisplay", information );
3. Cancel the hyperlink and convert it into plain text.
Dispatch. Call (hyperlink, "delete ");
We recommend that you use tables for automatic expansion of batch data, which is convenient and simple.
Combine the above hyperlink technology. It will be very simple:
For example, the following data is available:
Dataa
Datab
1. List all tables
It is basically the same as listing all hyperlinks:
Private void gettables01 (dispatch objcet, vector vtablestore ){
Dispatch tables = Dispatch. Get (objcet, "tables"). todispatch ();
Int ntableamount = Dispatch. Get (tables, "Count"). toint ();
For (INT I = 0; I <ntableamount; I ++ ){
Dispatch table =
Dispatch
. Invoke (
Tables,
"Item ",
Dispatch. method,
New object [] {New INTEGER (I + 1 )},
New int [1])
. Todispatch ();
Vtablestore. Add (New dtable (table ));
Gettables01 (table, vtablestore); // process the table applied to the table
}
}
2. Controllable objects of tables
Dispatch drows = Dispatch. Get (dtable, "rows"). todispatch (); // all rows
Int nrows = Dispatch. Get (drows, "Count"). toint ();
3. Get the content of a row
Dispatch Drow =
Dispatch
. Invoke (
Rows,
"Item ",
Dispatch. method,
New object [] {New INTEGER (row + 1 )},
New int [1])
. Todispatch ();
Return Drow;
} Catch (comfailexception CFE)
{
/** Cases with merged rows */
Return NULL;
}
4. Obtain the hyperlink of a row.
Dhyperlink dhinrow [] = listhyperlinks (Drow );
5. Copy a row multiple times
Dispatch. Call (Drow, "select ");
Dispatch. Call (vselection, "copy ");
Int ncopynow = ndatablockrow-1;
For (INT ncopys = 0; ncopys <ncopynow; ncopys ++ ){
Try {
Dispatch. Call (vselection, "Paste ");
} Catch (exception e) {e. printstacktrace ();
// Sometimes the document is corrupted. You can ignore this issue and have actually pasted it.
}
}
6. Replace the content. You don't need to introduce it here.
Print preview:
Dispatch. Call (wordfile, "printpreview ");
Other functions
The macro recording of word, the VB editor, and auxiliary functions can be used to discover them.
Import com.jacb.com .*;
Import com. Jacob. ActiveX .*;
Public class dtable extends dobject {
Public int rows;
Public int Cols;
/**
* Dtable constructs a subannotation.
* @ Param D com.jacb.com. Dispatch
*/
Public dtable (com.jacb.com. Dispatch d ){
Dispatch drows = Dispatch. Get (d, "rows"). todispatch ();
Dispatch dcolumns = Dispatch. Get (d, "columns"). todispatch ();
Rows = Dispatch. Get (drows, "Count"). toint ();
Cols = Dispatch. Get (dcolumns, "Count"). toint ();
}
Public void fixrange (){
Super. fixrange ();
Dispatch drows = Dispatch. Get (dobject, "rows"). todispatch ();
Dispatch dcolumns = Dispatch. Get (dobject, "columns"). todispatch ();
Rows = Dispatch. Get (drows, "Count"). toint ();
Cols = Dispatch. Get (dcolumns, "Count"). toint ();
}