- Notifications
You must be signed in to change notification settings - Fork 80
kb(grid): Customize export with SpreadStreamProcessing #1423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ntacheva merged 34 commits into master from customize-export-with-spreadstreamprocessing May 30, 2023
+282 −22
Merged
Changes from 24 commits
Commits
Show all changes
34 commits Select commit Hold shift + click to select a range
6d8ecd0
kb(grid): Customize export with SpreadStreamProcessing
ntacheva ac1073a
kb(grid): add more links
ntacheva 02787c2
Update components/grid/export/csv.md
ntacheva 9899cb5
Update components/grid/export/csv.md
ntacheva c0969e9
Update components/grid/export/csv.md
ntacheva 81038d8
Update components/grid/export/events.md
ntacheva bb90167
Update components/grid/export/events.md
ntacheva 1e78644
Update components/grid/export/csv.md
ntacheva 7ef3276
Update knowledge-base/grid-custom-cell-formatting-with-radspreadstrea…
ntacheva 20122cc
Update knowledge-base/grid-custom-cell-formatting-with-radspreadstrea…
ntacheva 089a150
Update knowledge-base/grid-custom-cell-formatting-with-radspreadstrea…
ntacheva 889b6a8
Update knowledge-base/grid-custom-cell-formatting-with-radspreadstrea…
ntacheva 3c6b4bc
Update knowledge-base/grid-custom-cell-formatting-with-radspreadstrea…
ntacheva e0ef80c
Update components/grid/export/events.md
ntacheva 96c8c4c
Update components/grid/export/events.md
ntacheva f43cf13
Update components/grid/export/excel.md
ntacheva a3730ae
Update components/grid/export/excel.md
ntacheva 5522adf
Update knowledge-base/grid-custom-cell-formatting-with-radspreadproce…
ntacheva 33462f2
Update components/grid/export/excel.md
ntacheva 115deef
Update components/grid/export/excel.md
ntacheva b427ab5
Update knowledge-base/grid-custom-cell-formatting-with-radspreadproce…
ntacheva a7e870d
Update knowledge-base/grid-custom-cell-formatting-with-radspreadproce…
ntacheva 85b02c7
Update components/grid/export/csv.md
ntacheva 060787a
Update knowledge-base/grid-custom-cell-formatting-with-radspreadstrea…
ntacheva bb1ea6c
Update knowledge-base/grid-custom-cell-formatting-with-radspreadstrea…
ntacheva f54fbc0
Update knowledge-base/grid-custom-cell-formatting-with-radspreadstrea…
ntacheva 2afe318
Update knowledge-base/grid-custom-cell-formatting-with-radspreadstrea…
ntacheva 7d5ab3e
docs(grid):case fixing
ntacheva 13e6a91
Update components/grid/export/excel.md
ntacheva b1828b7
Update knowledge-base/grid-custom-cell-formatting-with-radspreadstrea…
ntacheva f4e3fe0
Update knowledge-base/grid-custom-cell-formatting-with-radspreadstrea…
ntacheva 95f8f7a
Update components/grid/export/events.md
ntacheva 330377a
chore(grid): fix wording
ntacheva 9add3ee
chore(grid): format comments, add caption
ntacheva File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
| @@ -17,9 +17,10 @@ When you click the Export button, your browser will receive the resulting file. | |
| ||
#### In this article | ||
| ||
| ||
* [Basics](#basics) | ||
* [Programmatic Export](#programmatic-export) | ||
* [Notes](#notes) | ||
- [Basics](#basics) | ||
- [Programmatic Export](#programmatic-export) | ||
- [Notes](#notes) | ||
- [See Also](#see-also) | ||
| ||
## Basics | ||
| ||
| @@ -194,3 +195,4 @@ The Excel export has the following specifics: | |
| ||
* [Live Demo: Grid Excel Export](https://demos.telerik.com/blazor-ui/grid/export-excel) | ||
* [Custom cell formatting of the exported file with RadSpreadProcessing]({%slug grid-kb-custom-cell-formatting-with-radspreadprocessing%}) | ||
* [Custom cell formatting of the exported file with RadSpreadStreamProcessing]({%slug grid-kb-custom-cell-formatting-with-radspreadstreamprocessing%}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
255 changes: 255 additions & 0 deletions 255 knowledge-base/grid-custom-cell-formatting-with-radspreadstreamprocessing.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,255 @@ | ||
--- | ||
title: Custom cell formatting of the exported file with RadSpreadStreamProcessing | ||
| ||
description: How to format the cells of the exported file with RadSpreadStreamProcessing? | ||
type: how-to | ||
page_title: Custom cell formatting of exported Excel files with RadSpreadStreamProcessing | ||
| ||
slug: grid-kb-custom-cell-formatting-with-radspreadstreamprocessing | ||
position: | ||
tags: grid, custom, cell, format, radspreadstreamprocessing | ||
ticketid: 1598580 | ||
res_type: kb | ||
--- | ||
| ||
## Environment | ||
<table> | ||
<tbody> | ||
<tr> | ||
<td>Product</td> | ||
<td>Grid for Blazor</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
| ||
| ||
## Description | ||
| ||
I want to format the cells of the exported Excel file and customize their appearance. For example, to set a background color. How to achieve this without compromising the application performance? When I use RadSpreadProcessing in Blazor WASM applications, customizing the exported file using is slow. | ||
ntacheva marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| ||
How to customize the appearance of the cells in the exported Excel file? | ||
ntacheva marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| ||
In Blazor WASM application customizing the exported file using RadSpreadProcessing is slow. How can I achieve a better performance? | ||
ntacheva marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| ||
## Solution | ||
| ||
Handle the [`OnAfterExport`]({%slug grid-export-events%}#onafterexport) event of the Grid. It fires before the actual file is provided to the user. The `Stream` field of its event argument contains the output of the export as a `MemoryStream`. | ||
| ||
You can copy the bytes from this stream and import them in a [`RadSpreadStreamProcessing workbook`](https://docs.telerik.com/devtools/document-processing/libraries/radspreadstreamprocessing/model/workbook) to access and modify the cells. | ||
| ||
| ||
In WebAssembly applications, the customization of the exported Excel files is faster with the [RadSpreadStreamProcessing library](https://docs.telerik.com/devtools/document-processing/libraries/radspreadstreamprocessing/overview) than with [RadSpreadProcessing]({%slug grid-kb-custom-cell-formatting-with-radspreadprocessing%}). | ||
| ||
The example below demonstrates the export and customization of an Excel file. You can apply the same approach to CSV files. | ||
| ||
To customize the cell format of the exported file before it reaches the client: | ||
| ||
1. Handle the [`OnAfterExport`]({%slug grid-export-events%}#onafterexport) event of the Grid. The stream it provides is finalized so that the resource does not leak. Its binary data, however, is available so you can copy the stream bytes to a new `MemoryStream` instance. | ||
| ||
1. Define the desired cell styles. The example below demonstrates adding cell fill, you can specify different [cell styles](https://docs.telerik.com/devtools/document-processing/libraries/radspreadstreamprocessing/features/cell-styles) based on the result you want to achieve. | ||
| ||
1. [Import the new `MemoryStream` in a `workbook` using the `IWorkbookImporter`](https://docs.telerik.com/devtools/document-processing/libraries/radspreadstreamprocessing/import). | ||
| ||
1. [Define a `IWorkbookExporter` to export the modified file](https://docs.telerik.com/devtools/document-processing/libraries/radspreadstreamprocessing/export) and save it in a new `MemoryStream` instance. | ||
| ||
1. Loop through the sheets, rows and cells to copy their content in the `IWorkbookExporter` instance. | ||
| ||
1. [Apply proper formatting](https://docs.telerik.com/devtools/document-processing/libraries/radspreadstreamprocessing/model/cells#set-a-format) depending on the cell value types. | ||
| ||
1. Apply the [defined styles](https://docs.telerik.com/devtools/document-processing/libraries/radspreadstreamprocessing/features/cell-styles) to the desired cells. | ||
| ||
1. Pass the new `MemoryStream` to the `args.Stream` of the `GridAfterExcelExportEventArgs`, so that the modifications can be saved to the actual exported file. | ||
| ||
````CSHTML | ||
@*Use RadSpreadStreamProcessing to set background to table headers*@ | ||
@using System.IO | ||
@using Telerik.Documents.SpreadsheetStreaming | ||
<TelerikGrid Data="@GridData" | ||
Pageable="true"> | ||
<GridToolBarTemplate> | ||
<GridCommandButton Command="ExcelExport">Export to Excel</GridCommandButton> | ||
<label class="k-checkbox-label"> | ||
<TelerikCheckBox @bind-Value="@ExportAllPages" /> | ||
Export All Pages | ||
</label> | ||
</GridToolBarTemplate> | ||
<GridExport> | ||
<GridExcelExport FileName="telerik-grid-export" | ||
AllPages="@ExportAllPages" | ||
OnAfterExport="@OnExcelAfterExport"> | ||
</GridExcelExport> | ||
</GridExport> | ||
<GridColumns> | ||
<GridColumn Field="@nameof(SampleData.ProductId)" Title="ID" Width="100px" /> | ||
<GridColumn Field="@nameof(SampleData.ProductName)" Title="Product Name" Width="300px" /> | ||
<GridColumn Field="@nameof(SampleData.UnitsInStock)" Title="In stock" Width="100px" /> | ||
<GridColumn Field="@nameof(SampleData.Price)" Title="Unit Price" Width="200px" /> | ||
<GridColumn Field="@nameof(SampleData.Discontinued)" Title="Discontinued" Width="100px" /> | ||
<GridColumn Field="@nameof(SampleData.FirstReleaseDate)" Title="Release Date" Width="300px" /> | ||
</GridColumns> | ||
</TelerikGrid> | ||
@code { | ||
private List<SampleData> GridData { get; set; } | ||
private bool ExportAllPages { get; set; } | ||
private async Task OnExcelAfterExport(GridAfterExcelExportEventArgs args) | ||
{ | ||
var bytes = args.Stream.ToArray(); | ||
var excelStream = new MemoryStream(bytes); | ||
var export = new MemoryStream(); | ||
//define the desired styling - https://docs.telerik.com/devtools/document-processing/libraries/radspreadstreamprocessing/features/cell-styles | ||
SpreadCellFormat markedCell = new SpreadCellFormat() | ||
{ | ||
Fill = SpreadPatternFill.CreateSolidFill(new SpreadColor(230, 238, 223)), | ||
IsBold = true | ||
}; | ||
//import the stream to modify it and then export it - see https://docs.telerik.com/devtools/document-processing/libraries/radspreadstreamprocessing/model/workbook | ||
using (excelStream) | ||
{ | ||
using (IWorkbookImporter workBookImporter = SpreadImporter.CreateWorkbookImporter(SpreadDocumentFormat.Xlsx, excelStream)) | ||
using (IWorkbookExporter workbookExporter = SpreadExporter.CreateWorkbookExporter(SpreadDocumentFormat.Xlsx, export)) | ||
{ | ||
//loop through the sheets to copy their content | ||
foreach (IWorksheetImporter worksheetImporter in workBookImporter.WorksheetImporters) | ||
{ | ||
using (var worksheetExporter = workbookExporter.CreateWorksheetExporter(worksheetImporter.Name)) | ||
{ | ||
//this sets fixed width to all columns. If you need to autofit the content, use the CellContentSizeHelper - https://docs.telerik.com/devtools/document-processing/libraries/radspreadstreamprocessing/features/text-measuring | ||
ntacheva marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
for (int i = 0; i < 5; i++) | ||
{ | ||
var column = worksheetExporter.CreateColumnExporter(); | ||
column.SetWidthInPixels(100); | ||
column.Dispose(); | ||
} | ||
int lastRow = 0; | ||
//loop through the rows to copy their content | ||
foreach (IRowImporter rowImporter in worksheetImporter.Rows) | ||
{ | ||
int rowDifference = rowImporter.RowIndex - lastRow; | ||
//this part ensures the proper cells order in case there are any empty rows | ||
if (rowDifference > 1) | ||
{ | ||
worksheetExporter.SkipRows(rowDifference - 1); | ||
} | ||
int lastColumn = 0; | ||
using (var rowExporter = worksheetExporter.CreateRowExporter()) | ||
{ | ||
//loop through the cells to copy their content | ||
foreach (ICellImporter cellImporter in rowImporter.Cells) | ||
{ | ||
string value = cellImporter.Value; | ||
if (string.IsNullOrEmpty(value)) | ||
{ | ||
continue; | ||
} | ||
var valueType = cellImporter.ValueType; | ||
var importedFormat = new SpreadCellFormat(); | ||
if (cellImporter.Format != null) | ||
{ | ||
importedFormat = cellImporter.Format; | ||
} | ||
int cellDifference = cellImporter.ColumnIndex - lastColumn; | ||
//this part ensures the proper cells order in case there are any empty cells | ||
if (cellDifference > 1) | ||
{ | ||
rowExporter.SkipCells(cellDifference - 1); | ||
} | ||
//check the cell value to apply proper formatting | ||
using (var cellExporter = rowExporter.CreateCellExporter()) | ||
{ | ||
switch (valueType) | ||
{ | ||
case CellValueType.Boolean: | ||
var boolValue = bool.Parse(value); | ||
cellExporter.SetValue(boolValue); | ||
break; | ||
case CellValueType.Number: | ||
var dateNumberValue = double.Parse(value); | ||
cellExporter.SetFormat(importedFormat); | ||
cellExporter.SetValue(dateNumberValue); | ||
break; | ||
case CellValueType.Error: | ||
case CellValueType.Text: | ||
case CellValueType.SharedString: | ||
case CellValueType.RichText: | ||
cellExporter.SetValue(value); | ||
break; | ||
} | ||
//apply the defined style to the desired cells | ||
if (cellImporter.RowIndex == 0) | ||
{ | ||
cellExporter.SetFormat(markedCell); | ||
} | ||
} | ||
lastColumn = cellImporter.ColumnIndex; | ||
} | ||
} | ||
lastRow = rowImporter.RowIndex; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
//pass the modified stream to the event arguments | ||
args.Stream = export; | ||
} | ||
protected override void OnInitialized() | ||
{ | ||
GridData = Enumerable.Range(1, 100).Select(x => new SampleData | ||
{ | ||
ProductId = x, | ||
ProductName = "Product " + x, | ||
UnitsInStock = x * 2, | ||
Price = 3.14159m * x, | ||
Discontinued = x % 4 == 0, | ||
FirstReleaseDate = DateTime.Now.AddDays(-x) | ||
}).ToList(); | ||
} | ||
public class SampleData | ||
{ | ||
public int ProductId { get; set; } | ||
public string ProductName { get; set; } | ||
public int UnitsInStock { get; set; } | ||
public decimal Price { get; set; } | ||
public bool Discontinued { get; set; } | ||
public DateTime FirstReleaseDate { get; set; } | ||
} | ||
} | ||
```` | ||
| ||
## See Also | ||
| ||
* [Custom cell formatting of the exported file with RadSpreadProcessing]({%slug grid-kb-custom-cell-formatting-with-radspreadprocessing%}) | ||
* [Format numbers and dates in the exported CSV file from the Grid]({%slug grid-kb-number-formatting-of-the-csv-export%}) | ||
* [Change the default CSV delimiter (comma) during Grid export]({%slug grid-kb-csv-export-change-field-delimiter%}) |
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.