|
| 1 | +--- |
| 2 | +title: Importing and Exporting CSV Files while changing their formatting in Telerik SpreadProcessing |
| 3 | +description: Learn how to handle culture settings, delimiters, and decimal separators 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 | +<table> |
| 16 | +<tbody> |
| 17 | +<tr> |
| 18 | +<td> Product </td> |
| 19 | +<td> SpreadProcessing for Telerik Document Processing </td> |
| 20 | +</tr> |
| 21 | +<tr> |
| 22 | +<td> Version </td> |
| 23 | +<td> 11.1.1 </td> |
| 24 | +</tr> |
| 25 | +</tbody> |
| 26 | +</table> |
| 27 | + |
| 28 | +## Description |
| 29 | + |
| 30 | +I want to import a CSV file and export its values to another CSV file while applying specific formatting during the process. I need to account for culture settings and delimiters during both import and export operations to ensure the desired formatting results. |
| 31 | + |
| 32 | +This knowledge base article also answers the following questions: |
| 33 | +- How to change culture settings during CSV import/export in SpreadProcessing? |
| 34 | +- How to use different delimiters and formats for CSV operations? |
| 35 | +- How to format date and numeric values during CSV export? |
| 36 | + |
| 37 | +## Solution |
| 38 | + |
| 39 | +To import and export a CSV file with custom formatting, follow these steps: |
| 40 | + |
| 41 | +1. Set the culture settings to English (en-EN) and the delimiter to a comma (",") for importing the CSV file. |
| 42 | +2. Import the CSV file using the `CsvFormatProvider` and parse the workbook. |
| 43 | +3. Switch the culture settings to German (de-DE), the delimiter to a semicolon (";"), and apply the desired formatting. |
| 44 | +4. Export the formatted workbook to a new CSV file. |
| 45 | + |
| 46 | +### Full Code Example |
| 47 | + |
| 48 | +```csharp |
| 49 | +using System.Globalization; |
| 50 | +using System.IO; |
| 51 | +using Telerik.Windows.Documents.Spreadsheet.Formatting; |
| 52 | +using Telerik.Windows.Documents.Spreadsheet.Model; |
| 53 | +using Telerik.Windows.Documents.Spreadsheet.FormatProviders.Csv; |
| 54 | + |
| 55 | +Telerik.Windows.Documents.Spreadsheet.Formatting.FormatHelper.CultureHelper = new SpreadsheetCultureHelper(new CultureInfo("en-EN")); |
| 56 | + |
| 57 | +Workbook workbook; |
| 58 | +CsvFormatProvider formatProvider = new CsvFormatProvider(); |
| 59 | +formatProvider.Settings.Delimiter = ','; |
| 60 | + |
| 61 | +using (Stream input = new FileStream("..\\..\\..\\input.csv", FileMode.Open)) |
| 62 | +{ |
| 63 | + workbook = formatProvider.Import(input, TimeSpan.FromSeconds(10)); |
| 64 | +} |
| 65 | + |
| 66 | +Telerik.Windows.Documents.Spreadsheet.Formatting.FormatHelper.CultureHelper = new SpreadsheetCultureHelper(new CultureInfo("de-DE")); |
| 67 | + |
| 68 | +var worksheet = workbook.Worksheets[0]; |
| 69 | + |
| 70 | +var currencyFormat = new CellValueFormat("#,##"); |
| 71 | +ColumnSelection surchargePercent = worksheet.Columns[1]; |
| 72 | +surchargePercent.SetFormat(currencyFormat); |
| 73 | + |
| 74 | +ColumnSelection surchargePercentNew = worksheet.Columns[2]; |
| 75 | +surchargePercentNew.SetFormat(currencyFormat); |
| 76 | + |
| 77 | +var dateFormat = new CellValueFormat("dd.mm.yyyy"); |
| 78 | +ColumnSelection date = worksheet.Columns[0]; |
| 79 | +date.SetFormat(dateFormat); |
| 80 | + |
| 81 | +formatProvider.Settings.Delimiter = ';'; |
| 82 | + |
| 83 | +string fileName = "..\\..\\..\\output.csv"; |
| 84 | +File.Delete(fileName); |
| 85 | +using (Stream output = new FileStream(fileName, FileMode.Create)) |
| 86 | +{ |
| 87 | + formatProvider.Export(workbook, output, TimeSpan.FromSeconds(10)); |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +### Key Notes: |
| 92 | +- Modify `SpreadsheetCultureHelper` settings before and after importing the file to ensure proper parsing and formatting. |
| 93 | +- Use `CsvFormatProvider.Settings.Delimiter` to set the appropriate delimiter for import and export. |
| 94 | +- Apply formatting such as `CellValueFormat` to columns before exporting the workbook. |
| 95 | + |
| 96 | +## See Also |
| 97 | + |
| 98 | +- [SpreadProcessing Overview](https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/overview) |
| 99 | +- [CsvFormatProvider Documentation](https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/features/import-export/csv) |
| 100 | +- [Formatting Cells in SpreadProcessing](https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/features/cells/formatting-cells) |
0 commit comments