Java exporting Excel with Freemarker implementation

Source: Internet
Author: User

A few days ago did JXL export Excel, now do it with Freemarker

Freemarker Exporting Excel and exporting Word steps are the same as the implementation method.

1. Making Excel Templates

2. Change the suffix name to FTL and place it in the corresponding position

3. Implementation methods

1  PackageOrg.lq.ssm.gp.controller;2 3 ImportJava.io.BufferedWriter;4 ImportJava.io.File;5 ImportJava.io.FileOutputStream;6 Importjava.io.IOException;7 ImportJava.io.OutputStreamWriter;8 ImportJava.io.Writer;9 Importjava.util.ArrayList;Ten ImportJava.util.HashMap; One Importjava.util.List; A ImportJava.util.Map; -  - ImportOrg.lq.ssm.entity.LandUser; the Importorg.springframework.web.bind.annotation.RequestMapping; -  - Importfreemarker.template.Configuration; - Importfreemarker.template.Template; + Importfreemarker.template.TemplateException; - ImportFreemarker.template.TemplateExceptionHandler; +  A  Public classExportexcel { at      -     PrivateConfiguration Configuration =NULL; -      -      PublicExportexcel () { -Configuration =NewConfiguration (); -Configuration.setdefaultencoding ("Utf-8"); in     } -     //@RequestMapping (params= "print") to      Public voidPrint ()throwsIOException { +           -Configuration =NewConfiguration (); theConfiguration.setdefaultencoding ("Utf-8"); *          //to fill in the Modeled data file $Map DataMap =NewHashMap ();Panax Notoginseng         //GetData (DATAMAP); -          theList<map<string, object>>list=NewArraylist<map<string,object>>(); +          for(inti=0;i<10;i++){ AMap<string, object>map=NewHashmap<string, object>(); theMap.put ("UserName", "Zhang San"); +Map.put ("Landname", "Zhang San"); -Map.put ("LANDSCMJ", "111111"); $Map.put ("Landbzdate", "20170626"); $ list.add (map); -         } -Datamap.put ("list", list); the          -         //set the Modeled appliance method and path, Freemarker supports a variety of template loading methods. Can be re-servlet,classpath, database loading,Wuyi           //Here are our templates the         //configuration.setdirectoryfortemplateloading (New File ("g:\\"));  -          Wu          -Configuration.setclassfortemplateloading ( This. GetClass (), "/org/lq/ssm/gp/controller"); About          //Setting the exception handler $ Configuration.settemplateexceptionhandler (templateexceptionhandler.ignore_handler); -Template T =NULL; -         Try { -              //TEST.FTL for the template to be loaded At = configuration.gettemplate ("LAND.FTL"); +T.setencoding ("Utf-8"); the  -}Catch(IOException e) { $ e.printstacktrace (); the         } the         //output document path and name theFile OutFile =NewFile ("G:/ttt/land.xls"); theWriter out =NULL; -          in         Try { theout =NewBufferedWriter (NewOutputStreamWriter (NewFileOutputStream (OutFile), "Utf-8")); the}Catch(Exception E1) { About e1.printstacktrace (); the         } the  the         Try { + t.process (DataMap, out); - out.close (); the}Catch(templateexception e) {Bayi e.printstacktrace (); the}Catch(IOException e) { the e.printstacktrace (); -         } -     } the      the      the      the     /** - * Note that the data stored in the Datamap key value should correspond to the parameters in the template the      * @paramDataMap the      * @throwsIOException the      * 94      */ the  the      Public Static voidMain (string[] args)throwsIOException { the         98Exportexcel exc=NewExportexcel (); About exc.print (); -     }101 102}

The 4.excel form will be fine. However, there may be an error while opening the file.

Modify the template content at this time to find

Change the value a bit, and you can.

Java exporting Excel with Freemarker implementation

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.