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, xlsx, xls file formats and DataTable objects.

To import a workbook, you need to instantiate a specific FormatProvider, invoke its Import() method and pass a Stream or byte[] array as an argument.

Example 2 uses a WebClient to download a xlsx file stored on a server. Further, the code creates a XlsxFormatProvider object and invokes its public Workbook Import(Stream stream) 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)); 

Additional examples about import are available in the Import/Load and Export/Save RadSpreadProcessing Workbook knowledge base article.

Save a Workbook

RadSpreadProcessing also allows you to save a workbook into a XLSX, XLS, CSV, TXT, and PDF file formats as well as into a DataTable object.

To export a workbook, you need to instantiate the FormatProvider 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 object and invokes its public void Export(Workbook workbook, Stream output). 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(); 
workbook.Worksheets.Add(); 
 
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(); 
workbook.Worksheets.Add(); 
 
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); 
    } 
} 

Additional examples about export are available in the Import/Load and Export/Save RadSpreadProcessing Workbook knowledge base article.

See Also

In this article
Not finding the help you need? Improve this article