1. Download Freemarker-2.3.19.jar
2. Replace the content you want to fill with ${title},${no}
3. Open with Word, save as 2003xml
4. Open the Generate XML file, see if there is no expression ${title},${no} separate, if separate, then to merge, and finally save as an FTL file
5. Code
Public classdocuitlsdemo{PrivateConfiguration configuration=NULL; PublicDocuitlsdemo () {Configuration=NewConfiguration ();//configuration.setclassiccompatible (false);//Null value processingConfiguration.setdefaultencoding ("UTF-8"); } /*** Generate word *@paramtmplatepath Path to template/com/document/temlpate *@paramtemplatename "TEXT.FTL" *@paramData Map Collection *@paramoutputFile File Output path *@throwsException*/ Public voidCreatedoc (File tmplatepath,string templatename,map<?,? > Data,file outputFile)throwsException {//set the Modeled appliance method and path, Freemarker supports a variety of template loading methods. Can be re-servlet,classpath, database loading,//here Our template is placed under the Com.havenliu.document.template packageconfiguration.setdirectoryfortemplateloading (Tmplatepath); Template T=NULL; T=configuration.gettemplate (templatename); //output document path and nameWriter out =NULL; //Troubleshoot garbled occurrencesout =NewBufferedWriter (NewOutputStreamWriter (NewFileOutputStream (OutputFile), "UTF-8")); T.process (data, out); Out.close (); } Public Static voidMain (string[] args) {Calendar Calendar=calendar.getinstance (); intYear=Calendar.get (calendar.year); intMonth=calendar.get (Calendar.month) +1; intday=Calendar.get (Calendar.day_of_month); Docuitlsdemo Demo=NewDocuitlsdemo (); Map<string, Object>data=NewHashmap<string, object>(); Data.put ("No", "gk001"); Data.put ("Year", year+ "");//the year here is to be represented by a string, otherwise, as a number, there will be a commaData.put ("Month", month); Data.put ("Day", day); Data.put ("title", "2015-1-1"); File Tmplatepath=NewFile ("d://"); String Tempaltename= "TEMPLATE.FTL"; File OutputFile=NewFile (Tmplatepath, "xx contract. Doc"); Try{Demo.createdoc (Tmplatepath, Tempaltename, data, outputFile); } Catch(Exception e) {e.printstacktrace (); } System.out.println ("Build Succeeded"); }}
Java Freemark Generating Word documents