當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Mvc + Easyui中根据查询结果导出文件
生活随笔
收集整理的這篇文章主要介紹了
Spring Mvc + Easyui中根据查询结果导出文件
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
項目是典型的SpringMvc + Easyui,需求是前臺頁面根據(jù)查詢條件導出生成CSV文件。
?
基本流程是:前臺頁面通過表單提交的方式,請求后臺,后臺接受到查詢參數(shù),根據(jù)查詢參數(shù)查詢到數(shù)據(jù)集合,最后導出生成CSV文件。(用ajax請求后臺時遇到一點問題,回調(diào)函數(shù)一直報錯,有待研究)
1、前臺頁面效果圖:
2、前臺頁面代碼:
1 <div class="easyui-layout" data-options="fit:true"> 2 <div class="easyui-panel" data-options="region:'north'" style="height:40px;"> 3 <form id="formQuery" method="post"> 4 <input type="hidden" id = "serverId" /> 5 <table cellpadding="4"> 6 <tr> 7 <td><spring:message code="篩選時間" />:</td> 8 <td><input class="easyui-datebox" id="countTime" data-options="editable:false,width:140" /></td> 9 <td><a href="javascript:reloadCountData()" class="easyui-linkbutton" iconCls="icon-search"><spring:message code="篩選" /></a></td> 10 <td><a href="javascript:exportData()" class="easyui-linkbutton" iconCls="icon-save"><spring:message code="導出" /></a></td> 11 </tr> 12 </table> 13 </form> 14 </div> 15 <div data-options="region:'center'" style="width: 100%;height: 100%"> 16 <table id="gridOnline" class="easyui-datagrid" data-options="fit:true,pagination:true,rownumbers:true,pageSize:20,loader:loadCountData" > 17 <thead> 18 <tr> 19 <th data-options="field:'id',width:150,hidden:true"><spring:message code="ID" /></th> 20 <th data-options="field:'countTime',width:150"><spring:message code="統(tǒng)計時間" /></th> 21 <th data-options="field:'online',width:150"><spring:message code="在線人數(shù)" /></th> 22 </tr> 23 </thead> 24 </table> 25 </div> 26 </div>熟悉Easyui的人應該能看出來,這是一個常見的layout,north放查詢條件。center放datagrid展示數(shù)據(jù),因為要使用form提交表單的方式請求后臺,所以north中的元素都放在了form中。
3、前臺JS代碼:
1 function exportData(){ 2 var serverId = $("#formQuery").find("#serverId").val(); 3 var countTime = $("#formQuery").find("#countTime").datebox("getValue"); 4 if(countTime!=""){ 5 if(countTime.indexOf("/") > -1){ //英文狀態(tài)下格式 6 var month = (countTime).substring(0,2); 7 var day = (countTime).substring(3,5); 8 var year = (countTime).substring(6,10); 9 countTime = (year+"-"+month+"-"+day)+" 00:00:00"; 10 }else{ 11 countTime+=" 00:00:00"; 12 } 13 } 14 $('#formQuery').form('submit',{ 15 url: '${URI}serverManager/exportOnlineData.htm?serverId='+serverId+"&countTime="+countTime 16 }) 17 }這是點擊導出按鈕調(diào)用的JS方法,其中獲取serverId的值可以忽略不計,具體看需求,而對countTime進行處理的那一段代碼,也可以忽略不計,因為系統(tǒng)實現(xiàn)了國際化,在英文狀態(tài)下日期格式跟中文狀態(tài)下日期格式不一樣,統(tǒng)一處理成yyyy-MM-dd HH:mm:ss格式
真正起作用的代碼是給form加submit事件,可以看到參數(shù)是拼接到url中的。
4、后臺controller方法:
1 @RequestMapping("exportOnlineData") 2 public void exportOnlineData(HttpServletRequest request, HttpServletResponse response,String serverId,String countTime) throws ParseException, IllegalArgumentException, IllegalAccessException, IOException{ 3 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 4 SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss"); 5 ServerManager serverManager = new ServerManager(); 6 serverManager.setServer(serverId); 7 serverManager.setCountTime(sdf.parse(countTime)); 8 List<ServerManager> list = this.gameUserDao.queryOnlineListForExport(serverManager); 9 String[] titles = new String[]{"服務器ID","統(tǒng)計時間","在線人數(shù)"}; 10 String[] propertys = new String[]{"server","countTimeStr","online"}; 11 ExportUtil.exportCsv(titles, propertys, list, sdf2.format(new Date()) + "_服務器在線人數(shù)統(tǒng)計.csv", request, response); 12 }這段代碼,就是拿到頁面?zhèn)鬟f過來的參數(shù),查詢到數(shù)據(jù)集合,之后使用工具類導出。
5、因為導出功能點較多,所以做了簡單封裝如下:
1 /** 2 * 3 * 導出生成csv文件 4 * @author ccg 5 * @param titles 標題頭 6 * @param propertys 每一列標題頭對應數(shù)據(jù)集合里對象的屬性 7 * @param list 數(shù)據(jù)集合 8 * @param fileName 文件名稱注意不能有空格以及冒號 9 * @param request 10 * @param response 11 * @return 12 * @throws IOException 13 * @throws IllegalArgumentException 14 * @throws IllegalAccessException 15 * Created 2017年2月22日 下午8:35:57 16 */ 17 public static<T> String exportCsv(String[] titles,String[] propertys,List<T> list,String fileName,HttpServletRequest request, HttpServletResponse response) throws IOException, IllegalArgumentException, IllegalAccessException{ 18 BufferedWriter bw = null; 19 20 response.setContentType("text/html;charset=UTF-8"); 21 request.setCharacterEncoding("UTF-8"); 22 response.setHeader("Content-disposition", "attachment; filename="+URLEncoder.encode(fileName,"UTF-8")); 23 //構建輸出流,同時指定編碼 24 bw = new BufferedWriter(new OutputStreamWriter(response.getOutputStream(),"gbk")); 25 26 //csv文件是逗號分隔,除第一個外,每次寫入一個單元格數(shù)據(jù)后需要輸入逗號 27 for(String title : titles){ 28 bw.write(title); 29 bw.write(","); 30 } 31 //寫完文件頭后換行 32 bw.write("\r\n"); 33 //寫內(nèi)容 34 for(Object obj : list){ 35 //利用反射獲取所有字段 36 Field[] fields = obj.getClass().getDeclaredFields(); 37 for(String property : propertys){ 38 for(Field field : fields){ 39 //設置字段可見性 40 field.setAccessible(true); 41 if(property.equals(field.getName())){ 42 bw.write(field.get(obj).toString()); 43 //如果包含:說明是日期最后寫一個|否則日期不顯示秒 44 if((field.get(obj).toString()).indexOf(":") > -1){ 45 bw.write("|"); 46 } 47 bw.write(","); 48 continue; 49 } 50 } 51 } 52 //寫完一行換行 53 bw.write("\r\n"); 54 } 55 bw.flush(); 56 bw.close(); 57 return "0"; 58 }以上實現(xiàn)了根據(jù)頁面查詢結(jié)果,將數(shù)據(jù)集合導出生成csv文件。
轉(zhuǎn)載于:https://www.cnblogs.com/FlyHeLanMan/p/6432616.html
《新程序員》:云原生和全面數(shù)字化實踐50位技術專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的Spring Mvc + Easyui中根据查询结果导出文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JMeter常用组件
- 下一篇: 中英文翻河南志超市政工程有限公司译