Skip to content

Commit 6c6c85e

Browse files
authored
Merge pull request #655 from telerik/new-kb-spreadprocessing-import-export-csv-formatting-617e0aec524946d2a30c1e846c8109fa
Added new kb article spreadprocessing-import-export-csv-formatting
2 parents d3f8855 + 228d051 commit 6c6c85e

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed
12 KB
Loading
11.4 KB
Loading
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
title: Importing and Exporting CSV Files while changing their formatting in Telerik SpreadProcessing
3+
description: Learn how to handle culture settings, delimiters, decimal separators, and date formats while importing and exporting CSV files in Telerik SpreadProcessing.
4+
type: how-to
5+
page_title: Changing Formatting While Importing and Exporting CSV Files in SpreadProcessing
6+
meta_title: Changing Formatting While Importing and Exporting CSV Files in SpreadProcessing
7+
slug: spreadprocessing-import-export-csv-formatting
8+
tags: telerik, document, processing, spread, csv, culture, delimiter, decimal, separator, formatting, export, import
9+
res_type: kb
10+
ticketid: 1700417
11+
---
12+
13+
## Environment
14+
15+
| Version | Product | Author |
16+
| --- | --- | ---- |
17+
| 2025.3.806 | RadSpreadProcessing |[Yoan Karamanov](https://www.telerik.com/blogs/author/yoan-karamanov)|
18+
19+
## Description
20+
21+
This article shows how to import a CSV file and export it with different formatting (delimiters, decimal separators, and date formats) back to CSV. You need to account for culture settings and delimiters during both import and export operations to ensure the desired formatting results.
22+
23+
This knowledge base article also answers the following questions:
24+
- How to change culture settings during CSV import/export in SpreadProcessing?
25+
- How to use different delimiters and formats for CSV operations?
26+
- How to format date and numeric values during CSV export?
27+
28+
## Solution
29+
30+
Example input data (*a comma (",") as the delimiter and a dot (".") as the decimal separator*):
31+
32+
![Input CSV](images/import-export-csv-formatting-input.png)
33+
34+
Example result data (*a semicolon (";") as the delimiter, a comma (",") as the decimal separator, and a formatted date*):
35+
36+
![Output CSV](images/import-export-csv-formatting-output.png)
37+
38+
To import and process the input file correctly you must:
39+
* Set the **Delimiter** property of the [CsvFormatProvider Settings]({%slug radspreadprocessing-formats-and-conversion-csv-settings%}) to a comma (",")
40+
* Set the [culture]({%slug radspreadprocessing-features-setting-the-culture%}) to English ("en-EN"), since its default decimal separator is a dot (".") and must match the file decimal separator
41+
42+
Once the document is imported and parsed, you can:
43+
* Switch to a culture that has a comma (",") as its default decimal separator (e.g German - "de-DE")
44+
* Set a new [Number Format]({%slug radspreadprocessing-features-number-formats%}) with a comma ("#,##") to the number values
45+
* Set the **Delimiter** property of the [CsvFormatProvider Settings]({%slug radspreadprocessing-formats-and-conversion-csv-settings%}) to a semicolon (";")
46+
47+
### Full Code Example
48+
49+
```csharp
50+
using System.Globalization;
51+
using System.IO;
52+
using Telerik.Windows.Documents.Spreadsheet.Formatting;
53+
using Telerik.Windows.Documents.Spreadsheet.Model;
54+
using Telerik.Windows.Documents.Spreadsheet.FormatProviders.Csv;
55+
56+
Telerik.Windows.Documents.Spreadsheet.Formatting.FormatHelper.CultureHelper = new SpreadsheetCultureHelper(new CultureInfo("en-EN"));
57+
58+
Workbook workbook;
59+
CsvFormatProvider formatProvider = new CsvFormatProvider();
60+
formatProvider.Settings.Delimiter = ',';
61+
62+
using (Stream input = new FileStream("input.csv", FileMode.Open))
63+
{
64+
workbook = formatProvider.Import(input, TimeSpan.FromSeconds(10));
65+
}
66+
67+
Telerik.Windows.Documents.Spreadsheet.Formatting.FormatHelper.CultureHelper = new SpreadsheetCultureHelper(new CultureInfo("de-DE"));
68+
69+
var worksheet = workbook.Worksheets[0];
70+
71+
var currencyFormat = new CellValueFormat("#,##");
72+
ColumnSelection surchargePercent = worksheet.Columns[1];
73+
surchargePercent.SetFormat(currencyFormat);
74+
75+
ColumnSelection surchargePercentNew = worksheet.Columns[2];
76+
surchargePercentNew.SetFormat(currencyFormat);
77+
78+
var dateFormat = new CellValueFormat("dd.mm.yyyy");
79+
ColumnSelection date = worksheet.Columns[0];
80+
date.SetFormat(dateFormat);
81+
82+
formatProvider.Settings.Delimiter = ';';
83+
84+
string fileName = "output.csv";
85+
File.Delete(fileName);
86+
using (Stream output = new FileStream(fileName, FileMode.Create))
87+
{
88+
formatProvider.Export(workbook, output, TimeSpan.FromSeconds(10));
89+
}
90+
```
91+
92+
## See Also
93+
94+
* [SpreadProcessing]({%slug radspreadprocessing-overview%})
95+
* [CsvFormatProvider]({%slug radspreadprocessing-formats-and-conversion-csv-csvformatprovider%})

0 commit comments

Comments
 (0)