从 Kendo Grid 数据源导出所有数据 [英] Exporting all data from Kendo Grid datasource

查看:52
本文介绍了从 Kendo Grid 数据源导出所有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了有关导出剑道网格数据的教程:http://www.kendoui.c​​om/blogs/teamblog/posts/13-03-12/exporting_the_kendo_ui_grid_data_to_excel.aspx

I followed that tutorial about exporting Kendo Grid Data : http://www.kendoui.com/blogs/teamblog/posts/13-03-12/exporting_the_kendo_ui_grid_data_to_excel.aspx

现在我正在尝试导出所有数据(不仅仅是显示的页面)......我该怎么做?

Now I´m trying to export all data (not only the showed page) ... How can I do that?

我尝试在获取数据之前更改 pagezise:

I tried change the pagezise before get the data:

grid.dataSource.pageSize(grid.dataSource.total());

但是我的实际网格刷新为新的 pageSize.这是一种在不刷新网格的情况下查询剑道数据源的方法吗?

But with that my actual grid refresh with new pageSize. Is that a way to query kendo datasource without refresh the grid?

谢谢

推荐答案

更好的解决方案是从真实数据生成 Excel 文件,而不是从数据源生成.

A better solution is to generate an Excel file from the real data, not from the dataSource.

1]在html页面添加

1] In the html page, add

$('#export').click(function () {
    var title = "EmployeeData";
    var id = guid();
    var filter = $("#grid").data("kendoGrid").dataSource._filter;

    var data = {
        filter: filter,
        title: title,
        guid: id
    };

    $.ajax({
        url: '/Employee/Export',
        type: "POST",
        dataType: 'json',
        data: JSON.stringify(data),
        contentType: "application/json; charset=utf-8",
        success: function (result) {
            window.location = kendo.format("{0}?title={1}&guid={2}", '/Employee/GetGeneratedExcel', title, id);
        }
    });
});


2]给控制器添加一个方法Export":


2] Add a method "Export" to the controller:

[HttpPost]
public JsonResult Export(KendoGridFilter filter, string guid)
{
    var gridRequest = new KendoGridRequest();
    if (filter != null)
    {
        gridRequest.FilterObjectWrapper = filter.Filters != null ? filter.ToFilterObjectWrapper() : null;
        gridRequest.Logic = filter.Logic;
    }

    var query = GetQueryable().AsNoTracking();
    var results = query.FilterBy<Employee, EmployeeVM>(gridRequest);

    using (var stream = new MemoryStream())
    {
        using (var excel = new ExcelPackage(stream))
        {
            excel.Workbook.Worksheets.Add("Employees");
            var ws = excel.Workbook.Worksheets[1];
            ws.Cells.LoadFromCollection(results);
            ws.Cells.AutoFitColumns();

            excel.Save();
            Session[guid] = stream.ToArray();
            return Json(new { success = true });
        }
    }
}


3]还将方法GetGeneratedExcel"添加到控制器:


3] Also add the method "GetGeneratedExcel" to the controller:

[HttpGet]
public FileResult GetGeneratedExcel(string title, string guid)
{
    // Is there a spreadsheet stored in session?
    if (Session[guid] == null)
    {
        throw new Exception(string.Format("{0} not found", title));
    }

    // Get the spreadsheet from session.
    var file = Session[guid] as byte[];
    string filename = string.Format("{0}.xlsx", title);

    // Remove the spreadsheet from session.
    Session.Remove(title);

    // Return the spreadsheet.
    Response.Buffer = true;
    Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", filename));
    return File(file, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", filename);
}

还可以在 github 上查看此项目.

Also see this project on github.

请参阅这个现场示例项目,您可以在其中将员工导出到 Excel.(虽然这会返回过滤后的数据,但您可以修改代码以忽略kendo网格过滤器并始终返回所有数据.

See this live example project where you can export the Employees to Excel. (Although this returns filtered data, but you can modify the code to ignore the kendo grid filter and always return all data.

这篇关于从 Kendo Grid 数据源导出所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆