To merge several Word files into a single word file, use the note point:
1. The jar package jacob-1.9, which is attached to the project,
2. And in Jacob's application, the Jacob.dll in the attachment needs to be placed under the windows/system32.
Syntax Introduction:
Divide a code about Jacob into the following steps:
1) activexcomponent ax = new Activexcomponent ("A1");//Building An instance of an ActiveX component
Where the value of A1 is related to the ActiveX control you need to invoke
MS Control name |
The value of the A1 |
InternetExplorer |
Internetexplorer.application |
Excel |
Excel.Application |
Word |
Word.Application |
Powerpoint |
PowerPoint.Application |
Vb/java Script |
ScriptControl |
Windows Media Player |
WMPlayer.OCX |
Outlook |
Outlook.Application |
Visio |
Visio.Application |
DAO |
DAO. privatedbengine.35 |
Multiface |
Multiface.face |
2) Dispatch ds = Ax.getobject (). Todispatch ();//Get dispatch objects, we can think of each dispatch object as an action on an ActiveX control, which is to gain control of the ActiveX control.
(Note: An analysis of the variant is mentioned in Jacob, where the Ax.getobject () is the object, and we convert it to any object (type))
3) Dispatch DS1 = Dispatch.get (ds, "A2"). Todispatch ();//Gets the A2 property in the ActiveX object's data structure
4) Dispatch D2 = Dispatch.invoke (DS1, "A3", A4, A5, A6). Todispatch ();//function call, DS1 property for ActiveX object A3 execute A4 (dispatch.put\ Dispatch.get and so on), the value of A3 after execution is a5,a6 as the error parameter code is often defined as new int[1], (note: The call, get, and put methods are implemented by this method)
5) Dispatch ds2 = Dispatch.put (ds, "A7", "A8"). Todispatch ();//sets the value of the property A7 of the ActiveX object DS to A8, which returns the same type as get
6) Dispatch DS3 = Dispatch.call (DS1, "A9", A10);//The method is very similar to the Get method, which assigns the A9 attribute to A10
For a specific example, "merge multiple word into one Word document":
1 "First put the package jacob-1.9 in the Lib,build path into the project
2 "Put Jacob.dll under the C:\Windows\System32
1 Packageaaatest;2 3 Importjava.util.ArrayList;4 Importjava.util.List;5 6 Importcom.jacob.activeX.ActiveXComponent;7 ImportCom.jacob.com.Dispatch;8 Importcom.jacob.com.Variant;9 Ten Public classWordtest { One A Public Static voidMain (string[] args) { -List List =NewArrayList (); -String file1= "D:\\2.doc"; theString file2= "D:\\1.doc"; - //String file3= "D:\\2.docx"; - List.add (file1); - List.add (file2); + //List.add (file3); -Unitedoc (list, "D:\\file.doc"); + } A Public Static voidUnitedoc (List fileList, String savepaths) { at if(filelist.size () = = 0 | | fileList = =NULL) { - return; - } - //Open Word -Activexcomponent app =NewActivexcomponent ("Word.Application");//Start Word - Try { in - //set Word not visible---that is, set a property of the Activexcomponent object toApp.setproperty ("Visible",NewVariant (false)); + //gets----Variant of the Documents object. Todispatch () -Object docs = app.getproperty ("Documents"). Todispatch (); the //Open the first file * //function call, perform dispatch.method (Dispatch.put\dispatch.get, etc.) operations on the "open" property of the ActiveX object docs, and the value of "open" after execution is parameter 4, Parameter 5 is the error parameter code is often defined as new int[1], (note: The call, get, and put methods are implemented by this method) $Object doc =DispatchPanax Notoginseng . Invoke ( -(Dispatch) Docs,//Call Target the"Open", + Dispatch.method, A NewObject[] {(String) filelist.get (0),NewVariant (false),NewVariant (true) },//Parameter 4 the New int[3]). Todispatch ();//parameter 5 + //Append file - //The same as above, but another popular explanation: Parameter 1 is the Invoke method of the subject operand, parameter 2 is the name of the method to execute the parameter 1, the parameter 3 is the method of the parameter 2 is the specific operation, the parameter 4 is a series of parameters, parameter 5 is the error parameter code is often defined as new INT[1] $ for(inti = 1; I < filelist.size (); i++) { $ Dispatch.invoke ( -App.getproperty ("Selection"). Todispatch (),//parameter 1 -"InsertFile",//Parameter 2 theDispatch.method,//Parameter 3 - NewObject[] {(String) filelist.get (i), "",NewVariant (false),NewVariant (false),NewVariant (false) },//Parameter 4Wuyi New int[3]);//parameter 5 the } - //Save a new Word file Wu Dispatch.invoke ( - (Dispatch) doc, About"SaveAs", $ Dispatch.method, - NewObject[] {savepaths,NewVariant (1) }, - New int[3]); -Variant f =NewVariant (false); ADispatch.call ((Dispatch) doc, "Close", f);//This method is very similar to the Get method, where he assigns the "Close" attribute to the F +}Catch(Exception e) { the Throw NewRuntimeException ("Error merging Word file." Cause: "+e); -}finally { $App.invoke ("Quit",Newvariant[] {}); the } the } the}
View Code
"Jacob word" uses Jacob to combine multiple word as a Word file