Skip to content

Commit 1604630

Browse files
authored
kb(Grid): Update CSV export KB with more delimiter information (#3256)
* kb(Grid): Update CSV export KB with more delimiter information * polishing
1 parent 4ced597 commit 1604630

File tree

1 file changed

+68
-70
lines changed

1 file changed

+68
-70
lines changed

knowledge-base/grid-csv-export-change-field-delimiter.md

Lines changed: 68 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -6,57 +6,53 @@ page_title: Grid export to CSV - change the default field delimiter (comma)
66
slug: grid-kb-csv-export-change-field-delimiter
77
position:
88
tags: grid, export, csv, field, delimiter, change, semicolon, comma, custom
9-
ticketid: 1576816
9+
ticketid: 1576816, 1699366
1010
res_type: kb
1111
---
1212

1313
## Environment
14+
1415
<table>
15-
<tbody>
16-
<tr>
17-
<td>Product</td>
18-
<td>Grid for Blazor</td>
19-
</tr>
20-
</tbody>
16+
<tbody>
17+
<tr>
18+
<td>Product</td>
19+
<td>Grid for Blazor</td>
20+
</tr>
21+
</tbody>
2122
</table>
2223

23-
2424
## Description
25+
2526
I want to change the default field delimiter when exporting the Grid to CSV file. How to set a different list separator?
2627

2728
I want to use a semicolon field delimiter for the exported CSV Grid instead of comma. How to achieve this?
2829

29-
>tip At the time of writing (**UI for Blazor 3.5.0**), the Blazor Grid [does not expose a built-in option for configuring the field delimiter](https://feedback.telerik.com/blazor/1577167-option-to-configure-the-field-delimiter-when-exporting-to-csv). You may vote for the enhancement and follow it to receive status updates.
30+
>tip The Blazor Grid [does not expose a built-in option for setting the CSV delimiter](https://feedback.telerik.com/blazor/1577167-option-to-configure-the-field-delimiter-when-exporting-to-csv). You can vote for the enhancement and follow it to receive status updates. It depends on a [feature of RadSpreadProcessing](https://feedback.telerik.com/document-processing/1356286-spreadstreamprocessing-implement-settings-for-changing-the-delimiter-and-quote-when-exporting-to-csv).
3031
3132
## Solution
3233

33-
A possible option is to manually modify the exported CSV file before it reaches the client to change the field delimiter.
34-
35-
For that purpose use the [`RadSpreadProcessing`](https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/overview) library - it allows you to create spreadsheets from scratch, modify existing documents or convert between the most common spreadsheet formats. In this case, we will focus on the [`CsvFormatProvider` which exposes setting to configure the field delimiter](https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/formats-and-conversion/csv/settings).
36-
37-
To change the field delimiter, do the following:
38-
39-
1. Install `Telerik.Documents.Spreadsheet.FormatProviders.Xls` package, so you can use the `CsvFormatProvider`
40-
41-
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.
34+
Modify the exported CSV file before it reaches the user to change the field delimiter.
4235

43-
1. Create a `CsvFormatProvider` instance and use it to [import the new `MemoryStream` in a `workbook`](https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/formats-and-conversion/csv/csvformatprovider#import).
36+
Use the [`RadSpreadProcessing`](https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/overview) library. It allows you to create spreadsheets from scratch, modify existing documents or convert between the most common spreadsheet formats. In this case, we will focus on the [`CsvFormatProvider` which exposes setting to configure the field delimiter](https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/formats-and-conversion/csv/settings).
4437

45-
1. Set the desired `Delimiter` through the [settings of the `CsvFormatProvider` instance](https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/formats-and-conversion/csv/settings)
38+
To change the CSV value delimiter:
4639

47-
1. [Export the modified `workbook` to a `MemoryStream`](https://docs.telerik.com/devtools/document-processing/knowledge-base/import-export-save-load-workbook#save-workbook-to-filestream-or-memorystream).
48-
49-
1. Pass that `MemoryStream` to the `args.Stream` of the `GridAfterCsvExportEventArgs`, so that the modifications can be saved to the actual exported file.
50-
51-
<div class="skip-repl"></div>
52-
````RAZOR
53-
@*Customize the field delimiter of the exported CSV file*@
40+
1. Install the `Telerik.Documents.Spreadsheet.FormatProviders.Xls` NuGet package, so you can use the `CsvFormatProvider`.
41+
1. Handle the [Grid `OnAfterExport` event](slug:grid-export-events#onafterexport). 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.
42+
1. Create a `CsvFormatProvider` and [set its `Delimiter` setting to a comma `','`](https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/formats-and-conversion/csv/settings). This is necessary because the delimiter in the exported CSV file is always a comma, while the `CsvFormatProvider` assumes it based on the culture.
43+
1. [Import the new `MemoryStream` to a `Workbook`](https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/formats-and-conversion/csv/csvformatprovider#import).
44+
1. Set the desired new `Delimiter` through the [settings of the `CsvFormatProvider` instance](https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/formats-and-conversion/csv/settings).
45+
1. [Export the modified `Workbook` to a new `MemoryStream`](https://docs.telerik.com/devtools/document-processing/knowledge-base/import-export-save-load-workbook#save-workbook-to-filestream-or-memorystream).
46+
1. Pass that `MemoryStream` to the `Stream` property of the `GridAfterCsvExportEventArgs`, so that the modifications can be saved to the actual exported file.
5447

48+
````RAZOR.skip-repl
5549
@using Telerik.Windows.Documents.Spreadsheet.FormatProviders.TextBased.Csv
5650
@using Telerik.Windows.Documents.Spreadsheet.Model
5751
@using System.IO
5852
59-
<TelerikGrid Data="@GridData" Pageable="true">
53+
<TelerikGrid Data="@GridData"
54+
Pageable="true"
55+
Sortable="true">
6056
6157
<GridToolBarTemplate>
6258
<GridCommandButton Command="CsvExport" Icon="@SvgIcon.FileCsv">Export to CSV</GridCommandButton>
@@ -71,76 +67,78 @@ To change the field delimiter, do the following:
7167
</GridExport>
7268
7369
<GridColumns>
74-
<GridColumn Field="@nameof(SampleData.ProductId)" Title="ID" Width="100px" />
75-
<GridColumn Field="@nameof(SampleData.ProductName)" Title="Product Name" Width="300px" />
76-
<GridColumn Field="@nameof(SampleData.UnitsInStock)" Title="In stock" Width="100px" />
77-
<GridColumn Field="@nameof(SampleData.Price)" Title="Unit Price" Width="200px" />
78-
<GridColumn Field="@nameof(SampleData.Discontinued)" Title="Discontinued" Width="100px" />
79-
<GridColumn Field="@nameof(SampleData.FirstReleaseDate)" Title="Release Date" Width="300px" />
70+
<GridColumn Field="@nameof(Product.Id)" Title="ID" Width="100px" />
71+
<GridColumn Field="@nameof(Product.Name)" Title="Product Name" Width="300px" />
72+
<GridColumn Field="@nameof(Product.Price)" Width="200px" />
73+
<GridColumn Field="@nameof(Product.Quantity)" Width="100px" />
74+
<GridColumn Field="@nameof(Product.ReleaseDate)" Title="Release Date" Width="300px" />
75+
<GridColumn Field="@nameof(Product.Discontinued)" Title="Discontinued" Width="100px" />
8076
</GridColumns>
8177
8278
</TelerikGrid>
8379
8480
@code {
85-
86-
private async Task OnCSVAfterExport(GridAfterCsvExportEventArgs args)
81+
private void OnCSVAfterExport(GridAfterCsvExportEventArgs args)
8782
{
88-
//args.Stream is finalized. The Import() method of the CSVFormatProvider requires a readable stream, so you should copy the stream bytes to a new MemoryStream instance which will be used for the import.
89-
var bytes = args.Stream.ToArray();
83+
//args.Stream is finalized. The Import() method of the CSVFormatProvider requires a readable stream,
84+
//so you should copy the stream bytes to a new MemoryStream for the import.
85+
using MemoryStream importCsvStream = new MemoryStream(args.Stream.ToArray());
9086
91-
var CSVStream = new MemoryStream(bytes);
92-
93-
//create a format provider instance to call the import
87+
//Create a CSV format provider that imports and exports the CSV file.
9488
CsvFormatProvider formatProvider = new CsvFormatProvider();
9589
96-
//import the stream to a workbook
97-
Workbook workbook = formatProvider.Import(CSVStream, new TimeSpan(0, 0, 5));
90+
//The delimiter in the exported CSV file is always a comma,
91+
//while the CsvFormatProvider assumes it based on the culture.
92+
//Set the delimiter explicitly to avoid mismatch.
93+
formatProvider.Settings.Delimiter = ',';
94+
95+
//Import the stream to a Telerik Workbook
96+
Workbook workbook = formatProvider.Import(importCsvStream, new TimeSpan(0, 0, 5));
9897
99-
//create a new MemoryStream to export the modified workbook in
100-
MemoryStream modifiedExport = new MemoryStream();
98+
//Create a new MemoryStream to export the modified Workbook.
99+
using MemoryStream exportCsvStream = new MemoryStream();
101100
102-
//set the desired delimiter
101+
//Set the desired new CSV delimiter.
103102
formatProvider.Settings.Delimiter = ';';
104103
105-
//export the modified workbook to a stream
106-
formatProvider.Export(workbook, modifiedExport);
104+
//Export the modified Workbook.
105+
formatProvider.Export(workbook, exportCsvStream, new TimeSpan(0, 0, 5));
107106
108-
//pass the modified stream to the event arguments
109-
args.Stream = modifiedExport;
107+
//Pass the modified Stream to the OnAfterExport event argument.
108+
args.Stream = exportCsvStream;
110109
}
111110
112-
private List<SampleData> GridData { get; set; }
111+
private List<Product>? GridData { get; set; }
113112
114113
private bool ExportAllPages { get; set; }
115114
116115
protected override void OnInitialized()
117116
{
118-
GridData = Enumerable.Range(1, 100).Select(x => new SampleData
119-
{
120-
ProductId = x,
121-
ProductName = $"Product {x}",
122-
UnitsInStock = x * 2,
123-
Price = 3.14159m * x,
124-
Discontinued = x % 4 == 0,
125-
FirstReleaseDate = DateTime.Now.AddDays(-x)
126-
}).ToList();
117+
GridData = Enumerable.Range(1, 100).Select(x => new Product
118+
{
119+
Id = x,
120+
Name = $"Product {x}",
121+
Quantity = x * 2,
122+
Price = 3.14159m * x,
123+
Discontinued = x % 4 == 0,
124+
ReleaseDate = DateTime.Now.AddDays(-x)
125+
}).ToList();
127126
}
128127
129-
public class SampleData
128+
public class Product
130129
{
131-
public int ProductId { get; set; }
132-
public string ProductName { get; set; }
133-
public int UnitsInStock { get; set; }
134-
public decimal Price { get; set; }
130+
public int Id { get; set; }
131+
public string Name { get; set; } = string.Empty;
132+
public decimal? Price { get; set; }
133+
public int Quantity { get; set; }
135134
public bool Discontinued { get; set; }
136-
public DateTime FirstReleaseDate { get; set; }
135+
public DateTime ReleaseDate { get; set; }
137136
}
138137
}
139138
````
140139

141140
## See Also
142141

143-
* [Format numbers and dates in the exported CSV file from the Grid](slug:grid-kb-number-formatting-of-the-csv-export)
144-
* [Custom cell formatting of the exported file with RadSpreadProcessing](slug:grid-kb-custom-cell-formatting-with-radspreadprocessing)
145-
* [Configure Document Processing Libraries](slug:getting-started-vs-integration-dpl)
146-
142+
* [Format numbers and dates in the exported CSV file from the Grid](slug:grid-kb-number-formatting-of-the-csv-export)
143+
* [Custom cell formatting of the exported file with RadSpreadProcessing](slug:grid-kb-custom-cell-formatting-with-radspreadprocessing)
144+
* [Configure Document Processing Libraries](slug:getting-started-vs-integration-dpl)

0 commit comments

Comments
 (0)