剑道网格出口优秀

时间:2015-02-12 11:57:45

标签: asp.net-mvc kendo-grid kendo-asp.net-mvc

我有一个Kendo网格,我想导出到Excel工作表。我编写的代码确实生成代码但无法下载excel文件。你有什么建议。请查看我的代码。感谢

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 个答案:

答案 0 :(得分:0)

我解决了很多问题,现在我可以使用下面的代码在Firefox中生成excel文件。

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();
        }

但我仍然无法使用IE生成Excel。