Edit this page
Available for: UI for ASP.NET AJAX | UI for ASP.NET MVC | UI for WPF | UI for WinForms | UI for Silverlight

Using XlsxFormatProvider

XlsxFormatProvider makes it easy to import and export xlsx 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 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.

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.

[C#] Example 1: Import Xlsx file

string fileName = "SampleFile.xlsx";
if (!File.Exists(fileName))
{
    throw new FileNotFoundException(String.Format("File {0} was not found!", fileName));
}

Workbook workbook;
IWorkbookFormatProvider formatProvider = new XlsxFormatProvider();

using (Stream input = new FileStream(fileName, FileMode.Open))
{
    workbook = formatProvider.Import(input);
}

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.

[C#] Example 2: Export Xlsx file

Workbook workbook = new Workbook();
workbook.Worksheets.Add();
string fileName = "SampleFile.xlsx";

IWorkbookFormatProvider formatProvider = new XlsxFormatProvider();

using (Stream output = new FileStream(fileName, FileMode.Create))
{
    formatProvider.Export(workbook, output);
}