Freemarker Creating a Word Document

Source: Internet
Author: User

To build a Word document using Freemarker steps:

Import jar Package: Freemarker-2.3.20.jar

Create a new Word template, adjust the style,

The Word template is saved as 2003 XML format,

The content that needs to be replaced in XML is replaced with ${param}, param is the parameter name, for example: ${name}, passed in Parameter Map.put ("name", "Zhang San");

Write Java code, replace XML document, export.

1 /**2 *@ file name: Createword.java3 *@ Date: 2016-5-194 * @Copyright:5  */6  PackageCom.billionsfinance.contractmanagement.archives.util;7 8 ImportJava.io.File;9 ImportJava.io.FileInputStream;Ten ImportJava.io.FileOutputStream; One Importjava.io.IOException; A ImportJava.io.InputStream; - ImportJava.io.OutputStreamWriter; - ImportJava.io.Writer; the ImportJava.util.Map; -  - ImportJavax.servlet.ServletOutputStream; - Importjavax.servlet.http.HttpServletRequest; + ImportJavax.servlet.http.HttpServletResponse; -  + Importcom.sun.org.apache.xerces.internal.impl.dv.util.Base64; A  at Importfreemarker.template.Configuration; - Importfreemarker.template.Template; -  - /** - * TODO (generate Word template). -  * in  * @authorXiaofei.xian -  * @versionv 1.0 to * @ClassName: Createword + * @date: May 17, 2016 PM 3:18:10 -  */ the  Public classCreateword { *      $     /**The configuration.*/Panax Notoginseng     PrivateConfiguration Configuration =NULL; -      the     /** + * Instantiates a new createword. A      */ the      PublicCreateword () { +Configuration =NewConfiguration (); -Configuration.setdefaultencoding ("Utf-8"); $     } $      -     /** - * TODO (Generate Word template and download). the      * -      * @authorXiaofei.xianWuyi      * @paramrequest the request the      * @paramResponse The response -      * @paramtemplatename Template Name web-inf\templates\ The following example: CONTRACTCERTIFI.FTL Wu      * @paramwordname Word document name generation -      * @paramDATAMAP Data Binding About      * @throwsException void return value $ * @date: May 17, 2016 PM 3:18:34 - * @Title: CreateFile -      */ -      Public voidCreateFile (httpservletrequest request, httpservletresponse response, AString templatename, String wordname, Map<string, object>DataMap) +         throwsException { theString name = "Temp" + (int) (Math.random () * 100000) + ". Doc"; -File f =NewFile (name); $         //set the freemarker template file location the configuration.setservletcontextfortemplateloading (Request.getsession (). Getservletcontext (), the"Web-inf/templates"); theTemplate T =NULL; the         //the template to be loaded -t =configuration.gettemplate (templatename); inWriter W =NewOutputStreamWriter (NewFileOutputStream (f), "Utf-8"); the t.process (DataMap, W); theInputStream fin =NULL; AboutServletoutputstream out =NULL; the f.setreadonly (); theF.setwritable (false); the         Try { +             //Call the tool class Wordgenerator Createdoc method to generate a Word document -Fin =NewFileInputStream (f); the             BayiResponse.setcharacterencoding ("Utf-8"); theResponse.setcontenttype ("Application/msword"); the             //set the browser to download the way the file is processed by default named Resume.doc -Response.AddHeader ("Content-disposition", -"Attachment;filename=" +NewString (Wordname.getbytes ("gb2312"), "Iso8859-1") the+ ". Doc"); the              theout =Response.getoutputstream (); the             byte[] buffer =New byte[512];//buffers -             intBytestoread =-1; the             //output the contents of a read-in Word file to a browser by looping the              while((Bytestoread = fin.read (buffer))! =-1) { theOut.write (buffer, 0, bytestoread);94             } the         } the         finally { the             if(W! =NULL) {98 w.close (); About             } -             if(Fin! =NULL) {101 fin.close ();102             }103             if(Out! =NULL) {104 out.close (); the             }106             if(F! =NULL) {107F.delete ();//Delete temporary files108             }109         } the     }111      the     /**113 * TODO (Get the image's BASE64 encoding). the      * the      * @authorXiaofei.xian the      * @paramfilename The filename117      * @returnString118      * @throwsIOException String return value119 * @date: May 17, 2016 PM 4:12:19 - * @Title: getimagestring121      */122      Public Staticstring getimagestring (string filename)123         throwsIOException {124InputStream in =NULL; the         byte[] data =NULL;126         Try {127in =Newfileinputstream (filename); -data =New byte[In.available ()];129 in.read (data); the in.close ();131         } the         Catch(IOException e) {133             Throwe;134         }135         finally {136             if(In! =NULL) {137 in.close ();138             }139         } $         returnBase64.encode (data);141     }142}

Freemarker Creating a Word Document

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.