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

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

Using XlsxFormatProvider

XlsxFormatProvider makes it easy to import and export XLSX (Excel Workbook) files. An XLSX file is a group of zipped files that conform to the Office Open XML schema. That said, the format allows you export all parts of a workbook: worksheets, formula values, formatting, hyperlinks, etc.

Unlike the CSV and TXT format providers, the XlsxFormatProvider requires references to the following assemblies:

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

  • Telerik.Windows.Zip.dll

Once you reference the aforementioned assemblies, you need to create an instance of the XlsxFormatProvider in order to import and export XLSX (Excel Workbook) files. This provider appears in the Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx namespace. XlsxFormatProvider 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 xlsx file using a FileStream. The code assures that a file with the specified name exists. Further, the sample instantiates an XlsxFormatProvider and passes a FileStream to its Import() method.

Example 1: Import XLSX (Excel Workbook) file

string fileName = "SampleFile.xlsx"; 
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.OpenXml.Xlsx.XlsxFormatProvider(); 
 
using (Stream input = new FileStream(fileName, FileMode.Open)) 
{ 
    //workbook = formatProvider.Import(input); //This method is obsolete since Q4 2024. 
 
    workbook = formatProvider.Import(input, TimeSpan.FromSeconds(10)); 
} 

Export

Example 2 demonstrates how to export an existing Workbook to an xlsx file. The snippet creates a new workbook with a single worksheet. Further, the example creates an XlsxFormatProvider 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 XLSX (Excel Workbook) file

 Telerik.Windows.Documents.Spreadsheet.Model.Workbook workbook = new Telerik.Windows.Documents.Spreadsheet.Model.Workbook(); 
 workbook.Worksheets.Add(); 
 string fileName = "SampleFile.xlsx"; 
 
 Telerik.Windows.Documents.Spreadsheet.FormatProviders.IWorkbookFormatProvider formatProvider = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.XlsxFormatProvider(); 
 
 using (Stream output = new FileStream(fileName, FileMode.Create)) 
 { 
     //formatProvider.Export(workbook, output); //This method is obsolete since Q4 2024. 
 
     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.IWorkbookFormatProvider formatProvider = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.XlsxFormatProvider(); 
 
byte[] bytes; 
using (MemoryStream output = new MemoryStream()) 
{ 
    //formatProvider.Export(workbook, output); //This method is obsolete since Q4 2024. 
    formatProvider.Export(workbook, output, TimeSpan.FromSeconds(10)); 
    bytes = output.ToArray(); 
} 

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

See Also

In this article