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

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 XlsFormatProvider requires references to the following assembly:

  • Telerik.Windows.Documents.Spreadsheet.FormatProviders.Xls.dll

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.

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.

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