One, create a Word template
1. Create a new Word document
2. Set the fields for four parameters respectively
(1) Position the mouse where you want to set the field
(2) Setting the domain name
(3) After setting up as shown
Second, the project
1, introducing Maven dependency
<Dependency> <groupId>Com.aspose</groupId> <Artifactid>Aspose-words</Artifactid> <version>18.5</version> <classifier>Jdk16</classifier> </Dependency>
2. Load the authorization file
Public Static BooleanGetlicense () {Booleanresult =false; Try{InputStream is= Asposetowordtest.class. getClassLoader (). getResourceAsStream ("License-word.xml"); License Aposelic=NewLicense (); Aposelic.setlicense (IS); Result=true; } Catch(Exception e) {e.printstacktrace (); } returnresult; }
3, get the value and insert into the template and generate a new document
Public Static voidMain (string[] args)throwsException {//Verify License if(!Getlicense ()) { return; } //Template WordString template = "E:\\test\\temp.docx"; //Target WordString DestDoc = "E:\\test\\edit.docx"; //defining the Document interfaceDocument doc =NewDocument (template); //text Fieldstring[] Flds =Newstring[]{"Caseissue", "Policename", "Casename", "Casetime"}; String caseissue= "001"; String Policename= "XX police station"; String Casename= "0727 electric car Theft case"; String Casetime= "2018-07-26 12:20:22"; //valueobject[] Vals =NewObject[]{caseissue,policename, Casename, casetime}; //Calling InterfaceDoc.getmailmerge (). Execute (Flds, Vals); Doc.save (DestDoc); System.out.println (Complete); }
4, results
Where a picture of the set field is referenced in http://www.xiaoguo123.com/p/aspose_words_java_template/
Aspose.words Java-based template generates plain text content for Word