简体   繁体   English

无法将jqgrid表导出为ex​​cel / pdf。

[英]Not able to export jqgrid table as excel/pdf.

I successfully created a small jqgrid table. 我成功创建了一个小的jqgrid表。 I'm trying to export this table to an excel or pdf file using Jquery. 我正在尝试使用Jquery将此表导出到excel或pdf文件。 I am new to jquery and jqgrid. 我是jquery和jqgrid的新手。 Could someone please let me know what is wrong in the code? 有人可以让我知道代码中有什么问题吗? I would really appreciate some help or suggestions. 我非常感谢您的帮助或建议。

I found the export function online. 我在网上找到了导出功能。 It said I have to just call this function with the grid id. 它说我只需要使用网格ID调用此函数。 Am I doing something wrong? 难道我做错了什么?

export.php file: export.php文件:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<link href="CSS/CalibrationKit.css" rel="stylesheet" type="text/css">

<!-- ------------------------JQGRID files-------------------------------- -->
<link rel="stylesheet" type="text/css" media="screen" href="jquery/css/jquery-ui-1.7.1.custom.css" />
<link rel="stylesheet" type="text/css" media="screen" href="css/ui.jqgrid.css" />
<script src="jquery/js/jquery-1.11.0.min.js" type="text/javascript"></script>
<script src="jquery/js/i18n/grid.locale-en.js" type="text/javascript"></script>
<script src="jquery/js/jquery.jqGrid.min.js" type="text/javascript"></script>

<script type="text/javascript">

 $(document).ready(function(e) {

/*jqgrid*/
     var mydata = [{
    head_mean_volume: "50",
    head_std_dev: "2",
    head_cv: "3",
    offset_factor: "4",
    scaling_factor: "5"
}];

    $("#projectSpreadsheet").jqGrid({
    data: mydata,
    datatype: "local",
    colNames: ["Head Mean Volume Dispensed", "Head Standard Deviation", "Head %CV", "Whole Head Offset Factor", "Whole Head Scaling Factor"],
    colModel: [{
        name: 'head_mean_volume',
        index: 'head_mean_volume',
        editable: false,
    }, {
        name: 'head_std_dev',
        index: 'head_std_dev',
        editable: false,
    }, {
        name: 'head_cv',
        index: 'head_cv',
        editable: false,
    }, {
        name: 'offset_factor',
        index: 'offset_factor',
        editable: false,
    }, {
        name: 'scaling_factor',
        index: 'scaling_factor',
        editable: false,
    }],
    'cellEdit': false,
    'cellsubmit' : 'clientArray',
    editurl: 'clientArray'

}); /*jqGrid close */


/* createExcelFromGrid */

  $('#btnSun').click(function() {
    $.fn.myFunction("projectSpreadsheet");
  });
  $.fn.myFunction = function(gridID,filename) {
    var grid = $('#' + gridID);
    var rowIDList = grid.getDataIDs();
    var row = grid.getRowData(rowIDList[0]); 
    var colNames = [];
    var i = 0;
    for(var cName in row) {
        colNames[i++] = cName; // Capture Column Names
    }
    var html = "";
    for(var j=0;j<rowIDList.length;j++) {
        row = grid.getRowData(rowIDList[j]); // Get Each Row
        for(var i = 0 ; i<colNames.length ; i++ ) {
            html += row[colNames[i]] + ';'; // Create a CSV delimited with ;
        }
        html += '\n';
    }
    html += '\n';

    var a         = document.createElement('a');
    a.id = 'ExcelDL';
    a.href        = 'data:application/vnd.ms-excel,' + html;
    a.download    = filename ? filename + ".xls" : 'DataList.xls';
    document.body.appendChild(a);
    a.click(); // Downloads the excel document
    document.getElementById('ExcelDL').remove();
}

}); /* function close */

</script>

</head>

<body>

<table id="projectSpreadsheet" class="fixed_headers" style="width:875px"></table>
                        <br><br>
 <button id="btnSun">Export Table data into Excel</button>               


</body>
</html>

Replace your function with this one and let me know if it works for you. 用此功能替换您的功能,并让我知道它是否对您有用。

function(gridID,filename) {
    var html = $('#gview_' + gridID).html();
    var a = document.createElement('a');
    a.id = 'tempLink';
    a.href = 'data:application/vnd.ms-excel,' + html;
    a.download = filename + ".xls";
    document.body.appendChild(a);
    a.click(); // Downloads the excel document
    document.getElementById('tempLink').remove();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM