Go: Java generates a Word document with a two-dimensional code picture

Source: Internet
Author: User
Tags base64

(a) function description : in the system of the intelligent terminal, the information of all the learners is to generate a Word document with a two-dimensional code picture of the registration certificate and the admission ticket.

(ii) Implementation process:

1. Create a Word template for the registration certificate and admission ticket, and populate the template with the appropriate variables.

                  2. The template has a picture, so in the production template should first put a picture in the appropriate location, adjust the size, play the role of the positioning                    1. Create a Word template for the registration certificate and the admission ticket, and populate the template with the appropriate variables.                   2. There is a picture in the template, so in the production template should first put a picture in the appropriate location, adjust the size, play to                                      3. Save the created template as an. xml format file                   3. At this point in the. xml format of the file at the location of the picture will appear base64-bit image code, delete this code, replace the corresponding variable, save                    4. Create a TEMPLATE.FTL in the project and paste the code from the XML into the past.                   4. Freemarker-2.3.13.jar Rack Package       &NBS required in this function P         &NBSP 5. Write the background code.                   6. The template style looks like this: Word template style         (c) Background main code implementation process:1. Check the registration certificate and the admission ticket on the information to be displayed, the query image calls tool class Handlimage inside the Getimagestr method, the image into Base64 code, Put the queried data into the list, and here's some code:
List list= This. Documentservice.printproofbymid ();//get information about the registration card and admission ticket in the list  for(intI=0;i<list.size (); i++) {//gets the path to the picture in the list by traversing the listUser u=(User) list.get (i); User UU=NewUser (); FilePath=request.getrealpath ("image2/" + u.getbak1 () + "_" + u.getname () + ". png");//get the path to a pictureUu.setname (U.getname ()); ..........//Other properties are alsoUu.setimage (Handlimage.getimagestr (FilePath));//call the Getimagestr method in Handlimage to convert the image to Base64 codeAddlist.add (UU);//to add an object to the Addlist}map<String,Object> map=NewHashmap<string,object>(); if(Addlist.size () >0) {//a non-null judgment on the addlistMap.put ("TED", addlist);//put the addlist in the mapDocexportutils.downloaddoc (REQUEST,MAP);//Call the Downloaddoc method under Docexportutils to generate the Word document}
2. The following method is the way to generate a Word document, the following is all the code for this method:
ImportJava.io.BufferedWriter;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.OutputStreamWriter;Importjava.io.UnsupportedEncodingException;ImportJava.io.Writer;ImportJava.util.Map;Importjavax.servlet.http.HttpServletRequest;Importfreemarker.template.Configuration;Importfreemarker.template.Template;Importfreemarker.template.TemplateException; Public classDocexportutils {PrivateConfiguration Configuration =NULL;  Publicdocexportutils () {Configuration=NewConfiguration (); Configuration.setdefaultencoding ("Utf-8"); }  Public Static voidDownloaddoc (httpservletrequest request,map Map) {Configuration Configuration=NewConfiguration (); Configuration.setdefaultencoding ("Utf-8"); Configuration.setclassfortemplateloading (docexportutils.class, "/com/ecoinfo/terminal/template");//The Red section holds the path to the Freemarker templateTemplate t=NULL; Try{T= Configuration.gettemplate ("TEMPLATE.FTL");// //TEMPLATE.FTL for the template to be loaded}Catch(IOException e) {e.printstacktrace (); } String name= (String) map.get ("title");//build the name of a Word documentFile OutFile =NewFile (Request.getrealpath ("word/" + name + ". Doc"));//Output Document PathWriter out =NULL; Try{ out=NewBufferedWriter (NewOutputStreamWriter (NewFileOutputStream (OutFile), "UTF-8")); } Catch(FileNotFoundException E1) {e1.printstacktrace (); } Catch(unsupportedencodingexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }     Try{t.process (map, out); } Catch(templateexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } }}

(iv) tool methods used in generating word:Description: There is a picture in the template, so to convert the picture to Base64 code, as long as the picture stored in the path to Getimagestr, by the following method can generate base64 code of the picture code, the following is the Base64 code of the tool method of all the code:
/*** Processing Picture * Handlimage class, used to convert the image to Base64 code*/ ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException;ImportJava.io.InputStream;ImportCom.ecoinfo.commons.security.BASE64Encoder; Public classhandlimage{ Public StaticString Getimagestr (string filepath) {//filepath the path where the picture is storedInputStream in=NULL; Try{ in=NewFileInputStream (filepath); } Catch(FileNotFoundException E1) {//TODO auto-generated Catch blockE1.printstacktrace (); } if(!"". Equals (in) &&in!=NULL){  byte[]data=NULL; Try{Data=New byte[In.available ()];   In.read (data);   In.close (); }Catch(IOException e) {e.printstacktrace ();} Base64encoder Encoder=NewBase64encoder (); returnEncoder.encode (data);}Else{  return NULL; }}

(v) Precautions:1. In the template, for the loop of the list of pictures, replace the code for the position of the looping picture with the form of a variable, otherwise, the picture that appears in the Word document is the first picture, which is modified as follows: How to modify a picture
2. There are some special characters in Freemarker that need to be escaped before they can be implemented, and the method code that needs to escape characters is listed below:< #if Ted. Name?? >${(Ted. Name?replace ("&", "&amp;"))? Replace ("<", "&lt;"))? Replace (">", "&gt;")} </#if >

Go: Java generates a Word document with a two-dimensional code picture

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.