Java exports data to Word with freemarker (with multiple images)

Source: Internet
Author: User

First, make Word template

Create a new Word document, set up a variety of formats as needed, and use a borderless table in order to appear neatly.

Save the Word document as an XML file (note that it is not a Word XML document, I ate this guy's big loss)

Then open the XML file with a text editor and replace the text that needs to be dynamically displayed with a variable, such as: ${topicname},

The picture needs 1. Replace a bunch of characters from the w:bindata tag with a variable that will contain the picture characters in the future 2. To prevent errors when generating multiple graphs, the id attribute of the v:shape tag, the src attribute of the v:imagedata tag, and the W of the W:bindata label are added: The name attribute is replaced with a variable, where the variable can be written in a string like an El expression, and an expression such as ${var_index} can be used to get the index of the variable that the current list iterates over.

1 <w:pict><w:bindata w:name= "Wordml://${module_index}_${childmodule_index}.png" >${ Childmodule.src}</w:bindata><v:shape id= "_x0000_s1026_${module_index}_${childmodule_index}" o:spt= "75" alt= "${childmodule.name}" type= "#_x0000_t75" style= "HEIGHT:240PT;WIDTH:300PT;" filled= "F" o:preferrelative= "T" Stroked= "F" coordsize= "21600,21600" ><v:path/><v:fill on= "F" focussize= "0,0"/><v:stroke on= "F" Joinstyle= "miter"/><v:imagedata src= "Wordml://${module_index}_${childmodule_index}.png" o:title= "${ Childmodule.name} "/><o:lock v:ext=" edit "aspectratio=" T "/><w10:wrap type=" None "/><W10: Anchorlock/></v:shape></w:pict>

When adding < #list ></#list > tags, note the position of the label and see which labels are included. Code a lot of quick look preoccupied (tbl harm AH), using a highlighted editor how important!

After the suffix is changed to FTL, put in the project.

Second, bean configuration, I used the most simple configuration of official documents

1 <!--freemarkertemplate -2     <BeanID= "Freemarkerconfig"class= "Org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">3        < Propertyname= "Templateloaderpath">4            <value>classpath:/templates/freemarker/</value>5        </ Property>6      </Bean>

Third, controller organization data

1 Private Freemarkerconfigurer Freemarkerconfig;

This is just some of the core code.

1list<moduleparam> modulelist = Jsonobject.parsearray (Json.get ("Parentlist"). ToString (), Moduleparam.class);2String topicname = Json.get ("Topicname"). toString ();3String TopicID = Json.get ("TopicID"). toString ();4String summarize = Json.get ("Summarize"). toString ();5 6Map<string,object> DataMap =NewHashmap<string,object>();7Datamap.put ("Modulelist", modulelist);8Datamap.put ("Topicname", topicname);9Datamap.put ("Summarizecontent", summarize);
1Configuration Configuration =freemarkerconfig.getconfiguration ();2Configuration.setdefaultencoding ("UTF-8");3Template t=NULL; 4t = configuration.gettemplate ("REPORTTEMPLATE.FTL"); 5File OutFile =NewFile (fileName); 6Writer out =NULL; 7FileOutputStream fos=NULL; 8         Try{9FOS =NewFileOutputStream (OutFile); Tenout =NewBufferedWriter (NewOutputStreamWriter (FOS, "UTF-8")); One t.process (DataMap, out);  A}finally{ -            if(Out! =NULL){ - out.close (); the            } -            if(Fos! =NULL){ - fos.close (); -            } +}

Basically that's all.

Reference:

Java export word http://blog.csdn.net/wangqiuyun/article/details/26348819 with Freemarker

Java multiple ways to generate DOC documents dynamically: http://www.cnblogs.com/Joanna-Yan/p/5280272.html

Recommendation: Freemarker Series

Java exports data to Word with freemarker (with multiple images)

Related Article

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.