0

I want to export the kendo grid data to excel on button click.I have tried that one,it's working but it exports only one page data and footer also exported.But i want to export total grid data without footer.

my code is

$("#btnExport").click(function(e) {
    window.open('data:application/vnd.ms-excel,' + document.getElementById('grid').outerHTML.replace(/ /g, '%20'));
    e.preventDefault();
}); 
4

1 回答 1

1

Your code is exporting the outerHTML of the grid. This of course includes all HTML (header, footer, data etc.). Since you have paging enabled the grid HTML will include only the current page of data.

Also after checking your fiddle it seems that you are using serverPaging which means you don't have all the data to begin with. You need to disable serverPaging if you want to have all data. Then create the HTML of the grid by traversing the result of the data() method of the data source.

$("#btnExport").click(function(e) {

     var data = $("#grid").data("kendoGrid").dataSource.data();
     var result = "data:application/vnd.ms-excel,";

     result += "<table><tr><th>OrderID</th><th>Freight</th><th>Order Date</th><th>Ship Name</th><th>Ship City</th></tr>";

     for (var i = 0; i < data.length; i++) {
         result += "<tr>";

         result += "<td>";
         result += data[i].OrderID;
         result += "</td>";

         result += "<td>";
         result += data[i].Freight;
         result += "</td>";

         result += "<td>";
         result += kendo.format("{0:MM/dd/yyyy}", data[i].OrderDate);
         result += "</td>";

         result += "<td>";
         result += data[i].ShipName;
         result += "</td>";

         result += "<td>";
         result += data[i].ShipCity;
         result += "</td>";

         result += "</tr>";
     }

     result += "</table>";
     window.open(result);

     e.preventDefault();
});

Here is the updated jsfiddle: http://jsfiddle.net/SZBrt/4/

于 2013-03-06T09:09:47.677 回答