Create, Open and Save Workbooks
You can create workbooks from scratch, open existing files as workbooks, and save workbooks into different file formats. The following sections describe 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 create a new workbook using the nullary constructor of the Workbook class. When a new workbook is created in this manner, its Worksheets 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 import a workbook from a number of formats. The model supports csv, txt, xlsx, xls file formats, and DataTable objects.
To import a workbook, 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. The code then 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, TimeSpan.FromSeconds(10));
};
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 format as well as into a DataTable object.
To export a workbook, instantiate the FormatProvider you want 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. It then creates a XlsxFormatProvider object and invokes its public void Export(Workbook workbook, Stream output) method. Saving to the other formats works the same way with a different format provider class.
Example 3: Save XLSX File
Workbook workbook = new Workbook();
workbook.Worksheets.Add();
string fileName = "SampleFile.xlsx";
XlsxFormatProvider formatProvider = new XlsxFormatProvider();
using (Stream output = new FileStream(fileName, FileMode.Create))
{
formatProvider.Export(workbook, output, TimeSpan.FromSeconds(10));
}
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
Workbook workbook = new Workbook();
workbook.Worksheets.Add();
SaveFileDialog saveFileDialog = new SaveFileDialog();
CsvFormatProvider formatProvider = new CsvFormatProvider();
saveFileDialog.Filter = "CSV (comma delimited) (*.csv)|*.csv|All Files (*.*)|*.*";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
using (Stream output = saveFileDialog.OpenFile())
{
formatProvider.Export(workbook, output, TimeSpan.FromSeconds(10));
}
}
Additional examples about export are available in the Import/Load and Export/Save RadSpreadProcessing Workbook knowledge base article.