当前位置:编程学习 > JAVA >>

jqgrid 表格数据导出

jqgrid 表格数据导出

jqgrid并没有自带导出表格数据的方法,这里就自己实现了一个,尝试过在页面直接将数据导出,发现只有IE下可以通过调用saveas来实现,但是别的浏览器不支持,于是考虑将数据传回后台,然后后台返回下载文件来实现。

首先,是一段javascript脚本:

[javascript] 
/** 
 *  
 *  
 * @param table_id 表格的id 
 * @param container_id 容器的id 
 * @param form_id 提交表单的id 
 * @param title 文件名 
 * @param rownumbers<div> 
</div> 
*/
[javascript] 
function getXlsFromTbl(table_id, container_id ,form_id, title, rownumbers) { 
    try { 
        var content = ""; 
 
 
        if (table_id != null && table_id != "" && table_id != "null") { 
        <span style="white-space:pre">  </span>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) { 
            <span style="white-space:pre">    </span>var temp = $(row[each]).text(); 
            <span style="white-space:pre">    </span>if($(row[each]).text() == null || $(row[each]).text() == ""){ 
            <span style="white-space:pre">        </span>if(row[each].charAt(0) != '<') 
            <span style="white-space:pre">            </span>outStr += row[each] + ","; 
            <span style="white-space:pre">        </span> 
            <span style="white-space:pre">    </span>} 
            <span style="white-space:pre">    </span>else 
            <span style="white-space:pre">        </span>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) { 
&

补充:web前端 , JavaScript ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,