Skip to content

Commit a532841

Browse files
Grid csv export (#171)
* chore(grid): add csv article prototype * chore(grid): csv export example and unify notes in a template
1 parent 6e8cecb commit a532841

File tree

3 files changed

+182
-76
lines changed

3 files changed

+182
-76
lines changed

_contentTemplates/grid/export.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#export-common-notes
2+
* `bool` fields are exported as `TRUE` or `FALSE` strings, because there is no boolean data type in Excel and these string values are the most common ones used in data and macros.
3+
4+
* Date and number formats are exported with the following format: `mm/dd/yyyy hh:mm:ss` plus the current app culture AM/PM specifier for dates, and `Convert.ToDouble(value)` for numbers (which uses the current thread culture). The Excel date formats are different than .NET date formats and Excel may not always recognize the column as dates, for example, if the entire date format from the .NET culture is used.
5+
6+
* Templates are not exported, because there is no provision in the framework for getting them at runtime. If a column, header or group header/footer has a template or aggregates, it will be ignored. The headers will be the `Title` of the column, the data is the data from the `Field`. If you need additional information, see if you can add it in e Field in the model, or create your own excel file (see example <a href="https://feedback.telerik.com/blazor/1485764-customize-the-excel-file-before-it-gets-to-the-client" target="_blank">here</a>).
7+
8+
* Only columns that have a `Field` set are exported.
9+
10+
* If you are using the `OnRead` event, only the current page of data will be exported, because that's all the grid has at the time of the export action.
11+
12+
* With Server-side Blazor, the file may become larger than the default SignalR connection limit, and this can disconnect the client and result in an error. Generally, this requires quite a lot of data to happen, but you may need to increase the size limit of the connection in the `ConfigureServices` method of your `Startup.cs` file, for example:
13+
14+
**C#**
15+
16+
services.AddServerSideBlazor().AddHubOptions(o =>
17+
{
18+
o.MaximumReceiveMessageSize = 1024 * 1024; // 1MB
19+
});
20+
21+
* With Client-side Blazor (WebAssembly), all the code runs in the browser and, at the time of writing, is considerably slower than server-side Blazor, and it only has one actual thread. This means that while the file is being generated, the UI will be unresponsive, so you may want to show a loading sign to the user through the `OnClick` handler of the command button, something like:
22+
23+
**Component**
24+
25+
@* Exporting a lot of rows can be slow in a WebAssembly app more so than in a server-side app, and it blocks the UI *@
26+
27+
<TelerikGrid Data="@GridData" AutoGenerateColumns="true" Pageable="true">
28+
<GridToolBar>
29+
<GridCommandButton OnClick="@ShowLoadingSign" Command="ExcelExport" Icon="@IconName.FileExcel">Export to Excel</GridCommandButton>
30+
<GridCommandButton OnClick="@ShowLoadingSign" Command="CsvExport" Icon="@IconName.FileCsv">Export to CSV</GridCommandButton>
31+
</GridToolBar>
32+
<GridExport>
33+
<GridExcelExport AllPages="true" FileName="telerik-grid-export" />
34+
<GridCsvExport AllPages="true" FileName="telerik-grid-export" />
35+
</GridExport>
36+
</TelerikGrid>
37+
38+
<TelerikWindow Visible="@isExporting" Modal="true">
39+
<WindowTitle>Please wait...</WindowTitle>
40+
<WindowContent>We are exporting your data, your file will download shortly.</WindowContent>
41+
</TelerikWindow>
42+
43+
@code {
44+
bool isExporting { get; set; }
45+
46+
async Task ShowLoadingSign()
47+
{
48+
isExporting = true;
49+
StateHasChanged();
50+
// This won't work for server-side Blazor, the UI will render immediately after the delay and the loading sign will only flicker
51+
await Task.Delay(50);
52+
isExporting = false;
53+
}
54+
55+
List<SampleData> GridData { get; set; }
56+
57+
protected override void OnInitialized()
58+
{
59+
GridData = Enumerable.Range(1, 1000).Select(x => new SampleData
60+
{
61+
ProductId = x,
62+
ProductName = $"Product {x}",
63+
UnitsInStock = x * 2,
64+
Price = 3.14159m * x,
65+
Discontinued = x % 4 == 0,
66+
FirstReleaseDate = DateTime.Now.AddDays(-x)
67+
}).ToList();
68+
}
69+
70+
public class SampleData
71+
{
72+
public int ProductId { get; set; }
73+
public string ProductName { get; set; }
74+
public int UnitsInStock { get; set; }
75+
public decimal Price { get; set; }
76+
public bool Discontinued { get; set; }
77+
public DateTime FirstReleaseDate { get; set; }
78+
}
79+
}
80+
#end

components/grid/export/csv.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
title: CSV
3+
page_title: Grid - CSV Export
4+
description: Export to CSV the Grid for Blazor.
5+
slug: grid-export-csv
6+
tags: telerik,blazor,grid,export,csv
7+
published: True
8+
position: 1
9+
---
10+
11+
# CSV Export
12+
13+
You can export the grid to CSV with a click of a button. The current filter, sort, page, grouping and column order are applied to the `.csv` document.
14+
15+
When you click the Export button, your browser will receive the resulting file.
16+
17+
18+
## Basics
19+
20+
To enable the grid CSV Export, add a [command button]({%slug components/grid/columns/command%}) with the `CsvExport` command name to the [toolbar]({%slug components/grid/features/toolbar%}).
21+
22+
````
23+
<GridToolBar>
24+
<GridCommandButton Command="CsvExport" Icon="@IconName.FileCsv">Export to CSV</GridCommandButton>
25+
</GridToolBar>
26+
````
27+
28+
Optionally, you can also set the `GridCsvExport` tag settings under the `GridExport` tag to choose:
29+
30+
* `FileName` - the name of the file. The grid will add the `.csv` extension for you.
31+
* `AllPages` - whether to export the current page only, or the entire data from the data source.
32+
33+
>caption Export the Grid to CSV - Example
34+
35+
````CSHTML
36+
@* You can sort, group, filter, page the grid, reorder its columns, and you can click the
37+
Export button to save the current data *@
38+
39+
<TelerikGrid Data="@GridData" Pageable="true" Sortable="true" Reorderable="true"
40+
FilterMode="@GridFilterMode.FilterRow" Groupable="true">
41+
42+
<GridToolBar>
43+
<GridCommandButton Command="CsvExport" Icon="@IconName.FileCsv">Export to CSV</GridCommandButton>
44+
<label><TelerikCheckBox @bind-Value="@ExportAllPages" />Export All Pages</label>
45+
</GridToolBar>
46+
47+
<GridExport>
48+
<GridCsvExport FileName="telerik-grid-export" AllPages="@ExportAllPages" />
49+
</GridExport>
50+
51+
<GridColumns>
52+
<GridColumn Field="@nameof(SampleData.ProductId)" Title="ID" />
53+
<GridColumn Field="@nameof(SampleData.ProductName)" Title="Product Name" />
54+
<GridColumn Field="@nameof(SampleData.UnitsInStock)" Title="In stock" />
55+
<GridColumn Field="@nameof(SampleData.Price)" Title="Unit Price" />
56+
<GridColumn Field="@nameof(SampleData.Discontinued)" Title="Discontinued" />
57+
<GridColumn Field="@nameof(SampleData.FirstReleaseDate)" Title="Release Date" />
58+
</GridColumns>
59+
</TelerikGrid>
60+
61+
@code {
62+
List<SampleData> GridData { get; set; }
63+
bool ExportAllPages { get; set; }
64+
65+
protected override void OnInitialized()
66+
{
67+
GridData = Enumerable.Range(1, 100).Select(x => new SampleData
68+
{
69+
ProductId = x,
70+
ProductName = $"Product {x}",
71+
UnitsInStock = x * 2,
72+
Price = 3.14159m * x,
73+
Discontinued = x % 4 == 0,
74+
FirstReleaseDate = DateTime.Now.AddDays(-x)
75+
}).ToList();
76+
}
77+
78+
public class SampleData
79+
{
80+
public int ProductId { get; set; }
81+
public string ProductName { get; set; }
82+
public int UnitsInStock { get; set; }
83+
public decimal Price { get; set; }
84+
public bool Discontinued { get; set; }
85+
public DateTime FirstReleaseDate { get; set; }
86+
}
87+
}
88+
````
89+
90+
## Notes
91+
92+
The CSV export has the following specifics:
93+
94+
* Column widths are not applied because a CSV document does not have such a concept. You can use any units in the grid itself, they will not be reflected in the exported document. If you need to add such structure, consider [exporting to an Excel file]({%slug grid-export-excel%}).
95+
96+
@[template](/_contentTemplates/grid/export.md#export-common-notes)
97+
98+
## See Also
99+
100+
* [Live Demo: Grid CSV Export](https://demos.telerik.com/blazor-ui/grid/export-csv)
101+

components/grid/export/excel.md

Lines changed: 1 addition & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -91,84 +91,9 @@ Optionally, you can also set the `GridExcelExport` tag settings under the `GridE
9191

9292
The Excel export has the following specifics:
9393

94-
* `bool` fields are exported as `TRUE` or `FALSE` strings, because there is no boolean data type in Excel and these string values are the most common ones used in data and macros.
95-
96-
* Date and number formats are exported with the following format: `mm/dd/yyyy hh:mm:ss` plus the current app culture AM/PM specifier for dates, and `Convert.ToDouble(value)` for numbers (which uses the current thread culture). The Excel date formats are different than .NET date formats and Excel may not always recognize the column as dates, for example, if the entire date format from the .NET culture is used.
97-
98-
* Templates are not exported, because there is no provision in the framework for getting them at runtime. If a column, header or group header/footer has a template or aggregates, it will be ignored. The headers will be the `Title` of the column, the data is the data from the `Field`. If you need additional information, see if you can add it in e Field in the model, or create your own excel file (see example <a href="https://feedback.telerik.com/blazor/1485764-customize-the-excel-file-before-it-gets-to-the-client" target="_blank">here</a>).
99-
100-
* Only columns that have a `Field` set are exported.
101-
10294
* Excel does not understand units different than `px` for the column `Width`, and if you use them (such as `rem` or `%`), it will fail to parse them and will render a collapsed (hidden) column with zero width.
10395

104-
* If you are using the `OnRead` event, only the current page of data will be exported, because that's all the grid has at the time of the export action.
105-
106-
* With Server-side Blazor, the file may become larger than the default SignalR connection limit, and this can disconnect the client and result in an error. Generally, this requires quite a lot of data to happen, but you may need to increase the size limit of the connection in the `ConfigureServices` method of your `Startup.cs` file, for example:
107-
108-
**C#**
109-
110-
services.AddServerSideBlazor().AddHubOptions(o =>
111-
{
112-
o.MaximumReceiveMessageSize = 1024 * 1024; // 1MB
113-
});
114-
115-
* With Client-side Blazor (WebAssembly), all the code runs in the browser and, at the time of writing, is considerably slower than server-side Blazor, and it only has one actual thread. This means that while the file is being generated, the UI will be unresponsive, so you may want to show a loading sign to the user through the `OnClick` handler of the command button, something like:
116-
117-
**Component**
118-
119-
@* Exporting a lot of rows can be slow in a WebAssembly app more so than in a server-side app, and it blocks the UI *@
120-
121-
<TelerikGrid Data="@GridData" AutoGenerateColumns="true" Pageable="true">
122-
<GridToolBar>
123-
<GridCommandButton OnClick="@ShowLoadingSign" Command="ExcelExport" Icon="@IconName.FileExcel">Export to Excel</GridCommandButton>
124-
</GridToolBar>
125-
<GridExport>
126-
<GridExcelExport AllPages="true" FileName="telerik-grid-export" />
127-
</GridExport>
128-
</TelerikGrid>
129-
130-
<TelerikWindow Visible="@isExporting" Modal="true">
131-
<WindowTitle>Please wait...</WindowTitle>
132-
<WindowContent>We are exporting your data, your file will download shortly.</WindowContent>
133-
</TelerikWindow>
134-
135-
@code {
136-
bool isExporting { get; set; }
137-
138-
async Task ShowLoadingSign()
139-
{
140-
isExporting = true;
141-
StateHasChanged();
142-
// This won't work for server-side Blazor, the UI will render immediately after the delay and the loading sign will only flicker
143-
await Task.Delay(50);
144-
isExporting = false;
145-
}
146-
147-
List<SampleData> GridData { get; set; }
148-
149-
protected override void OnInitialized()
150-
{
151-
GridData = Enumerable.Range(1, 1000).Select(x => new SampleData
152-
{
153-
ProductId = x,
154-
ProductName = $"Product {x}",
155-
UnitsInStock = x * 2,
156-
Price = 3.14159m * x,
157-
Discontinued = x % 4 == 0,
158-
FirstReleaseDate = DateTime.Now.AddDays(-x)
159-
}).ToList();
160-
}
161-
162-
public class SampleData
163-
{
164-
public int ProductId { get; set; }
165-
public string ProductName { get; set; }
166-
public int UnitsInStock { get; set; }
167-
public decimal Price { get; set; }
168-
public bool Discontinued { get; set; }
169-
public DateTime FirstReleaseDate { get; set; }
170-
}
171-
}
96+
@[template](/_contentTemplates/grid/export.md#export-common-notes)
17297

17398

17499
## See Also

0 commit comments

Comments
 (0)