防止kendo datagrid导出到excell下载对话框

时间:2017-06-29 19:23:24

标签: javascript excel kendo-ui kendo-grid

我需要将Excell文件restult从导出kendo datagrid保存到数据库中,向服务器发出ajax请求(POST)。所以,我怀疑数据网格的excelExport事件。这是我用于此的代码:

var grid = $("#myGrid").data("kendoGrid");
grid.bind("excelExport", exportGridAsExcell);
grid.saveAsExcell();

function exportGridAsExcell(e) {
        e.defaultPrevented = true;
        var dataUrl = new kendo.ooxml.Workbook(e.workbook).toDataURL();
        var base64 = dataUrl.split(";base64,")[1];

        //make an ajax request with base64 variable as param
}

问题是在exportGridAsExcell结束后,浏览器会显示一个DownloadDialog。我不想要这个。我怎么能达到这个目的呢?

1 个答案:

答案 0 :(得分:0)

看起来像e.defaultPrevented = true;在您的情况下不起作用,尝试使用来自telerik docs的代码source

 $("#grid").kendoGrid({
        toolbar: ["excel"],
        dataSource: {
            type: "odata",
            transport: {
                read: "http://demos.telerik.com/kendo-ui/service/Northwind.svc/Products"
            },
            pageSize: 7
        },
        pageable: true,
        columns: [
            { width: 300, field: "ProductName", title: "Product Name" },
            { field: "UnitsOnOrder", title: "Units On Order" },
            { field: "UnitsInStock", title: "Units In Stock" }
        ],
        excelExport: function(e) {
            // Prevent the default behavior which will prompt the user to save the generated file.
            e.preventDefault();
            // Get the Excel file as a data URL.
            var dataURL = new kendo.ooxml.Workbook(e.workbook).toDataURL();
            // Strip the data URL prologue.
            var base64 = dataURL.split(";base64,")[1];
            // Post the base64 encoded content to the server which can save it.
            $.post("/server/save", {
                base64: base64,
                fileName: "ExcelExport.xlsx"
            });
        }
    });

这是"工作"在dojo click上的示例,只需按下excel导出按钮并查看控制台输出