Freemarker Generating Word,excel Documents

Source: Internet
Author: User

Previously exported documents have been using POI technology, this project using Freemarker technology, and then looked at, found more simple than the POI. So it was published.

Freemarker is aTemplate engine: A template-based and data to be changed, and used to generate output text (Htmlpages,Email,Configuration file,Sourceand other common tools. It is not intended for end-users, but a Java class library, a component that programmers can embed into the products they develop.

First First , save the Word document you want to export as an XML format, and then use Notepad to open it, replacing the dynamically generated code with an El expression (Jstl tag).

Examples are as follows:

Word Document

Name: AAA

Gender: BBB

Save as XML after opening, modify the following

<w:t> Name: ${NAME}</W:T></W:R></W:P><W:P><W:PPR><W:JC w:val= "left"/>< W:rpr><w:rfonts w:ascii= "Microsoft Ya Black" w:h-ansi= "Microsoft Ya Black" w:fareast= "Microsoft Ya Black" w:cs= "Microsoft Ya Black" w:hint= "Fareast"/><w:sz W: Val= "/><w:sz-cs w:val="/><w:lang w:val= "en-US" w:fareast= "ZH-CN"/></w:rpr></w:ppr ><w:r><w:rpr><w:rfonts w:ascii= "Microsoft Ya Black" w:h-ansi= "Microsoft Ya Black" w:fareast= "Microsoft Ya Black" w:cs= "Microsoft Ya Black" w:hint= " Fareast "/><w:sz w:val=" "/><w:sz-cs w:val="/><w:lang w:val= "en-US" w:fareast= "ZH-CN"/> </w:rPr><w:t> Gender:${sex}</w:t>



package com.freemarkes.word;import java.io.ioexception;import java.io.writer;import  java.util.map;import org.apache.commons.logging.log;import org.apache.commons.logging.logfactory; import freemarker.template.configuration;import freemarker.template.template;import  freemarker.template.templateexception;public class wordhandler {       private configuration configuration = null;      log  logger = logfactory.getlog (Wordhandler.class);         Public wordhandler ()  {          configuration  = new configuration ();           Configuration.setdefaultencoding ("UTF-8");       }     / *      * configUration is relative to the file path.       */     private template gettemplate ( String templatepath, string templatename)  throws IOException {          configuration.setclassfortemplateloading (This.getClass (),  TemplatePath);         /***          * public void setclassfortemplateloading (class clazz, string  Pathprefix);  * public void setdirectoryfortemplateloading (File dir)  throws  Ioexception; * public void setservletcontextfortemplateloading (Object servletContext,  string path);   * See the name, respectively, based on the classpath, file system, and Servlet context.           ***/          template t =  Null;         t = configuration.gettemplate ( templatename);          t.setencoding ("UTF-8");          return t;     }            public void write (string templatepath, string  templatename, map datamap, writer out)  throwsException {          try {              template t = gettemplate (Templatepath, templatename);              t.process (datamap, out);          } catch  (exception e)  {            &nBsp; logger.error (e);         } finally{          out.close ();          }      }public static void main (String[] args)  throws  Exception {  map map=getmap ();  wordhandler handler = new  Wordhandler ();   writer out = new outputstreamwriter (New FileOutputStream ("D : \\chaoslee.doc "), " UTF-8 ")   handler.write (",  "Chaoslee.xml",  map, out);} Public static map getmap () {map map = new hashmap (); Map.put ("Name",  " Chaoslee "); Map.put (" Sex ", " male "); return map;}  }


You can export a Word document by running the Main method. code See attachment

My XML document is a copy of the fragment, so may not be used, if there is a use of people, or save themselves as a bit.

This blog can not copy pictures so I am very embarrassed ah, or I will not copy. The copy is blank.

The Excel method is similar.

This article is from "Walking Taihang Yan Zhao" blog, please be sure to keep this source http://chaoslee.blog.51cto.com/10643967/1851642

Freemarker Generating Word,excel 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.