Skip to content

Commit 404ad11

Browse files
dimodidimodi
authored andcommitted
kb(Grid): Update CSV export KB with more delimiter information
1 parent ab7229a commit 404ad11

File tree

1 file changed

+53
-55
lines changed

1 file changed

+53
-55
lines changed

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

Lines changed: 53 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,24 @@ A possible option is to manually modify the exported CSV file before it reaches
3434

3535
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).
3636

37-
To change the field delimiter, do the following:
37+
To change the CSV value delimiter, do the following:
3838

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.
42-
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).
44-
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)
46-
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*@
39+
1. Install the `Telerik.Documents.Spreadsheet.FormatProviders.Xls` NuGet package, so you can use the `CsvFormatProvider`.
40+
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.
41+
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.
42+
1. [Import the new `MemoryStream` to a `Workbook`](https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/formats-and-conversion/csv/csvformatprovider#import).
43+
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).
44+
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).
45+
1. Pass that `MemoryStream` to the `Stream` property of the `GridAfterCsvExportEventArgs`, so that the modifications can be saved to the actual exported file.
5446

47+
````RAZOR.skip-repl
5548
@using Telerik.Windows.Documents.Spreadsheet.FormatProviders.TextBased.Csv
5649
@using Telerik.Windows.Documents.Spreadsheet.Model
5750
@using System.IO
5851
59-
<TelerikGrid Data="@GridData" Pageable="true">
52+
<TelerikGrid Data="@GridData"
53+
Pageable="true"
54+
Sortable="true">
6055
6156
<GridToolBarTemplate>
6257
<GridCommandButton Command="CsvExport" Icon="@SvgIcon.FileCsv">Export to CSV</GridCommandButton>
@@ -71,69 +66,72 @@ To change the field delimiter, do the following:
7166
</GridExport>
7267
7368
<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" />
69+
<GridColumn Field="@nameof(Product.Id)" Title="ID" Width="100px" />
70+
<GridColumn Field="@nameof(Product.Name)" Title="Product Name" Width="300px" />
71+
<GridColumn Field="@nameof(Product.Price)" Width="200px" />
72+
<GridColumn Field="@nameof(Product.Quantity)" Width="100px" />
73+
<GridColumn Field="@nameof(Product.ReleaseDate)" Title="Release Date" Width="300px" />
74+
<GridColumn Field="@nameof(Product.Discontinued)" Title="Discontinued" Width="100px" />
8075
</GridColumns>
8176
8277
</TelerikGrid>
8378
8479
@code {
85-
86-
private async Task OnCSVAfterExport(GridAfterCsvExportEventArgs args)
80+
private void OnCSVAfterExport(GridAfterCsvExportEventArgs args)
8781
{
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();
82+
//args.Stream is finalized. The Import() method of the CSVFormatProvider requires a readable stream,
83+
//so you should copy the stream bytes to a new MemoryStream for the import.
84+
using MemoryStream importCsvStream = new MemoryStream(args.Stream.ToArray());
9085
91-
var CSVStream = new MemoryStream(bytes);
92-
93-
//create a format provider instance to call the import
86+
//Create a CSV format provider that imports and exports the CSV file.
9487
CsvFormatProvider formatProvider = new CsvFormatProvider();
9588
96-
//import the stream to a workbook
97-
Workbook workbook = formatProvider.Import(CSVStream, new TimeSpan(0, 0, 5));
89+
//The delimiter in the exported CSV file is always a comma,
90+
//while the CsvFormatProvider assumes it based on the culture.
91+
//Set the delimiter explicitly to avoid mismatch.
92+
formatProvider.Settings.Delimiter = ',';
93+
94+
//Import the stream to a Telerik Workbook
95+
Workbook workbook = formatProvider.Import(importCsvStream, new TimeSpan(0, 0, 5));
9896
99-
//create a new MemoryStream to export the modified workbook in
100-
MemoryStream modifiedExport = new MemoryStream();
97+
//Create a new MemoryStream to export the modified Workbook
98+
using MemoryStream exportCsvStream = new MemoryStream();
10199
102-
//set the desired delimiter
100+
//Set the desired new CSV delimiter.
103101
formatProvider.Settings.Delimiter = ';';
104102
105-
//export the modified workbook to a stream
106-
formatProvider.Export(workbook, modifiedExport);
103+
//Export the modified Workbook.
104+
formatProvider.Export(workbook, exportCsvStream, new TimeSpan(0, 0, 5));
107105
108-
//pass the modified stream to the event arguments
109-
args.Stream = modifiedExport;
106+
//Pass the modified Stream to the OnAfterExport event argument.
107+
args.Stream = exportCsvStream;
110108
}
111109
112-
private List<SampleData> GridData { get; set; }
110+
private List<Product>? GridData { get; set; }
113111
114112
private bool ExportAllPages { get; set; }
115113
116114
protected override void OnInitialized()
117115
{
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();
116+
GridData = Enumerable.Range(1, 100).Select(x => new Product
117+
{
118+
Id = x,
119+
Name = $"Product {x}",
120+
Quantity = x * 2,
121+
Price = 3.14159m * x,
122+
Discontinued = x % 4 == 0,
123+
ReleaseDate = DateTime.Now.AddDays(-x)
124+
}).ToList();
127125
}
128126
129-
public class SampleData
127+
public class Product
130128
{
131-
public int ProductId { get; set; }
132-
public string ProductName { get; set; }
133-
public int UnitsInStock { get; set; }
134-
public decimal Price { get; set; }
129+
public int Id { get; set; }
130+
public string Name { get; set; } = string.Empty;
131+
public decimal? Price { get; set; }
132+
public int Quantity { get; set; }
135133
public bool Discontinued { get; set; }
136-
public DateTime FirstReleaseDate { get; set; }
134+
public DateTime ReleaseDate { get; set; }
137135
}
138136
}
139137
````

0 commit comments

Comments
 (0)