Friday, 26 September 2014 08:01

Convert XLS to XLSM and Maintain Macro in C#, VB.NET

Written by  support iceblue
Rate this item
(0 votes)

A file with the XLSM extension is an Excel Macro-Enabled Workbook file. For security reasons, XLS file or XLSX file does not enable macros by default. Thus, if you want to execute macros in Excel file, you need to convert XLS or XLSX to XLSM at the first place. In this article, I’ll introduce you how to convert XLS to XLSM with the macro maintained using Spire.XLS.

Here is the method:

Step 1: Create a new instance of Spire.Xls.Workbook class.

Workbook workbook = new Workbook();

Step 2: Load the test file and imports its data to workbook.

workbook.LoadFromFile("test.xls", ExcelVersion.Version97to2003);

Step 3: Save the workbook as a new XLSM file.

workbook.SaveToFile("result.xlsm", FileFormat.Version2007);

Full Code:

[C#]
static void Main(string[] args)
{
    Workbook workbook = new Workbook();
    workbook.LoadFromFile("test.xls", ExcelVersion.Version97to2003);
    workbook.SaveToFile("result.xlsm", FileFormat.Version2007);
}
[VB.NET]
Private Shared Sub Main(args As String())
        Dim workbook As New Workbook()
        workbook.LoadFromFile("test.xls", ExcelVersion.Version97to2003)
        workbook.SaveToFile("result.xlsm", FileFormat.Version2007)
End Sub

Test File:

As is shown in the picture, Excel automatically disables macro in XLS file.

Convert XLS to XLSM and Maintain Macro in C#, VB.NET

Result:

No security warning in the converted XLSM file.

Convert XLS to XLSM and Maintain Macro in C#, VB.NET

Additional Info

  • tutorial_title: Convert XLS to XLSM and Maintain Macro
Last modified on Friday, 26 September 2014 08:06