Java +EASYUI+SPRINGMVC Implementation excle Import and export (bottom)

Source: Internet
Author: User

Preface

in the previous article, we introduced the need to implement excle The import does some configuration and Excel imported front-end Easyui writing and backstage of code Controller the specific writing, this we we mainly to learn excle the implementation of the export and Excelutil class is written.

The Chase

Excel Export is based on the foreground conditions to the parameters of the Controller , according to the parameters to the database query, query out List collection, calling Excelutil Tool class that will List set into excle data, output to the browser.

Export Implementation

First, we first look at the foreground code, the foreground to get parameters, the parameters to the Controller Medium:

Export the question bank function Todownloadexcel () {   //Get the question question  var id= $ (' #questionType '). ComboBox (' GetValue ')  var Questiontypes=encodeuri (ID);     Get the course  var coursetypeid =$ (' #courseTypeId '). ComboBox (' GetValue ')  var coursetype=encodeuri (Coursetypeid);    if (questiontypes! = "") {   document.getElementById ("Downloadexcel"). href = "${pagecontext.request.contextpath}/ Leadtoquestiontypes/leadtoexcelquestionbank?questiontype= "+questiontypes+" &coursetype= "+courseType;          $.messager.alert (' hint ', ' Operation succeeded! ', ' info ');    } else{            $.messager.alert (' hint ', ' Please select course types! ', ' info ');   

BackstageControllerget the parameters from the foreground, go to the database query according to the parametersListcollection that willListThe collection is transformed by a tool class,excleOutput to Browser

/** * Export the Excel question bank * * @param request Requests * * @param resposne response * * @throws UNSUPPORTEDENCODINGEXCE Ption Code Exception * */@RequestMapping ("/leadtoexcelquestionbank") public void Leadtoexcelquestionbank (httpservletreq Uest request,httpservletresponse response) throws Unsupportedencodingexception {try {//Get reception questions and lessons string Questiontype = Request.getparameter ("Questiontype"). Trim (); String Coursetype = Request.getparameter ("Coursetype"). Trim (); String Questiontypenameid = new String (Questiontype.getbytes ("iso-8859-1"), "Utf-8"); String Coursetypeid = new String (Coursetype.getbytes ("iso-8859-1"), "Utf-8");//Excel table header,maplinkedhashmap< String, string> fieldMap = Leadtoinquestiontypesmanagebean.getmapleadtoexcelquestionbank (Questiontypenameid);// Excel sheetnamestring SheetName = "question bank";//Excel to export the data list List = Leadtoinquestiontypesmanagebean.leadtoexcelquestionbank (Questiontypenameid, Coursetypeid);//export if (list = = NULL | | List.size () = = 0) {System.out.println ("The question Bank is empty");} else {//Convert list collection to Excleexcelutil.listtoexcel (list, FIELDMAP, sheetname, response); SYSTEM.OUT.PRINTLN ("Export Success ~ ~ ~ ~");}} catch (Excelexception e) {e.printstacktrace ();}}

whichListthe collection is transformed intoexcleparameters of the timeFieldMap, which is the exported database field andexcleChinese table header in theMap, the method is as follows:

/** * Get Export excle when the English map * * @return return the properties of the question map */public linkedhashmap<string, string> Getleadtofiledpublicquestionbank () {linkedhashmap<string, string> superclassmap = new LinkedHashMap<String, String> (), Superclassmap.put ("Stemcontent", "dry Content"), Superclassmap.put ("difficulty", "difficulty level"); Superclassmap.put ( "Scorecoefficient", "score Factor"), Superclassmap.put ("chapter", "chapter"), Superclassmap.put ("Availability", "whether to use"), return Superclassmap;}

in the previous article,excleData ConversionListcollection, you also use aMap, theMapwhenexclethe Chinese header and database fieldsMapcollection, with the following methods:

/** * When importing excle, the Chinese and English map * * @return Return the properties of type class map */public linkedhashmap<string, string> Getleadinfiledpublicquestionbank () {//Excel header corresponds to text linkedhashmap<string, string> map = new linkedhashmap< String, String> (), Map.put ("Dry content", "stemcontent"), Map.put ("difficulty level", "difficulty"), Map.put ("Score factor", " Scorecoefficient "), Map.put (" chapter "," chapter "), Map.put (" Whether to use "," availability "); return map; }


excelutil Tool class

Before introducing the excelutil Tool class, we should first understand the next JXL,JXL is an open source Java Excel API project, through Jxl,java can easily manipulate Excel documents. Our implementation of the excle Import and export is the use of JXL, of course, in addition to JXL, Apache a POI can also be manipulated excle , but compared to the two, JXL simple and easy to use, the POI is slightly more complex. The disadvantage of JXL is that the current JXL can not operate more than Excle2000 version.

This time we used the JXL is a 2.6.12 version of, in maven of the Pom.xml files are added JXL 's coordinate dependencies:

<dependency><groupid>net.sourceforge.jexcelapi</groupid><artifactid>jxl</artifactid ><version>2.6.12</version></dependency>

and we use theExcelutilTool class, the function that can be completed is toListtranslates toexcleoutput to the browser, and you can also complete theexcletranslates toListcollection, you can also setexclecolumn widths, and so on.

About Excelutil Class Code is more, interested students can download by themselves.

Summary

excle Import and export is our common problem in the development, through these two articles, we use JXL simple to complete the excle operation, the implementation of the excle Import and Export.


Java +EASYUI+SPRINGMVC Implementation excle Import and export (bottom)

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.