面向ASP的Telerik UI。网络核心免费下载30天试用版

服务器的出口

面向ASP的Telerik UI网格。网络核心exposes the functionality to export its data to XLSX (Excel) and CSV on the Server. This approach is recommended when the data is large and resource demanding. It escapes the need to serialize the whole dataset and to create the file on the Client. When Server-side export it used, the file is created on the Server and is pushed to the Client as a文件对象。

有关可运行示例,请参阅演示服务器导出的网格

开始

要启用网格的服务器导出选项:

  1. 包括对Telerik. core . export .nupkg的引用,该引用来自私有Telerik NuGet提要。
  2. 包含一个表单HTML元素,该元素将发送到服务器端的ActionMethod。

    action = " <形式。Action("ExportServer", "Grid")" method="POST" id="form">   
  3. 将click事件处理程序附加到表单中的按钮上,并包含列的数据和设置:

  4. 在服务器端创建文件并将其推送到客户端:

    public FileStreamResult ExportServer(字符串模型,字符串数据){var columnsData = JsonConvert.DeserializeObject>(httutility . urldecode(模型));dynamic options = JsonConvert.DeserializeObject(httutility . urldecode (data));SpreadDocumentFormat exportFormat = options.format.ToString() = "csv" ?exportFormat = SpreadDocumentFormat.Csv; exportFormat = SpreadDocumentFormat.Xlsx;Action cellStyle = new Action(ChangeCellStyle);Action rowStyle = new Action(ChangeRowStyle);Action columnStyle = new Action(ChangeColumnStyle);string fileName = string. format ("{0}. "{1}”选项。标题、options.format);字符串mimeType = Helpers.GetMimeType(exportFormat); Stream exportStream = exportFormat == SpreadDocumentFormat.Xlsx ? productService.Read().ToXlsxStream(columnsData, (string)options.title.ToString(), cellStyleAction: cellStyle, rowStyleAction: rowStyle, columnStyleAction: columnStyle) : productService.Read().ToCsvStream(columnsData); var fileStreamResult = new FileStreamResult(exportStream, mimeType); fileStreamResult.FileDownloadName = fileName; fileStreamResult.FileStream.Seek(0, SeekOrigin.Begin); return fileStreamResult; } private void ChangeCellStyle(ExportCellStyle e) { bool isHeader = e.Row == 0; SpreadCellFormat format = new SpreadCellFormat { ForeColor = isHeader ? SpreadThemableColor.FromRgb(50, 54, 58) : SpreadThemableColor.FromRgb(214, 214, 217), IsItalic = true, VerticalAlignment = SpreadVerticalAlignment.Center, WrapText = true, Fill = SpreadPatternFill.CreateSolidFill(isHeader ? new SpreadColor(93, 227, 0) : new SpreadColor(50, 54, 58)) }; e.Cell.SetFormat(format); } private void ChangeRowStyle(ExportRowStyle e) { e.Row.SetHeightInPixels(e.Index == 0 ? 80 : 30); } private void ChangeColumnStyle(ExportColumnStyle e) { double width = e.Name == "Product name" || e.Name == "Category Name" ? 250 : 100; e.Column.SetWidthInPixels(width); }
  5. 在Controller文件中增加如下引用:

    使用Kendo.Mvc.Export;使用Telerik.Documents.SpreadsheetStreaming;使用Microsoft.AspNetCore.Mvc;

仅导出选定数据

默认情况下,asp.net的Telerik UI Grid。NET Core导出所有行。但是,为了只导出选定的行,请将网格设置为可选择的。

.Selectable ()
<可选/ >

或者,为选择添加一个复选框列:

.Columns(columns => {columns. select ().Width(80);})

另请参阅

在本文中
Baidu
map