2

使用 kendo ui 控件导出数据,但在导出此文本时也在 IE 中导出,但在 chrome 和 mozilla 中工作正常。如何防止该文本?

   **var result = "data:application/vnd.ms-excel,";**  
                   //this text is also exporting in excel sheet//

任何人都可以检查html文件中的以下代码并给出解决方案

     <!DOCTYPE html>
      <html lang="en-US">
        <head>
           <link  href="http://cdn.kendostatic.com/2013.1.319/styles/kendo.common.min.css" rel="stylesheet" type="text/css" />
       <link href="http://cdn.kendostatic.com/2013.1.319/styles/kendo.dataviz.min.css" rel="stylesheet" type="text/css" />
      <link href="http://cdn.kendostatic.com/2013.1.319/styles/kendo.default.min.css" rel="stylesheet" type="text/css" />
         <link href="http://cdn.kendostatic.com/2013.1.319/styles/kendo.dataviz.default.min.css" rel="stylesheet" type="text/css" />

    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script src="http://cdn.kendostatic.com/2013.1.319/js/kendo.all.min.js"></script>
    <script src="http://cdn.kendostatic.com/2013.1.319/js/kendo.aspnetmvc.min.js"> 
          </script>
          </head>
        <body>
   <div class="toolbar" style="height:32px;" >
                          <input type="button" id="btnExport" value="Export Table Data"       style="font-size: 13px; font-family: Gill Sans MT;" />
                </div>     
                       <div id="grid"></div>

                   <script type="text/javascript"> 

             var ds1 =new kendo.data.DataSource({
             type: "odata",
           transport: {
               read:
             "http://demos.kendoui.com/service/Northwind.svc/Products"
               }
                     });



          function createGrid()
 {
 var grid= $("#grid").kendoGrid({ 
 dataSource:ds1,
   schema: {
 model: { 
      fields: {
         ProductID: { type: "number" }, 
         UnitPrice: { type: "number" },
          ProductName: { type: "string" }, 
       } }},

          pageable: true,
           columns: [
    { field: "ProductID", title:"Product ID", width:100 },
      { field: "ProductName", title:"Product Name" },
    { field: "UnitPrice", title:"Unit Price", width: 100 } ,
    {field: "SupplierID", title:"Supplier ID", width: 100}
        ]
        });
        }
        function gridExport() {
            $("#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,";**  
                   //this text is also exporting
                    result += "<table><tr><th>ProductID</th><th>ProductName</th><th>SupplierID</th><th>UnitPrice</th><th>UnitsOnOrder</th></tr>";
                    for (var i = 0; i < data.length; i++) {
                        result += "<tr>";

                        result += "<td>";
                        result += data[i].ProductID;
                        result += "</td>";
                        result += "<td>";
                        result += data[i].ProductName;
                        result += "</td>";
                        result += "<td>";
                        result += data[i].SupplierID;
                        result += "</td>";
                        result += "<td>";
                        result += data[i].UnitPrice;
                        result += "</td>";
                        result += "<td>";
                        result += data[i].UnitsOnOrder ;
                        result += "</td>";

                    }
                    result += "</table>";
                     var dt = new Date();
                    var day = dt.getDate();
                    var month = dt.getMonth() + 1;
                    var year = dt.getFullYear();
                    var hour = dt.getHours();
                    var mins = dt.getMinutes();
                    var postfix = day + "." + month + "." + year + " " + hour + ":" + mins;


                    if (window.navigator.msSaveBlob) {
                    window.navigator.msSaveBlob(new Blob([result]), 'Telesurdata@' + postfix + '.xls');
                    } 

                    else if(window.webkitURL != null){
                    var a = document.createElement('a');
                    var tableDiv = (document.getElementById('grid').getElementsByTagName('tbody')[0]);
                    // var tableHtml = tableDiv.innerHTML.replace();
                    a.href = result;
                    a.download = 'exportdata@' + postfix + '.xls';
                    a.click(); 
                    e.preventDefault();
                   }

                   else{
                     window.open(result); 
                      }

            });


        }

    $(document).ready(function () {
        createGrid(); 
      gridExport();
             });

            </script>

4

0 回答 0