Kendo grid data export to a excel file

廉价感情. 提交于 2019-12-05 18:13:35

Try this,

Put this after result += "</table>"; and it's working in all browser.

if (window.navigator.msSaveBlob) {
                window.navigator.msSaveBlob(new Blob([result]), 'exporteddata' + postfix + 'export.csv');
            }
            else if (window.webkitURL != null) {
                // Chrome allows the link to be clicked programmatically.
                var a = document.createElement('a');
                var table_div = (document.getElementById('grid').getElementsByTagName('tbody')[0]);
                var table_html = table_div.innerHTML.replace();
                a.href = result;
                a.download = 'exporteddata' + postfix + 'export.csv';
                a.click();
            }
            else {
                window.open(result);
            }

If you want to export the data to a CSV (openable in Excel), I've written a little bit of code do that and it's on github as Kendo Grid CSV Download

I made a Frankenstein with kendo grid and the jquery-javascript tablet library http://www.datatables.net/. to support export to csv,excel,pdf. Here is my solution:

Add DataTable library and css

            <script src="media/js/jquery.dataTables.min.js"></script>
    <script src="extras/TableTools/media/js/TableTools.min.js"></script>
    <style type="text/css" title="currentStyle">
            @import "media/css/demo_page.css";
            @import "media/css/jquery.dataTables_themeroller.css";
            @import "extras/TableTools/media/css/TableTools.css";
            @import "extras/TableTools/media/css/TableTools_JUI.css";
    </style>

In your .js

    $("#btnExport").click(function (e) {
    //Get your data from kendo grid and apply filters
    var dataSource = $("#yourKendoGrid").data("kendoGrid").dataSource;
    var filters = dataSource.filter();
    var allData = dataSource.data();
    var query = new kendo.data.Query(allData);
    var data = query.filter(filters).data;
    //Set your column headers
    var result = "<table  id='ExportedTable'><thead><tr><th>Col 1</th><th>Col 2</th><th>Col 3</th><th>Col 4</th></thead></tr>";

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

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

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

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

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

        result += "</tr>";
    }

    result += "</tbody>";
    result += "</table>";
    //Append your grid to your div or element. 
    $("#GridToExport").empty().append(result);
    //Initialize dataTable, to get the export functionality
    $("#ExportedTable").dataTable(
        {
            "sDom": 'T<"clear"><"H"lfr>t<"F"ip>',
            "oTableTools": {
                "sSwfPath": "extras/TableTools/media/swf/copy_csv_xls_pdf.swf"
            },
        }
     );
    //Hide your table if you want to have only kendo grid
    $("#ExportedTable").hide();
    //Hide the info panel and paginate controls from datatable component
    $("[class*='dataTables_info']").hide();
    $("[class*='dataTables_paginate']").hide();
    $("[class*='dataTables_filter']").hide();
    $("[class*='dataTables_length']").hide();

    e.preventDefault();
});

So you get this

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!