Java control of Office controls is a cumbersome thing.
Things have become much simpler since Jacob.
But it's still a hassle to implement Java's flexible control of word.
Here are some common word objects as well as some typical processing processes that I hope will help you.
(Please note: JDK1.3.2 running Jacob is more normal, JDK1.4 has problems)
/** Word Object */
Private activexcomponent word = null;
/** Document Object * *
Private Dispatch documents = NULL;
/** Selection object is a more important object.
Private Dispatch vselection = null;
/** a Word document * *
Private Dispatch wordfile = null;
1, initialize
Word = new Activexcomponent ("Word.Application");
Documents = Word.getproperty ("Documents"). Todispatch ();
(It's easier 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)/is read-only
}, new Int[1]). Todispatch ();
Vselection = Word.getproperty ("Selection"). Todispatch ();
In Word, when the selection is converted, it does not have to be fetched back and forth like a Java object, and the object is always valid.
3, Show word
Word.setproperty ("Visible", New Variant (Visible));
4, set the location of Word
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 and Word, it is often used to make some special marks on word, using Find and Replace methods, this method is not too good.
I think the mode of using hyperlinks is more convenient.
There are several great advantages:
1, Hyperlink has 3 areas for developers to use
ActiveDocument.Hyperlinks.Add
Anchor:=Selection.Range,
address:= "Location",//Address (available) has a disadvantage
Subaddress:= "",//Child position (available)
Screentip:= "",//ScreenTip
texttodisplay:= "Show content"/best use of things
Personal advice to use TextToDisplay.
The address is replaced with an absolute path at save time.
For example, you input a
"AA. Bb. CC "
May be replaced when saved
C:/Documents and Settings/administrator/my Documents/aa. Bb. Cc
2, can be automatic positioning
Use hyperlinks to get all the hyperlinks in the article.
You can also get a hyperlink to a specified range.
3, can be freely typesetting
4, you can copy and paste
To add a 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}"),//Suggested 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 font, color
Dispatch font = Dispatch.get (vselection, "Font"). Todispatch ();
Dispatch.put (font, "underline", new Variant (0));
Dispatch.put (font, "Color", New Variant (0));
Cancel Selection
Dispatch.call (vselection, "MoveRight", new variant (1), new variant (1));
Over-chain replacement content:
1, get all the hyperlinks
Select Object
Dispatch.call (Dobject, "select");
Get a collection of hyperlinks
Dispatch Hyperlinks = Dispatch.get (vselection, "Hyperlinks"). Todispatch ();
How many hyperlinks do we get?
int nhyperlink = Dispatch.get (Hyperlinks, "Count"). ToInt ();
Get a hyperlink
Dispatch Hyperlink=dispatch.invoke (Hyperlinks, "item",
Dispatch.method, new object[] {new Integer (i + 1)},
New Int[1]). Todispatch ());
2, replace the content
Dispatch.put (hyperlink, "TextToDisplay", information);
3, cancel the hyperlink and turn the hyperlink into plain text.
Dispatch.call (hyperlink, "delete");
How to realize the automatic expansion of batch data, it is recommended to use the form of automatic expansion, convenient and simple.
Combine the techniques of using the above hyperlink. will be very simple:
For example, the following data are available:
Dataa
Datab
1, List all forms
Basically the same as listing all the 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)//Handling table forms
}
}
2, the table can control the object
Dispatch drows = Dispatch.get (dtable, "Rows"). Todispatch ();//All Rows
int nrows = Dispatch.get (drows, "Count"). ToInt ();
3, get one line of content
Dispatch Drow =
Dispatch
. Invoke (
Rows
"Item",
Dispatch.method,
New object[] {new Integer (row + 1)},
New Int[1])
. Todispatch ();
return drow;
}catch (comfailexception CFE)
{
/** with merged rows.
return null;
}
4, get a line of hyperlinks
Dhyperlink dhinrow[] = listhyperlinks (drow);
5, a copy of a row many 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 and you can ignore the problem, which is actually pasted
}
}
6, to replace the content, read here will not be introduced.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service