Want to manipulate Word files in Java? Jacob is a good choice, the Java-com bridge, which you can download in http://sourceforge.net/projects/jacob-project/, I downloaded the version is 1.12, note that the version is too low may be an error.
If no special requirements are available, the Jacob.jar and Jacob.dll provided in Jacob_*.zip can be used directly. Put the Jacob.dll file in the system can find the path, generally put c:/windows/system32 under the line, pay attention to your Jacob.dll file and your Jacob.jar package to match, otherwise it will be an error.
If you want to compile it is also very simple, the jacob_*_src.zip untie, build a project, in the Build.xml can be slightly configured:
<property name= "JDK" value= "d:/java/j2sdk1.4.2_13"/>
<property name= "MSDevDir" value= "D:/microsoft Visual studio/vc98"/>
<property name= "version" value= "1.12"/>
See, you need to have JDK and VC environment on your machine, VC is used to generate Jacob.dll files, if the compile said to find MSPDB60.DLL, then in your Microsoft Visual Studio directory Search, copy to d:/ Microsoft Visual Studio/vc98/bin is on the line.
If you need to rename the jar bag in Jacob (although this is not usually the case, but if you need two versions of Jacob to use at the same time, renaming may be a choice), then you work a little more:
(1) Package renaming is necessary, for example, we will change the Com.jacob.activeX under SRC to Com.test.jacob.activeX, To change the com.jacob.com to com.test.jacob.com, only these two packages are useful when packing, so just change them is enough.
(2) Then modify the value of Src.java.jacob.mainpackage in Build.xml to Com.test.jacob and modify the value of Java.class.main to Com.test.jacob.com.Jacob.
(3) Do not forget the javajarbin in the packaging of the source path to be changed, <include name= "Com/**/*.class"/> to <include name= "Com/test/**/*.class"/>.
(4) Build.xml the generated DLL and jar package, for example, we change the two files to Jacob_test.dll and Jacob_test.jar. Modify the value of Enerated.filename.dll and Generated.filename.jar in Build.xml for your newly renamed name.
(5) in Com.test.jacob.com.LibraryLoader, System.loadlibrary ("Jacob"), changed to System.loadlibrary ("Jacob_test");
(6) In addition, it is important to change the com_jacob_com of *.cpp and *.h in JNI to Com_test_jacob_com,com/jacob/com to com/test/jacob/com.
(7) Ant compiles, compiles the good file in the release directory.
(8) Finally put the compiled Jacob_test.dll file in Windows/system32 under the success of the.
Now it's time to use the Jacob.jar, if you have modified the jar package name, with the new jar package, such as Jacob_test.jar, here the unification is called Jacob.jar.
First of all, the introduction of Jacob.jar package in the Classpath, if the WEB application, Web-inf Lib also to join the Jacob.jar package.
Here's an example:
Class Replaceword.java
Import com.jacob.com.*;
Import com.jacob.activex.*;
public class Replaceword {
public static void Main (string[] args) {
Activexcomponent app = new Activexcomponent ("Word.Application"); Start Word
String inFile = "C://test.doc"; The Word file to replace
try {
App.setproperty ("Visible", new Variant (false)); Set Word not visible
Dispatch docs = app.getproperty ("Documents"). Todispatch ();
Dispatch doc = Dispatch.invoke (docs, "Open", Dispatch.method,new object[] {inFile, new variant (FALSE), new variant (FALSE) }, new Int[1]). Todispatch (); Open the Word file, and note that the third argument here is set to False, which indicates whether it is opened as read-only because we want to save the original file and open it in a writable way.
Dispatch Selection=app.getproperty ("Selection"). Todispatch ()///Get to Selection component
Dispatch.call (Selection, "HomeKey", New Variant (6));/move to the beginning
Dispatch find = Dispatch.call (selection, "get"). Todispatch ();
Dispatch.put (Find, "Text", "name"); Find string "name"
Dispatch.call (Find, "Execute"); Execute Query
Dispatch.put (Selection, "Text", "John"); Replace with "John"
Dispatch.call (Doc, "Save"); Save
Dispatch.call (Doc, "Close", new Variant (false));
catch (Exception e) {
E.printstacktrace ();
finally {
App.invoke ("Quit", new variant[] {});
App.saferelease ();
}
}
}
You might ask, how do I know which arguments to call which method to invoke to operate. Don't forget, word still has macros. Record a macro yourself and edit the macro to see the code. Whichever method you use depends on what you have.
I have summed up:
The components under document are used dispatch Selection=app.getproperty ("Selection"). Todispatch () This method is obtained;
Further down the component needs to call the selection method to obtain, such as Dispatch find = Dispatch.call (selection, "find"). Todispatch ();
If a method requires parameters, Dispatch doc = Dispatch.invoke (docs, "Open", Dispatch.method,new object[] {inFile, new Variant (false), new Variant (False)}, new Int[1]). Todispatch () is an example, this is the call to the docs open method, object[] in the array is the argument it needs;
If you want to modify the properties of a component, use Dispatch.put (Find, "text", "name") in this form, "text" is the property name, and "name" is the value.