2X faster development
The ultimate WinForms UI toolkit to boost your development speed.
Syncfusion .NET Excel library supports preserving macros from input Excel files and saves them as macro enabled files. However, it does not support creation of Excel files with macros or VBA. Steps to open and save Excel files with macros: Step 1: Create a macro using Microsoft Excel application and save the file. Step 2: Open the saved file using Syncfusion Essential XlsIO.
C# using Syncfusion.XlsIO;
VB.NET Imports Syncfusion.XlsIO
C# //Create an instance of ExcelEngine using (ExcelEngine excelEngine = new ExcelEngine()) { //Instantiate the Excel application object IApplication application = excelEngine.Excel; //Set the application default version application.DefaultVersion = ExcelVersion.Excel2016; //Load an existing Excel macro workbook into IWorkbook IWorkbook workbook = application.Workbooks.Open("../../Sample.xlsm"); //Get the first worksheet in the workbook into IWorksheet IWorksheet worksheet = workbook.Worksheets[0]; //Modify the cell content in the worksheet worksheet.Range["A2"].Text = "Cell content modified using XlsIO"; worksheet.Range["A2"].CellStyle.Font.Bold = true; //Save the Excel document workbook.SaveAs("Output.xlsm"); }
VB.NET 'Create an instance of ExcelEngine Using excelEngine As ExcelEngine = New ExcelEngine() 'Instantiate the Excel application object Dim application As IApplication = excelEngine.Excel 'Set the application default version application.DefaultVersion = ExcelVersion.Excel2016 'Load an existing Excel macro workbook into IWorkbook Dim workbook As IWorkbook = application.Workbooks.Open("../../Sample.xlsm") 'Get the first worksheet in the workbook into IWorksheet Dim worksheet As IWorksheet = workbook.Worksheets(0) 'Modify the cell content in the worksheet worksheet.Range("A2").Text = "Cell content modified using XlsIO" worksheet.Range("A2").CellStyle.Font.Bold = True 'Save the Excel document workbook.SaveAs("Output.xlsm") End Using
A complete working sample to open a macro enabled Excel document and resaving it with some changes, can be downloaded from Open and Save Macro Enabled Excel file.zip. Note: Support for editing macros in Excel document will be provided in our future releases.
|
2X faster development
The ultimate WinForms UI toolkit to boost your development speed.
This page will automatically be redirected to the sign-in page in 10 seconds.