Edit this page
Available for: UI for ASP.NET AJAX | UI for ASP.NET MVC | UI for WPF | UI for WinForms | UI for Silverlight

Using CsvFormatProvider

CsvFormatProvider makes it easy to import and export CSV files. Note that CSV is a plain text format,meaning that it keeps only the contents of the worksheet without its formatting. Exporting a file to CSV strips all styling and saves only cell's result value with the respective format applied. Moreover, it exports only the contents of the active worksheet – no support for exporting multiple worksheets into a csv at once is available.

To import and export csv files, you need to use the CsvFormatProvider class that appears in the Telerik.Windows.Documents.Spreadsheet.FormatProviders.TextBased.Csv namespace. The CsvFormatProvider implements the IWorkbookFormatProvider interface which in turn is contained in the Telerik.Windows.Documents.Spreadsheet.FormatProviders namespace.


Example 1 shows how to import a CSV file using a FileStream. The code assures that a file with the specified name exists. Further, the sample instantiates a CsvFormatProvider and passes a FileStream to its Import() method.

[C#] Example 1: Import CSV file

string fileName = "FileName.csv";
if (!File.Exists(fileName))
    throw new FileNotFoundException(String.Format("File {0} was not found!", fileName));

Workbook workbook;
IWorkbookFormatProvider formatProvider = new CsvFormatProvider();

using (Stream input = new FileStream(fileName, FileMode.Open))
    workbook = formatProvider.Import(input);


Example 2 demonstrates how to export an existing Workbook to a CSV file. The snippet creates a new workbook with a single worksheet. Further, it creates a CsvFormatProvider and invokes its Export() method:

[C#] Example 2: Export CSV file

Workbook workbook = new Workbook();

string fileName = "SampleFile.csv";
IWorkbookFormatProvider formatProvider = new CsvFormatProvider();

using (Stream output = new FileStream(fileName, FileMode.Create))
    formatProvider.Export(workbook, output);