gpt4 book ai didi

javascript - 从 Kendo Grid 数据源导出所有数据

转载 作者:搜寻专家 更新时间:2023-11-01 04:17:42 25 4
gpt4 key购买 nike

我遵循了关于导出 Kendo 网格数据的教程:http://www.kendoui.com/blogs/teamblog/posts/13-03-12/exporting_the_kendo_ui_grid_data_to_excel.aspx

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

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

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

但是我的实际网格会用新的 pageSize 刷新。这是一种无需刷新网格即可查询剑道数据源的方法吗?

谢谢

最佳答案

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

1]在html页面中,添加

$('#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]向 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”添加到 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 上的该项目.

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

关于javascript - 从 Kendo Grid 数据源导出所有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21120909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com