項目開發經驗分享—匯出Excel,項目開發經驗分享
上一篇部落格我們分享了資料上傳經驗《匯入Excel》。今天我們來分享一下資料下載經驗:匯出Excel!
引言
作為一個資訊管理類系統,我們不僅需要錄入很多資料,同時我們也會收集儲存很多資料,並通過條件檢索對得到的資料進行整理和分析,這時候我們就可以匯出這些資料,如所示: 同匯入Excel一樣,很多系統也同時提供了匯出Excle功能,方便把檢索到的有用資料匯出Excel並列印出來,如此人性化的功能,我們在做類似的系統時也應該借鑒進去,給使用者更高的體驗度!下面我們來看看具體的實現吧:
思路
1、添加ExcelUtility工具類(完整版連結)
2、實現匯出Excel
實現
1、添加ExcelUtility工具類
(1)ExcelUtility工具類中匯出Excel代碼:
/** * @MethodName: listToExcel * @Description: 匯出Excel * @param response:使用response可以匯出到瀏覽器 * @param list:資料來源 * @param fieldMap:中英文欄位對應Map * @throws ExcelException */public <T> void listToExcel (HttpServletResponse response, List<T> list ,LinkedHashMap<String,String> fieldMap) throws ExcelException{//設定預設檔案名稱為目前時間:年月日時分秒String fileName=new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()).toString();//設定response頭資訊response.reset(); response.setContentType("application/vnd.ms-excel"); //改成輸出excel檔案 response.setHeader("Content-disposition","attachment; filename="+fileName+".xls" ); //建立活頁簿並發送到瀏覽器 try { OutputStream out=response.getOutputStream(); listToExcel(out, list, fieldMap);} catch (Exception e) {e.printStackTrace();//如果是ExcelException,則直接拋出if(e instanceof ExcelException){throw (ExcelException)e;//否則將其它異常封裝成ExcelException再拋出}else{throw new ExcelException("匯出Excel失敗");}}}
(2)在web的pom檔案裡添加相關依賴:
<dependency> <groupId>com.tgb</groupId> <artifactId>itoo-assess-tool</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
2、實現匯出Excel
(1)前台Jsp代碼
<div id="dlg" class="easyui-dialog" style="width:90%;height:57%; padding: 10px 20px" closed="true" buttons="#dlg-buttons"><form id="fm" method="post" ><div> <div class="bblock" style="text-align:center" ><div style="float:left; width:33%;" class="innoblock"><label id="teaName">教師姓名:</label><span id="dialogTeacher" ></span><span id="teaId" ></span></div><div style="float:left; width:33%;" class="innoblock"><label id="courName">課程名稱:</label><span id="dialogCourse"></span><span id="courId" ></span></div></div></div></form><br><br><table id="detail" class="easyui-datagrid" title="建議詳細資料" style="width: 100% x; height: 255px" charset="UTF-8" toolbar="#detailToolbar" pagination="true" rownumbers="true" fitColumns="true" singleSelect="true" method="post" ><thead> <tr > <th field="suggestionId" hidden="true">建議Id</th> <th field="teacherId" hidden="true">教師Id</th> <th field="courseId" hidden="true">課程Id</th> <th field="suggestion" width="98%" align="center" font-size="30px">學生建議</th> </tr></thead></table><div id="detailToolbar"> <a href="javascript:void(0)" class="easyui-linkbutton"iconCls="icon-edit" plain="true" onclick="printSuggestion()">匯出Excel</a> </div>
(2)前台js代碼
// 匯出Excel方法function printSuggestion(){var teacherId=teaId.innerHTML;var courseId=courId.innerHTML;alert("執行到這裡");//調用匯出Excel方法 $.ajax({type : 'post',url : "exportExcel?teacherId=" + teacherId+"&courseId="+courseId,// dataType : "text",})}(3)後台Controller代碼
/** * 匯出excel * @param request 請求 * @param resposne 響應 * @throws UnsupportedEncodingException 編碼異常 */@RequestMapping("/exportExcel")public void leadToExcelQuestionBank(HttpServletRequest request, HttpServletResponse response)throws UnsupportedEncodingException {//String courseId = request.getParameter("courseId").trim(); //String teacherId = request.getParameter("teacherId").trim(); String courseId = request.getParameter("courseId"); String teacherId = request.getParameter("teacherId"); courseId = "jisuanjiyingyong123"; teacherId = "wangyajin001"; //Excel要匯出的資料List<StudentAssessSuggestion> suggestions=studentAssessSuggestionBean.findSuggestionByIdExcel(teacherId,courseId);LinkedHashMap<String, String> fieldMap=new LinkedHashMap<String, String>();fieldMap.put("teacherName","教師姓名");fieldMap.put("courseName","課程名稱");fieldMap.put("suggestion","學生建議");try {excelUtil=new CommonExcelUtil();excelUtil.listToExcel(response, suggestions, fieldMap);} catch (Exception e) {e.printStackTrace();}}(4)StudentAssessSuggestionBeanImpl代碼:
@SuppressWarnings("unchecked")public List<StudentAssessSuggestion> findSuggestionByIdExcel(String teacherId, String courseId) {//查詢語句的參數mapMap<Serializable,Serializable> map=new HashMap<Serializable,Serializable>();map.put("teacherId", teacherId);map.put("courseId", courseId);String hql=null;hql="From StudentAssessSuggestion where teacherId=:teacherId and courseId=:courseId ";List<StudentAssessSuggestion> studentAssessSuggestion = studentAssessSuggestionEao.queryByHql(hql, map);return studentAssessSuggestion;}
ok,到這裡為止資料下載經驗--匯出Excel已經完成。
總結
從使用者角度出發,以使用者為主,越靠近使用者的使用習慣,軟體親和力越高,開發的軟體越受歡迎!