Java +EasyUI+SpringMvc實現Excle匯入匯出(下),easyuispringmvc

來源:互聯網
上載者:User

Java +EasyUI+SpringMvc實現Excle匯入匯出(下),easyuispringmvc

前言

 

    接上篇,在上篇文章我們介紹了要實現Excle匯入做的一些配置和Excel匯入的前端EasyUI代碼的書寫和後台controller的具體書寫,這篇我們我們主要來學習Excle匯出的實現和ExcelUtil類的編寫。

 

正題

 

    Excel匯出就是根據前台條件將參數傳到controller,根據參數去資料庫中進行查詢,查詢出list集合,調用ExcelUtil工具類,將list集合轉為成excle資料,輸出到瀏覽器。

 

匯出實現

 

    首先我們先來看下前台代碼,前台擷取參數,將參數傳到對於的controller中:

//匯出題庫function toDownLoadExcel(){   //擷取題型  var id= $('#questionType').combobox('getValue')  var questionTypes=encodeURI(id);     //擷取課程  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('提示','操作成功!','info');    }else{            $.messager.alert('提示','請選擇課程題型!','info');   }} 

    後台controller擷取前台傳來的參數,根據參數去資料庫查詢list集合,將list集合通過工具類進行轉化,將Excle輸出到瀏覽器

/** * 匯出excel題型題庫 * * @param request 請求 *             * @param resposne 響應 *             * @throws UnsupportedEncodingException   編碼異常 *             */@RequestMapping("/leadToExcelQuestionBank")public void leadToExcelQuestionBank(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {try {// 擷取前台傳來的題型和課程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表格的表頭,mapLinkedHashMap<String, String> fieldMap = leadToInQuestionTypesManageBean.getMapLeadToExcelQuestionBank(questionTypeNameId);// excel的sheetNameString sheetName = "題庫";// excel要匯出的資料List list = leadToInQuestionTypesManageBean.leadToExcelQuestionBank(questionTypeNameId, courseTypeId);// 匯出if (list == null || list.size() == 0) {System.out.println("題庫為空白");}else {//將list集合轉化為excleExcelUtil.listToExcel(list, fieldMap, sheetName, response);System.out.println("匯出成功~~~~");}} catch (ExcelException e) {e.printStackTrace();}}

    其中list集合轉化為excle時候的參數fieldMap,是匯出的資料庫欄位和excle中的中文表頭map,方法如下:

/** * 得到匯出Excle時題型的英中文map * * @return 返回題型的屬性map */public LinkedHashMap<String, String> getLeadToFiledPublicQuestionBank() {LinkedHashMap<String, String> superClassMap = new LinkedHashMap<String, String>();superClassMap.put("stemContent", "題幹內容");superClassMap.put("difficulty", "難度等級");superClassMap.put("scoreCoefficient", "分值係數");superClassMap.put("chapter", "章節");superClassMap.put("availability", "是否使用");return superClassMap;}

    在上一篇文章中的excle資料轉為list集合時,也用到了一個map,那個map時excle中文表頭和資料庫欄位的map集合,方法如下:

/** * 得到匯入Excle時,題型類的中英文map * * @return 返回題型類的屬性map */public LinkedHashMap<String, String> getLeadInFiledPublicQuestionBank() {// excel的表頭與文字對應LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();map.put("題幹內容", "stemContent");map.put("難度等級", "difficulty");map.put("分值係數", "scoreCoefficient");map.put("章節", "chapter");map.put("是否使用", "availability");return map; }


ExcelUtil工具類

 

    在介紹ExcelUtil工具類之前,我們應該先瞭解下Jxl,Jxl是一個開源的Java Excel API項目,通過Jxl,Java可以很方便的操作Excel文檔。我們實現的Excle匯入匯出就是利用了Jxl,當然了除了Jxl,Apache的一個POI也可以操縱Excle,只不過兩者相比,Jxl使用簡單比較容易上手,POI稍微複雜點。而Jxl的缺點就是目前Jxl還不能操作Excle2000以上的版本。

 

    我們本次使用的Jxl是2.6.12版本的,在maven的pom.xml檔案中添加Jxl的座標依賴:

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

    而我們使用的ExcelUtil工具類,可以完成的功能是將list轉化為Excle輸出到瀏覽器,同時也可以完成將Excle轉化為list集合,還可以設定excle列寬等。

 

    關於ExcelUtil類代碼比較多,感興趣的同學可以自行下載。

 

 

小結

 

    Excle的匯入和匯出是我們在開發中經常遇到的問題,通過這兩篇文章,我們就利用Jxl簡單完成了excle的操作,實現了excle的匯入和匯出。

 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.