Excel for .NET supports loading and saving OpenXMLMacro file format. The macro-enabled Excel files (XLSM) written in Visual Basic Application (VBA) language can now be loaded and saved in C1Excel. XLSM is Excel Macro-enabled spreadsheet devised by Microsoft Excel. It contains embedded macros programmed in VBA code. XLSM files are similar to XLSX files, but with macros enabled.
The following code provides an example of how to load template xls file with VBA macro and save the files in XLSM format:
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Imports C1.C1Excel
|
To write code in C#
C# |
Copy Code
|
---|---|
using C1.C1Excel;
|
Add the following code to the btnLoad_Click event to save the workbook in .xlsm format:
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
' copy the book Dim xlsFileName As String = "..\..\XlsxTestBin.xlsm" C1XLBook1.Load(xlsFileName) C1XLBook1.Sheets(0)(0, 0).Value = "It is copy!" xlsFileName = xlsFileName.Replace(".xlsm", "Modified.xlsm") C1XLBook1.Save(xlsFileName) ' run Excel file System.Diagnostics.Process.Start(xlsFileName) |
To write code in C#
C# |
Copy Code
|
---|---|
// copy the book string xlsFileName = "..\\..\\XlsxTestBin.xlsm"; c1XLBook1.Load(xlsFileName); c1XLBook1.Sheets[0][0, 0].Value = "It is copy!"; xlsFileName = xlsFileName.Replace(".xlsm", "Modified.xlsm"); c1XLBook1.Save(xlsFileName); // run Excel file System.Diagnostics.Process.Start(xlsFileName); |
Press F5 to run the project and view the .xslm file, similar to the one given below: