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

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

Using XlsmFormatProvider

XlsmFormatProvider makes it easy to import and export Xlsm (Excel Workbook that supports Macros) files. An Xlsm 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.

Currently the Macros are only preserved during import and export. They cannot be executed or changed in the code.

Unlike the CSV and TXT format providers, the XlsmFormatProvider 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 XlsmFormatProvider in order to import and export Xlsm (Excel Workbook) files. This provider appears in the Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsm namespace. XlsmFormatProvider 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 Xlsm file using a FileStream. The code assures that a file with the specified name exists. Further, the sample instantiates an XlsmFormatProvider and passes a FileStream to its Import() method.

Example 1: Import Xlsm (Excel Workbook) file

string fileName = "SampleFile.Xlsm"; 
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.Xlsm.XlsmFormatProvider(); 
 
using (Stream input = new FileStream(fileName, FileMode.Open)) 
{ 
    workbook = formatProvider.Import(input); 
} 

Export

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

Exporting worbook created with RadSpreadProcessing will result in a file with empty Macros (VBA Project).

Example 2: Export spreadsheet document to Xlsm (Excel Workbook) file

Telerik.Windows.Documents.Spreadsheet.Model.Workbook workbook = new Telerik.Windows.Documents.Spreadsheet.Model.Workbook(); 
workbook.Worksheets.Add(); 
string fileName = "SampleFile.Xlsm"; 
 
 
Telerik.Windows.Documents.Spreadsheet.FormatProviders.IWorkbookFormatProvider formatProvider = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsm.XlsmFormatProvider(); 
 
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.OpenXml.Xlsm.XlsmFormatProvider(); 
 
byte[] bytes; 
using (MemoryStream output = new MemoryStream()) 
{ 
    formatProvider.Export(workbook, output); 
    bytes = output.ToArray(); 
} 

See Also

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