SoFunction
Updated on 2025-03-01

jqgrid table data export example


/**
 * 
 * 
* @param table_id The id of the table
* @param container_id The id of the container
* @param form_id id of the form submitted
* @param title file name
 * @param rownumbers
 */ 
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("The form does not exist");
            return; 
        } 
        var fileName = getExcelFileName(title); 

        doFileExport($('#' + form_id), fileName, content); 
    } 
    catch (e) { 
alert("Export exception:" + + "->" + + "!");
    } 

function getTblData(tableobj, containerobj, rownumbers) { 

 
    var outStr = ""; 
    if (tableobj != null) { 
        var rowdata = (); 
        var Lenr = 1; 

 
        for (i = 0; i < Lenr; i++) { 
            //var Lenc = (i).;  
            var th; 
            if (rownumbers == false) { 
                th = ('TH:not(:first-child)'); 
            } 
            else { 
                th = ('TH'); 
            } 
            (function(index, element) { 
                var j = index + 1; 
                var content = $(element).text(); 
content = (/(^\s*)|(\s*$)/g, "");//Remove spaces
                outStr += content + ","; 
            }); 
            outStr += "+nl+"; 
        } 
        var tmp = ""; 
        for (i = 0; i < ; 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 = (); 
    var curMonth = "" + (() + 1); 
    var curDate = "" + (); 
    var curHour = "" + (); 
    var curMinute = "" + (); 
    var curSecond = "" + (); 
    if ( == 1) { 
        curMonth = "0" + curMonth; 
    } 
    if ( == 1) { 
        curDate = "0" + curDate; 
    } 
    if ( == 1) { 
        curHour = "0" + curHour; 
    } 
    if ( == 1) { 
        curMinute = "0" + curMinute; 
    } 
    if ( == 1) { 
        curSecond = "0" + curSecond; 
    } 
    var fileName = title + "_" + curYear + curMonth + curDate + "_" 
            + curHour + curMinute + curSecond + ".csv"; 

 
    return fileName; 

function doFileExport(formobj, filename, content) { 
<SPAN style="WHITE-SPACE: pre"> </SPAN>("<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); 
    (); 
}