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

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

Using XlsFormatProvider

XlsFormatProvider makes it easy to import and export to XLS (Excel 97-2003 Workbook) files. The functionality is available since R3 2020.

The XlsFormatProvider requires references to the following assembly:

  • Telerik.Windows.Documents.Spreadsheet.FormatProviders.Xls.dll for .NET Framework projects
  • Telerik.Documents.Spreadsheet.FormatProviders.Xls.dll for .NET Standard projects

Once you reference the aforementioned assemblies, you need to create an instance of the XlsFormatProvider in order 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 the Telerik.Windows.Documents.Spreadsheet.FormatProviders. Depending on the whether you would like to work with the concrete class or the interface, you would need to include either the first or both namespaces.

For more examples and application scenarios of Importing and Exporting a Workbook to various formats using a FormatProvider check out the Import/Load and Export/Save RadSpreadProcessing Workbook knowledge base article.

Import

Example 1 shows how to import an XLS file using a FileStream. The code assures that a file with the specified name exists. Further, the sample instantiates an XlsFormatProvider 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; 
IWorkbookFormatProvider formatProvider = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.Xls.XlsFormatProvider(); 
 
using (Stream input = new FileStream(fileName, FileMode.Open)) 
{ 
    workbook = formatProvider.Import(input); 
} 

Export

Example 2 demonstrates how to export an existing Workbook to an XLS file. The snippet creates a new workbook with a single worksheet. Further, the example creates an XlsFormatProvider and invokes its Export() method. Note that 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.IWorkbookFormatProvider formatProvider = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.Xls.XlsFormatProvider(); 
 
using (Stream output = new FileStream(fileName, FileMode.Create)) 
{ 
    formatProvider.Export(workbook, output); 
} 

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.IWorkbookFormatProvider formatProvider = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.Xls.XlsFormatProvider(); 
 
byte[] bytes; 
using (MemoryStream output = new MemoryStream()) 
{ 
    formatProvider.Export(workbook, output); 
    bytes = output.ToArray(); 
} 

*This documentation is neither affiliated with, nor authorized, sponsored, or approved by, Microsoft Corporation.

See Also

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