0
votes

I have a Kendo grid that I want to export to an Excel worksheet. I have written code that does generate code but can't download an excel file. What suggestions do you have. Please check out my code. Thanks

var dataSource = $("#grid").data("kendoGrid").dataSource;

        var filteredDataSource = new kendo.data.DataSource({
            data: dataSource.data(),
            filter: dataSource.filter()
        });
        filteredDataSource.read();
        var data = filteredDataSource.view();
        var result = "data:application/vnd.ms-excel,";

        //start with the desired column headers here
        var result = '';
        result = "data:application/vnd.ms-excel,";
        result += "<table><tr><th>ID</th><th>Display Name</th></tr>";
        //each column will need to be called using the field name in the data source
        for (var i = 0; i < data.length; i++) {
            result += "<tr>";
            result += "<td>" + data[i].StudentID + "</td>";
            result += "<td>" + data[i].DisplayName + "</td>";
            result += "<td>" + data[i].BirthAddress + "</td>";
            result += "<td>" + data[i].Email1 + "</td>";
            result += "<td>" + data[i].isActive + "</td>";
            result += "</tr>";
        }

        result += "</table>";
        alert(result);
        if (window.navigator.msSaveBlob) {
            //Internet Explorer
            window.navigator.msSaveBlob(new Blob([result]), 'export.xls');
        }

        else if (window.webkitURL != null) {
            //Google Chrome and Mozilla Firefox
            var a = document.createElement('a');
            alert(a);
            a.href = result;
            a.download = 'export.xls';
            result = encodeURIComponent(result);
            a.href = 'data:application/xls;charset=UTF-8,' + result;
            a.download = 'export.xls';
            a.click();
        }
        else {
            //Everything Else
            window.open(result);
        }
        e.preventDefault();
1
Do you get an error? Which version of Kendo?christiandev
I can alert() result, but can't export it to excel. I think there is a kind of bug.Badar Ali
I am using Kendo 2014 for ASP.NET MVC.Badar Ali

1 Answers

0
votes

I have solved much of the problem, and now I can generate excel file in Firefox using code below.

if (navigator.appName == 'Microsoft Internet Explorer') {
          window.navigator.msSaveBlob(new Blob([result]), 'exporteddata' + postfix + 'export.xls');

        }
        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;
            result = encodeURIComponent(result);
            a.href = 'data:application/xls;charset=UTF-8,' + result;
            a.download = 'exporteddata' + postfix + 'export.xls';
            a.click();
        }

But still I cannot generate Excel using IE.