4

我已经配置了剑道网格并配置了所有表格行和标题。当我单击导出按钮时,它正在生成一个 excel 文件。但是问题发生在哪里我不知道我在 IE 中所做的相同配置而不是生成文件它在 URL 中显示数据格式data:application/vnd.ms-excel,<table><tr><th>OrderID</th><th>Freight</th>.....

   var grid = $("#grid").kendoGrid({
        dataSource: {
            type           : "odata",
            transport      : {
                read: "http://demos.kendoui.com/service/Northwind.svc/Orders"
            },
            schema         : {
                model: {
                    fields: {
                        OrderID  : { type: "number" },
                        Freight  : { type: "number" },
                        ShipName : { type: "string" },
                        OrderDate: { type: "date" },
                        ShipCity : { type: "string" }
                    }
                }
            },
            pageSize       : 10
        },
        filterable: true,
        sortable  : true,
        pageable  : true,
        columns   : [
            {
                field     : "OrderID",
                filterable: false
            },
            "Freight",
            {
                field : "OrderDate",
                title : "Order Date",
                width : 100,
                format: "{0:MM/dd/yyyy}"
            },
            {
                field: "ShipName",
                title: "Ship Name",
                width: 200
            },
            {
                field: "ShipCity",
                title: "Ship City"
            }
        ]
    }).data("kendoGrid");

按钮单击以将网格数据导出到 Excel。

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

 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,";

 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>";
 if (window.navigator.msSaveBlob) {
        window.navigator.msSaveBlob(new Blob([result]),'export.xls');
    } else {
        window.open(result);
    }


 e.preventDefault();
});
4

3 回答 3

1

尝试这个,

把它放在后面result += "</table>";,它可以在所有浏览器中工作。

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);
            }
于 2013-09-13T09:50:11.330 回答
0

如果您想将数据导出到 CSV(可在 Excel 中打开),我已经编写了一些代码,它在 github 上作为Kendo Grid CSV 下载

于 2014-03-03T11:48:53.410 回答
0

我用剑道网格和 jquery-javascript 平板电脑库http://www.datatables.net/制作了一个科学怪人。支持导出到 csv、excel、pdf。这是我的解决方案:

添加 DataTable 库和 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>

在你的 .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();
});

所以你得到这个

在此处输入图像描述

于 2014-03-08T00:17:33.540 回答