jqgrid导出excel文档

  1. 云栖社区>
  2. 博客>
  3. 正文

jqgrid导出excel文档

dsn727455218 2018-04-10 14:59:44 浏览969
展开阅读全文

喜欢的朋友可以关注下,粉丝也缺。

        

最近一个项目中需要把jqgrid的数据导出成excel文档,所以去查询一下jq的文档,它所提供的接口是需要收费的,当时我就一脸嫌弃的表情。

        无奈之举只能自己想办法了,前思后想,突然灵光一闪,可以把数据提交到后台通过后台写出到文档。

        首先我封装了一个js把jq的数据提交的后台,下面我们来看看这个封装的js:

        

function getXlsFromTbl(table_id, container_id ,form_id, title, rownumbers) {  
		    try {  
		        var content = "";  
		        if (table_id != null && table_id != "" && table_id != "null") {  
		        content = getTblData($('#' + table_id), $('#' + container_id), rownumbers);  
		        }  
		        if (content == "") {  
		            alert("表格不存在");  
		            return;  
		        }  
		        var fileName = getExcelFileName(title);  
		          
		        doFileExport($('#' + form_id), fileName, content);  
		    }  
		    catch (e) {  
		        alert("导出异常:" + e.name + "->" + e.description + "!");  
		    }  

	}

        function getTblData(tableobj, containerobj, rownumbers) {  
		    var outStr = "";  
		    if (tableobj != null) {  
		        var rowdata = tableobj.getRowData();  
		        var Lenr = 1;  
		        for (i = 0; i < Lenr; i++) {  
		            //var Lenc = curTbl.rows(i).cells.length;  
		            var th;  
		            if (rownumbers == false) {  
		                th = containerobj.find('TH:not(:first-child)');  
		            }  
		            else {  
		                th = containerobj.find('TH');  
		            }  
		            th.each(function(index, element) {  
		                var j = index + 1;  
		                var content = $(element).text();  
		                content = content.replace(/(^\s*)|(\s*$)/g, "");//去掉空格  
		                outStr += content + ",";  
		            });  
		            outStr += "+nl+";  
		        }  
		        var tmp = "";  
		        for (i = 0; i < rowdata.length; i++) {  
		            var row = eval(rowdata[i]);  
		            for (each in row) {  
		            var temp = $(row[each]).text();  
		            if($(row[each]).text() == null || $(row[each]).text() == ""){  
		            	    if(row[each].charAt(0) != '<')  
		            	    	 outStr += row[each] + ",";  
		            }  
		             else  
		            	     outStr += $(row[each]).text() + ",";  
		            }  
		            outStr += "+nl+";  
		        }  
		    }  
		    else {  
		        outStr = null;  
		        alert(inTbl + " null!");  
		    }  
		    return outStr;  

		}

            function getExcelFileName(title) {  
		    var d = new Date();  
		    var curYear = d.getYear();  
		    var curMonth = "" + (d.getMonth() + 1);  
		    var curDate = "" + d.getDate();  
		    var curHour = "" + d.getHours();  
		    var curMinute = "" + d.getMinutes();  
		    var curSecond = "" + d.getSeconds();  
		    if (curMonth.length == 1) {  
		        curMonth = "0" + curMonth;  
		    }  
		    if (curDate.length == 1) {  
		        curDate = "0" + curDate;  
		    }  
		    if (curHour.length == 1) {  
		        curHour = "0" + curHour;  
		    }  
		    if (curMinute.length == 1) {  
		        curMinute = "0" + curMinute;  
		    }  
		    if (curSecond.length == 1) {  
		        curSecond = "0" + curSecond;  
		    }  
		    var fileName = title + "_" + curYear + curMonth + curDate + "_"  
		            + curHour + curMinute + curSecond + ".csv";  
		  
		  
		    return fileName;  

		}

            function doFileExport(formobj, filename, content) {  
		   formobj.html("<input id='filename' name='filename' type='text' style='display: none'><input                             id='content' name='content' type='text' style='display: none'>");  
		       $("#filename").val(filename);  
		       $("#content").val(content);  
		       formobj.submit();  
		       $("#gridTable").trigger("reloadGrid");
		       

            }


上面就是封装的js文件,只需要在页面里调用一下方法就行,需要注意的是页面上需要一个form表达一个div容器包含table示例如下:

  

<form id="download_form" method="post"  target="_blank" action="/liushouet/DownLoadTableDataAction">  
	 <div id="table_container">  
	           <table id="gridTable"></table>
                   <div id="gridPager"></div>

	 </div>

  </form>

  接下来只需要在后台里面写一段java代码就好了,就可以把jq的数据导出到excel里面。

    

protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("gbk");


        String content = request.getParameter("content");
        String filename = request.getParameter("filename");


        content = content.replace("+nl+", "\n");
        response.setContentType("application/octet-stream;charset=gbk");
        response.setHeader("content-disposition", "attachment;filename=" + filename);
        BufferedOutputStream write = new BufferedOutputStream(response.getOutputStream());
        write.write(content.getBytes("gbk"));
        write.flush();
        write.close();

    }

    就是这么的简单,这里已经完了,一个完整的流程,对于jq的这个接口是要收费的真的必须鄙视一下,已经打算放弃这个插件,有好用的插件欢迎各位推荐下,欢迎各位喜欢水的加Q群308742428

喜欢的朋友可以关注下,粉丝也缺。


网友评论

登录后评论
0/500
评论