Available for: UI for ASP.NET MVC | UI for ASP.NET AJAX | UI for Blazor | UI for WPF | UI for WinForms | UI for Silverlight | UI for Xamarin | UI for WinUI | UI for ASP.NET Core

New to Telerik Document Processing? Download free 30-day trial

Create, Open and Save Workbooks

You have the possibility to create workbooks from scratch, open existing files as workbooks and save workbooks into different file formats. This article aims to help you get familiar with these operations.

Create a Workbook

The fact that RadSpreadProcessing is completely decoupled from UI enables numerous server side scenarios that build a document from scratch. The model allows you to instantiate a new workbook using the nullary constructor of the Workbook class. Note that when a new workbook is created in this manner its Worksheet's collection is still empty. Example 1 creates a new workbook and adds its first worksheet, which also becomes the ActiveWorksheet of the workbook.

Example 1: Create a workbook and add a worksheet to it

Telerik.Windows.Documents.Spreadsheet.Model.Workbook workbook = new Telerik.Windows.Documents.Spreadsheet.Model.Workbook(); 
Telerik.Windows.Documents.Spreadsheet.Model.Worksheet worksheet = workbook.Worksheets.Add(); 

Open a Workbook

RadSpreadProcessing allows you to easily import a workbook from a number of formats. Currently, the model supports csv, txt and xlsx formats. To import a workbook, you need to instantiate a specific format provider, invoke its Import() method and pass a stream as an argument. Example 2 uses a WebClient to download a csv file stored on a server. Further, the code creates a CsvFormatProvider object and invokes its Import() method.

Example 2: Download and import xlsx file

const string FilePath = @"http://localhost:54352/Resourses/SampleFile.xlsx"; 
WebClient webClient = new WebClient(); 
webClient.OpenReadCompleted += (sender, eventArgs) => 
    Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.XlsxFormatProvider formatProvider = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.XlsxFormatProvider(); 
    Telerik.Windows.Documents.Spreadsheet.Model.Workbook workbook = formatProvider.Import(eventArgs.Result); 
webClient.OpenReadAsync(new Uri(FilePath)); 

More information about import is available in the Formats and Conversion section.

Save a Workbook

RadSpreadProcessing also allows you to save a workbook into a XLSX, XLS, CSV, TXT, and PDF formats. To export a workbook, you need to instantiate the format provider you would like to use and invoke its Export() method. Example 3 demonstrates how to export an existing Workbook to a XLSX file. The snippet creates a new workbook with a single worksheet. Further, it creates a XlsxFormatProvider and invokes its Export() method. Saving to the other formats is done in the same way, using a different format provider class.

Example 3: Save XLSX file

Telerik.Windows.Documents.Spreadsheet.Model.Workbook workbook = new Telerik.Windows.Documents.Spreadsheet.Model.Workbook(); 
string fileName = "SampleFile.xlsx"; 
IWorkbookFormatProvider formatProvider = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.XlsxFormatProvider(); 
using (Stream output = new FileStream(fileName, FileMode.Create)) 
    formatProvider.Export(workbook, output); 

For security purposes accessing files in Silverlight can be achieved only through user-initiated dialogs. That said, to save workbook's contents into a csv file, you need to use the SaveFileDialog. Example 4 passes the stream returned by the dialog and the current workbook to the Export() method of the CsvFormatProvider.

Example 4: Save csv file using SaveFileDialog

Telerik.Windows.Documents.Spreadsheet.Model.Workbook workbook = new Telerik.Windows.Documents.Spreadsheet.Model.Workbook(); 
SaveFileDialog saveFileDialog = new SaveFileDialog(); 
Telerik.Windows.Documents.Spreadsheet.FormatProviders.TextBased.Csv.CsvFormatProvider formatProvider = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.TextBased.Csv.CsvFormatProvider(); 
saveFileDialog.Filter = "CSV (comma delimited) (.csv)|.csv|All Files (.)|."; 
if (saveFileDialog.ShowDialog() == true) 
    using (Stream output = saveFileDialog.OpenFile()) 
        formatProvider.Export(workbook, output); 
More information about export is available in the Formats and Conversion section.
In this article
Not finding the help you need? Improve this article