easyui導出Excel、Word java

easyui導出Excel、Word java

標籤: easyuiexceljava
 3651人閱讀 評論(1) 收藏 舉報
 分類:
 
問題描述:easyui datagrid導出Excel,使用java後臺適用於多個頁面
解決方案:第一:在含有datagrid的界面exportPage.jsp中引用js(主要用於獲取datagrid數據以及轉換爲html格式)
代碼爲:<script type="text/javascript" src="<%=request.getContextPath()%>/js/export.js"></script>
第二:在exportPage.jsp的javascritp編輯中設置變量var grid='';var exportString='';並且在<body></body>之間添加一個div,主要用於彈出對話框,選擇導出格式:代碼:<div id="dialog2"></div> 
第三:將exportPage.jsp的datagrid初始化時賦值於grid,
代碼爲:
[html] view plain copy
  1. <span style="white-space:pre">    </span>grid=$('#dg').datagrid({  
  2.                 url:.....,  
  3.                 toolbar: [{  
  4.                     text:'Export',  
  5.                     iconCls: 'icon-excel',//自定義的css樣式圖片  
  6.                     handler: function(){  
  7.                     expt(grid);  
  8.                     }  
  9.                 },'-']  
  10.             });  


第四:export.js的代碼爲:
[javascript] view plain copy
  1. function expt(grid){  
  2.         var tableString = '<table cellspacing="0" class="pb">';    
  3.             var frozenColumns = grid.datagrid("options").frozenColumns;  // 得到frozenColumns對象    
  4.         var columns = grid.datagrid("options").columns;    // 得到columns對象    
  5.             var nameList = new Array();    
  6.     
  7.     // 載入title    
  8.     if (typeof columns != 'undefined' && columns != '') {    
  9.         $(columns).each(function (index) {    
  10.             tableString += '\n<tr>';    
  11.             if (typeof frozenColumns != 'undefined' && typeof frozenColumns[index] != 'undefined') {    
  12.                 for (var i = 0; i < frozenColumns[index].length; ++i) {    
  13.                     if (!frozenColumns[index][i].hidden) {    
  14.                         tableString += '\n<th width="' + frozenColumns[index][i].width + '"';    
  15.                         if (typeof frozenColumns[index][i].rowspan != 'undefined' && frozenColumns[index][i].rowspan > 1) {    
  16.                             tableString += ' rowspan="' + frozenColumns[index][i].rowspan + '"';    
  17.                         }    
  18.                         if (typeof frozenColumns[index][i].colspan != 'undefined' && frozenColumns[index][i].colspan > 1) {    
  19.                             tableString += ' colspan="' + frozenColumns[index][i].colspan + '"';    
  20.                         }    
  21.                         if (typeof frozenColumns[index][i].field != 'undefined' && frozenColumns[index][i].field != '') {    
  22.                             nameList.push(frozenColumns[index][i]);    
  23.                         }    
  24.                         tableString += '>' + frozenColumns[0][i].title + '</th>';    
  25.                     }    
  26.                 }    
  27.             }    
  28.             for (var i = 0; i < columns[index].length; ++i) {    
  29.                 if (!columns[index][i].hidden) {    
  30.                     tableString += '\n<th width="' + columns[index][i].width + '"';    
  31.                     if (typeof columns[index][i].rowspan != 'undefined' && columns[index][i].rowspan > 1) {    
  32.                         tableString += ' rowspan="' + columns[index][i].rowspan + '"';    
  33.                     }    
  34.                     if (typeof columns[index][i].colspan != 'undefined' && columns[index][i].colspan > 1) {    
  35.                         tableString += ' colspan="' + columns[index][i].colspan + '"';    
  36.                     }    
  37.                     if (typeof columns[index][i].field != 'undefined' && columns[index][i].field != '') {    
  38.                         nameList.push(columns[index][i]);    
  39.                     }    
  40.                     tableString += '>' + columns[index][i].title + '</th>';    
  41.                 }    
  42.             }    
  43.             tableString += '\n</tr>';    
  44.         });    
  45.     }    
  46.     // 載入內容    
  47.     var rows = grid.datagrid("getRows"); // 這段代碼是獲取當前頁的所有行    
  48.     for (var i = 0; i < rows.length; ++i) {    
  49.         tableString += '\n<tr>';    
  50.         for (var j = 0; j < nameList.length; ++j) {    
  51.             var e = nameList[j].field.lastIndexOf('_0');    
  52.     
  53.             tableString += '\n<td';    
  54.             if (nameList[j].align != 'undefined' && nameList[j].align != '') {    
  55.                 tableString += ' style="text-align:' + nameList[j].align + ';"';    
  56.             }    
  57.             tableString += '>';    
  58.             if (e + 2 == nameList[j].field.length) {    
  59.                 tableString += rows[i][nameList[j].field.substring(0, e)];    
  60.             }    
  61.             else    
  62.                 tableString += rows[i][nameList[j].field];    
  63.             tableString += '</td>';    
  64.         }    
  65.         tableString += '\n</tr>';    
  66.     }    
  67.     tableString += '\n</table>';   
  68.     $('#hlf').val(tableString);  
  69.     exportString=tableString;  
  70.     var url="../export.jsp";  
  71.     var param2={  
  72.             doSize:false,  
  73.             shadow:false,  
  74.             content:'<iframe  scrolling="no" frameborder="0"  src="'+url+'" style="width:100%;height:95%;"></iframe>',  
  75.             title:'Export',  
  76.             width:300,      
  77.             height:170,  
  78.             modal:true        
  79.     };  
  80.     mpgdialog(param2);  
  81. }   
  82.   
  83.   
  84. function mpgdialog(param){    
  85.         $('#dialog2').dialog(param);  
  86.         $('#dialog2').window('center');  
  87.     }  




第五:export.jsp代碼爲:
[plain] view plain copy
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  3.     pageEncoding="gb2312"%>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
  8. <link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/js/easyui/themes/default/easyui.css">  
  9. <script type="text/javascript" src="<%=request.getContextPath()%>/js/easyui/jquery.min.js"></script>  
  10. <script type="text/javascript" src="<%=request.getContextPath()%>/js/easyui/jquery.easyui.min.js"></script>  
  11. <script type="text/javascript" src="<%=request.getContextPath()%>/js/common/init.js"></script>  
  12. <script type="text/javascript">  
  13. //var currTabId=parent.currTabId;  
  14. //var parentObj=parent.$("#"+currTabId)[0].contentWindow;  
  15. window.onload=function(){  
  16.     document.getElementById("hlf").value=parent.exportString;  
  17. };  
  18.   
  19.   
  20. function btnExcel(){  
  21.     document.getElementById("type").value="excel";  
  22.     document.getElementById("formAction").submit();  
  23. }  
  24. function btnWord(){  
  25.     document.getElementById("type").value="word";  
  26.     document.getElementById("formAction").submit();  
  27. }  
  28. </script>  
  29. </head>  
  30. <body style="padding:20px; align:center" >  
  31.      <form id='formAction' action="<%=request.getContextPath()%>/Export.do"  method="post">  
  32.         <font size="3px">Please select the type of export for export ......</font><br /><br />  
  33.         <input id="dgg" class="easyui-linkbutton" type="button" value="Excel" onclick="btnExcel()"/>  
  34.         <input id="dgg" class="easyui-linkbutton" type="button" style="margin-left:50px;" value="Word" onclick="btnWord()"/>  
  35.         <input id="hlf" name='hfs'  type="hidden"/>  
  36.         <input id="type" name='type'  type="hidden"/>  
  37.     </form>  
  38. </body>  
  39. </html>  


第五:後臺代碼爲:
[java] view plain copy
  1. package com.ieslab.eim.login.action;  
  2.   
  3.   
  4. import java.io.IOException;  
  5. import java.io.PrintWriter;  
  6. import java.io.UnsupportedEncodingException;  
  7.   
  8.   
  9. import javax.servlet.http.HttpServletRequest;  
  10. import javax.servlet.http.HttpServletResponse;  
  11.   
  12.   
  13. import org.apache.struts.action.Action;  
  14. import org.apache.struts.action.ActionForm;  
  15. import org.apache.struts.action.ActionForward;  
  16. import org.apache.struts.action.ActionMapping;  
  17.   
  18.   
  19. public class Export extends Action{  
  20.     public ActionForward execute(ActionMapping actionMapping,  
  21.             ActionForm actionForm,  
  22.             HttpServletRequest request,  
  23.             HttpServletResponse response) {    
  24.             response.reset();  
  25.             try {  
  26.                 request.setCharacterEncoding("UTF-8");  
  27.             } catch (UnsupportedEncodingException e1) {  
  28.             // TODO Auto-generated catch block  
  29.                 e1.printStackTrace();  
  30.             }  
  31.             String hf=request.getParameter("hfs");  
  32.             String type=request.getParameter("type");  
  33.             String exportname="grid";  
  34.             try {  
  35.                 if(type.equals("excel"))  
  36.                 {  
  37.                     exportname+=".xls";  
  38.                     response.setHeader("Content-disposition""attachment; filename="+java.net.URLEncoder.encode(exportname, "UTF-8")+"");  
  39.                     response.setContentType("application/msexcel;charset=utf-8");  
  40.                 }  
  41.                 else if(type.equals("word"))  
  42.                 {  
  43.                     exportname+=".doc";  
  44.                     response.setHeader("Content-disposition""attachment; filename="+java.net.URLEncoder.encode(exportname, "UTF-8")+"");  
  45.                     response.setContentType("application/ms-word;charset=UTF-8");  
  46.                 }  
  47.             } catch (UnsupportedEncodingException e) {  
  48.             // TODO Auto-generated catch block  
  49.                 e.printStackTrace();  
  50.             }  
  51.             PrintWriter out;  
  52.             try {  
  53.                 out = response.getWriter();  
  54.                 out.println(hf);  
  55.                 out.close();  
  56.             } catch (IOException e) {  
  57.                 // TODO Auto-generated catch block  
  58.                 e.printStackTrace();  
  59.             }  
  60.             return actionMapping.findForward("failure");  
  61.      }  
  62.   
  63.   
  64. }  

轉載網址:http://blog.csdn.net/haolifa/article/details/44344147
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章