Java Freemark Generating Word documents

Source: Internet
Author: User

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

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.