Other API Samples

Live SpreadsheetGear API Samples

Workbook Saving Excel 2007-2019 Macro-Enabled Open XML (*.xlsm)

This sample demonstrates saving to the Excel 2007-2019 Open XML (*.xlsx) file format using the FileFormat.OpenXMLWorkbookMacroEnabled enumeration option. Note SpreadsheetGear does not support modifying or executing VBA Macros. Existing Macros will be preserved when opened and saved through SpreadsheetGear.

// Create a new workbook and some local variables for convenience.
SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook();
SpreadsheetGear.IWorksheet worksheet = workbook.Worksheets["Sheet1"];
SpreadsheetGear.IRange cells = worksheet.Cells;

// Rename the worksheet.
worksheet.Name = "My Worksheet";

// Add some cell data.
cells["A1"].Value = "Hello World!";

// Save to Excel 2007 and later compatible Macro-Enabled Open XML (*.xlsm) file.
workbook.SaveAs(@"c:\path\to\workbook.xlsx",
    SpreadsheetGear.FileFormat.OpenXMLWorkbookMacroEnabled);
Run This Sample
Download File

Download an Excel 97-2003 (*.xls) file with the results of this sample.