Using XlsFormatProvider
XlsFormatProvider makes it easy to import and export XLS (Excel 97-2003 Workbook) files. The functionality is available starting with R3 2020.
The
XlsFormatProviderrequires references to the following packages:
- Telerik.Windows.Documents.Spreadsheet.FormatProviders.Xls for .NET Framework projects
- Telerik.Documents.Spreadsheet.FormatProviders.Xls for .NET Standard projects
After you reference these packages, create an instance of the XlsFormatProvider to import and export Excel 97-2003 Workbook files. This provider appears in the Telerik.Windows.Documents.Spreadsheet.FormatProviders.Xls namespace. XlsFormatProvider implements the IWorkbookFormatProvider interface, which in turn appears in Telerik.Windows.Documents.Spreadsheet.FormatProviders. Depending on whether you work with the concrete class or the interface, include either the first or both namespaces.
For more examples and application scenarios of importing and exporting a workbook to various formats, see the Import/Load and Export/Save RadSpreadProcessing Workbook knowledge base article.
Import
The following example shows how to import an XLS file through a FileStream. The code verifies that a file with the specified name exists. The sample creates an XlsFormatProvider instance and passes a FileStream to its Import() method.
Example 1: Import XLS (Excel 97-2003 Workbook) File
string fileName = "SampleFile.xls";
if (!File.Exists(fileName))
{
throw new FileNotFoundException(String.Format("File {0} was not found!", fileName));
}
Telerik.Windows.Documents.Spreadsheet.Model.Workbook workbook;
Telerik.Windows.Documents.Spreadsheet.FormatProviders.Xls.XlsFormatProvider formatProvider = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.Xls.XlsFormatProvider();
using (Stream input = new FileStream(fileName, FileMode.Open))
{
workbook = formatProvider.Import(input, TimeSpan.FromSeconds(10));
}
Export
The following example demonstrates how to export an existing workbook to an XLS file. The snippet creates a new workbook with a single worksheet. The example then creates an XlsFormatProvider instance and invokes its Export() method. The Export() method accepts a parameter of type Stream so you can work with any of its inheritors.
Example 2: Export Spreadsheet Document to XLS (Excel 97-2003 Workbook) File
Telerik.Windows.Documents.Spreadsheet.Model.Workbook workbook = new Telerik.Windows.Documents.Spreadsheet.Model.Workbook();
workbook.Worksheets.Add();
string fileName = "SampleFile.xls";
Telerik.Windows.Documents.Spreadsheet.FormatProviders.Xls.XlsFormatProvider formatProvider = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.Xls.XlsFormatProvider();
using (Stream output = new FileStream(fileName, FileMode.Create))
{
formatProvider.Export(workbook, output, TimeSpan.FromSeconds(10));
}
Example 3: Export Spreadsheet Document to a Stream and byte[]
Telerik.Windows.Documents.Spreadsheet.Model.Workbook workbook = new Telerik.Windows.Documents.Spreadsheet.Model.Workbook();
workbook.Worksheets.Add();
Telerik.Windows.Documents.Spreadsheet.FormatProviders.Xls.XlsFormatProvider formatProvider = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.Xls.XlsFormatProvider();
byte[] bytes;
using (MemoryStream output = new MemoryStream())
{
formatProvider.Export(workbook, output, TimeSpan.FromSeconds(10));
bytes = output.ToArray();
}
*This documentation is neither affiliated with, nor authorized, sponsored, or approved by, Microsoft Corporation.