Java + EasyUI + SpringMvc implements Excle Import and Export (below), easyuispringmvc

Source: Internet
Author: User

Java + EasyUI + SpringMvc implements Excle Import and Export (below), easyuispringmvc

Preface

 

Next, in the previous article, we will introduce some configuration for Excle import and the front-end EasyUI code writing for Excel import and the specific Writing of the background controller, in this article, we will mainly learn the implementation of Excle export and the compilation of ExcelUtil classes.

 

Question

 

Excel export transfers the parameters to the controller Based on the foreground conditions, queries the list set in the database based on the parameters, queries the list set, calls the ExcelUtil tool class, converts the list set to excle data, and outputs the data to the browser.

 

Export implementation

 

First, let's take a look at the front-end code. The front-end gets the parameters and transmits the parameters to the controller:

// Export the question library function toDownLoadExcel () {// obtain the question type var id =$ ('# questionType '). combobox ('getvalue') var questionTypes = encodeURI (id); // obtain 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 ('hprompt ',' operation successful! ', 'Info');} else {$. messager. alert ('hprompt', 'select the course question type! ', 'Info ');}}

The background controller obtains the parameters sent from the foreground, queries the list set in the database based on the parameters, converts the list set through the tool class, and outputs Excle to the browser.

/*** Export the excel question Library ** @ param request ** @ param resposne response ** @ throws UnsupportedEncodingException encoding exception **/@ RequestMapping ("/leadToExcelQuestionBank ") public void leadToExcelQuestionBank (HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {try {// obtain Question Types and course String questionType = request. getParameter ("questionType "). trim (); String courseType = request. g EtParameter ("courseType "). trim (); String questionTypeNameId = new String (questionType. getBytes ("iso-8859-1"), "UTF-8"); String courseTypeId = new String (courseType. getBytes ("iso-8859-1"), "UTF-8"); // header of the excel table, mapLinkedHashMap <String, String> fieldMap = leadToInQuestionTypesManageBean. getMapLeadToExcelQuestionBank (questionTypeNameId); // sheetNameString sheetName = "Question Bank" in excel; // List of data to be exported in excel = LeadToInQuestionTypesManageBean. leadToExcelQuestionBank (questionTypeNameId, courseTypeId); // export if (list = null | list. size () = 0) {System. out. println ("the question bank is blank");} else {// converts the list set to excleExcelUtil. listToExcel (list, fieldMap, sheetName, response); System. out. println ("exported successfully ~~~~ ") ;}} Catch (ExcelException e) {e. printStackTrace ();}}

The fieldMap parameter when the list set is converted to excle is the exported database field and the map of the Chinese header in excle. The method is as follows:

/*** Obtain the English map of the question type when exporting Excle ** @ return returns the question type attribute map */public LinkedHashMap <String, String> getLeadToFiledPublicQuestionBank () {LinkedHashMap <String, string> superClassMap = new LinkedHashMap <String, String> (); superClassMap. put ("stemContent", "stem content"); superClassMap. put ("difficulty", "difficulty level"); superClassMap. put ("scoreCoefficient", "score coefficient"); superClassMap. put ("chapter", "chapter"); superClassMap. put ("availability", "use"); return superClassMap ;}

When the excle data in the previous article is converted to a list set, a map is also used. The map set of the excle Chinese header and database fields is used in that map. The method is as follows:

/*** Get the import Excle. map of the question type in both Chinese and English ** @ return returns the question type attribute map */public LinkedHashMap <String, String> getLeadInFiledPublicQuestionBank () {// LinkedHashMap <String, String> map = new LinkedHashMap <String, String> (); map. put ("stem content", "stemContent"); map. put ("difficulty level", "difficulty"); map. put ("score coefficient", "scoreCoefficient"); map. put ("chapter", "chapter"); map. put ("use", "availability"); return map ;}


ExcelUtil Tool

 

Before introducing the ExcelUtil tool class, we should first understand Jxl. Jxl is an open-source Java Excel API project. Through Jxl, Java can easily operate Excel documents. The Excle Import and Export we implement is to use Jxl. Of course, apart from Jxl, a POI of Apache can also manipulate Excle. However, compared with Jxl, Jxl is easier to use and POI is a little complicated. The disadvantage of Jxl is that Jxl cannot operate Excle2000 or later versions.

 

The Jxl version we used this time is 2.6.12. In the maven pom. xml file, add the coordinate dependency of Jxl:

<dependency><groupId>net.sourceforge.jexcelapi</groupId><artifactId>jxl</artifactId><version>2.6.12</version></dependency>

The ExcelUtil tool class can be used to convert the list into Excle and output to the browser. It can also convert Excle into a list set and set the excle column width.

 

There are many ExcelUtil-type codes. If you are interested, you can download them by yourself.

 

 

Summary

 

The import and export of Excle is a problem we often encounter during development. Through these two articles, we use Jxl to simply complete the excle operation and implement the import and export of excle.

 


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.