You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: knowledge-base/grid-csv-export-change-field-delimiter.md
+53-55Lines changed: 53 additions & 55 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -34,29 +34,24 @@ A possible option is to manually modify the exported CSV file before it reaches
34
34
35
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
36
37
-
To change the field delimiter, do the following:
37
+
To change the CSV value delimiter, do the following:
38
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.
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
-
<divclass="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.
//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());
90
85
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.
94
87
CsvFormatProvider formatProvider = new CsvFormatProvider();
95
88
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));
98
96
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();
101
99
102
-
//set the desired delimiter
100
+
//Set the desired new CSV delimiter.
103
101
formatProvider.Settings.Delimiter = ';';
104
102
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));
107
105
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;
110
108
}
111
109
112
-
private List<SampleData> GridData { get; set; }
110
+
private List<Product>? GridData { get; set; }
113
111
114
112
private bool ExportAllPages { get; set; }
115
113
116
114
protected override void OnInitialized()
117
115
{
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
0 commit comments