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

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.

[C#] Example 1: Create a workbook and add a worksheet to it

Workbook workbook = new Workbook();
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.

[C#] Example 2: Download and import xlsx file

const string FilePath = @"http://localhost:54352/Resourses/SampleFile.xlsx";
WebClient webClient = new WebClient();

webClient.OpenReadCompleted += (sender, eventArgs) =>
    XlsxFormatProvider formatProvider = new XlsxFormatProvider();
    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 csv, txt and xlsx 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 csv file. The snippet creates a new workbook with a single worksheet. Further, it creates a CsvFormatProvider and invokes its Export() method.

[C#] Example 3: Save 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);

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.

[C#] Example 4: Save csv file using SaveFileDialog

Workbook workbook = new Workbook();

SaveFileDialog saveFileDialog = new SaveFileDialog();
CsvFormatProvider formatProvider = new 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.