EasyXLS™ enables you to write Excel files with macros or VBA project.
The first step is to create an XLSM, XLST, XLS or XLSB file having the macro defined. The macro-enabled Excel file can be empty or already set with sheets and some data.
The third step is to add data to Excel file. When the Excel file is completely filled, you can export data to Excel file. For writing an XLSM file, ExcelDocument.easy_WriteXLSXFile method is used, but the exported Excel file must have XLSM file extension. For writing an XLS file macro enabled, ExcelDocument.easy_WriteXLSFile method is used. For writing an XLSB file macro enabled, ExcelDocument.easy_WriteXLSBFile method is used.
If the loaded Excel file contains controls (like button, combo box, check box, list box, so on), the controls are not preserved during the load/write action. The solution is to programmatically create the controls from VBA.
The below example shows how to export data to an XLSM file that includes a VBA project with a possible macro.
// Create an instance of the class that exports Excel files
ExcelDocument workbook = new ExcelDocument();
// Load the XLSM template file with macro
workbook.easy_LoadXLSXFile("C:\\Samples\\Macro.xlsm");
// Add data to Excel file
...
// Export the XLSM file
workbook.easy_WriteXLSXFile("C:\\Samples\\Macro filled with data.xlsm");
' Create an instance of the class that exports Excel filesDim workbook As New ExcelDocument
' Load the XLSM template file with macro
workbook.easy_LoadXLSXFile("C:\Samples\Macro.xlsm")
' Add data to Excel file
...
' Export the XLSM file
workbook.easy_WriteXLSXFile("C:\Samples\Macro filled with data.xlsm")
C++// Create an instance of the class that exports Excel files
EasyXLS::IExcelDocumentPtr workbook;
hr = CoCreateInstance(__uuidof(EasyXLS::ExcelDocument),
NULL,
CLSCTX_ALL,
__uuidof(EasyXLS::IExcelDocument),
(void**) &workbook) ;
// Load the XLSM template file with macro
workbook->easy_LoadXLSXFile("C:\\Samples\\Macro.xlsm");
// Add data to Excel file
...
// Export the XLSM file
workbook->easy_WriteXLSXFile("C:\\Samples\\Macro filled with data.xlsm");
C++.NET// Create an instance of the class that exports Excel files
ExcelDocument ^workbook = gcnew ExcelDocument();
// Load the XLSM template file with macro
workbook->easy_LoadXLSXFile("C:\\Samples\\Macro.xlsm");
// Add data to Excel file
...
// Export the XLSM file
workbook->easy_WriteXLSXFile("C:\\Samples\\Macro filled with data.xlsm");
// Create an instance of the class that exports Excel files
ExcelDocument workbook = new ExcelDocument();
// Load the XLSM template file with macro
workbook.easy_LoadXLSXFile("C:\\Samples\\Macro.xlsm");
// Add data to Excel file
...
// Export the XLSM file
workbook.easy_WriteXLSXFile("C:\\Samples\\Macro filled with data.xlsm");
.NET:// Create an instance of the class that exports Excel files
$workbook = new COM("EasyXLS.ExcelDocument");
// Load the XLSM template file with macro
$workbook->easy_LoadXLSXFile("C:\Samples\Macro.xlsm");
// Add data to Excel file
...
// Export the XLSM file
$workbook->easy_WriteXLSXFile("C:\Samples\Macro filled with data.xlsm");
Java:// Create an instance of the class that exports Excel files
$workbook = new java("EasyXLS.ExcelDocument");
// Load the XLSM template file with macro
$workbook->easy_LoadXLSXFile("C:\Samples\Macro.xlsm");
// Add data to Excel file
...
// Export the XLSM file
$workbook->easy_WriteXLSXFile("C:\Samples\Macro filled with data.xlsm");
' Create an instance of the class that exports Excel filesset workbook = Server.CreateObject("EasyXLS.ExcelDocument")
' Load the XLSM template file with macro
workbook.easy_LoadXLSXFile("C:\Samples\Macro.xlsm")
' Add data to Excel file
...
' Export the XLSM file
workbook.easy_WriteXLSXFile("C:\Samples\Macro filled with data.xlsm")
' Create an instance of the class that exports Excel filesSet workbook = CreateObject("EasyXLS.ExcelDocument")
' Load the XLSM template file with macro
workbook.easy_LoadXLSXFile("C:\Samples\Macro.xlsm")
' Add data to Excel file
...
' Export the XLSM file
workbook.easy_WriteXLSXFile("C:\Samples\Macro filled with data.xlsm")
' Create an instance of the class that exports Excel filesset workbook = CreateObject("EasyXLS.ExcelDocument")
' Load the XLSM template file with macro
workbook.easy_LoadXLSXFile("C:\Samples\Macro.xlsm")
' Add data to Excel file
...
' Export the XLSM file
workbook.easy_WriteXLSXFile("C:\Samples\Macro filled with data.xlsm")
<!-- Create an instance of the class that exports Excel files --><cfobject type="java"class="EasyXLS.ExcelDocument"name="workbook"action="CREATE"><!-- Load the XLSM template file with macro --><cfset ret = workbook.easy_LoadXLSXFile("C:\Samples\Macro.xlsm")><!-- Add data to Excel file -->
...
<!-- Export the XLSM file --><cfset ret = workbook.easy_WriteXLSXFile("C:\Samples\Macro filled with data.xlsm")>
.NET:# Create an instance of the class that exports Excel files
workbook = ExcelDocument()
# Load the XLSM template file with macro
workbook.easy_LoadXLSXFile("C:\\Samples\\Macro.xlsm")
# Add data to Excel file
...
# Export the XLSM file
workbook.easy_WriteXLSXFile("C:\\Samples\\Macro filled with data.xlsm")
Java:# Create an instance of the class that exports Excel files
workbook = gateway.jvm.ExcelDocument()
# Load the XLSM template file with macro
workbook.easy_LoadXLSXFile("C:\\Samples\\Macro.xlsm")
# Add data to Excel file
...
# Export the XLSM file
workbook.easy_WriteXLSXFile("C:\\Samples\\Macro filled with data.xlsm")
// Create an instance of the class that loads Excel files
ExcelDocument workbook = new ExcelDocument();
// Load the XLSM file with macro
workbook.easy_LoadXLSXFile("C:\\Samples\\Excel with macro.xlsm");
// Remove macro from Excel file
workbook.easy_removeVBMacros();
// Export the XLSX file without macro
workbook.easy_WriteXLSXFile("C:\\Samples\\Excel without macro.xlsx");
' Create an instance of the class that loads Excel filesDim workbook As New ExcelDocument
' Load the XLSM file with macro
workbook.easy_LoadXLSXFile("C:\Samples\Excel with macro.xlsm")
' Remove macro from Excel file
workbook.easy_removeVBMacros()
' Export the XLSX file without macro
workbook.easy_WriteXLSXFile("C:\Samples\Excel without macro.xlsx")
C++// Create an instance of the class that loads Excel files
EasyXLS::IExcelDocumentPtr workbook;
hr = CoCreateInstance(__uuidof(EasyXLS::ExcelDocument),
NULL,
CLSCTX_ALL,
__uuidof(EasyXLS::IExcelDocument),
(void**) &workbook) ;
// Load the XLSM file with macro
workbook->easy_LoadXLSXFile("C:\\Samples\\Excel with macro.xlsm");
// Remove macro from Excel file
workbook->easy_removeVBMacros();
// Export the XLSX file without macro
workbook->easy_WriteXLSXFile("C:\\Samples\\Excel without macro.xlsx");
C++.NET// Create an instance of the class that loads Excel files
ExcelDocument ^workbook = gcnew ExcelDocument();
// Load the XLSM file with macro
workbook->easy_LoadXLSXFile("C:\\Samples\\Excel with macro.xlsm");
// Remove macro from Excel file
workbook->easy_removeVBMacros();
// Export the XLSX file without macro
workbook->easy_WriteXLSXFile("C:\\Samples\\Excel without macro.xlsx");
// Create an instance of the class that loads Excel files
ExcelDocument workbook = new ExcelDocument();
// Load the XLSM file with macro
workbook.easy_LoadXLSXFile("C:\\Samples\\Excel with macro.xlsm");
// Remove macro from Excel file
workbook.easy_removeVBMacros();
// Export the XLSX file without macro
workbook.easy_WriteXLSXFile("C:\\Samples\\Excel without macro.xlsx");
.NET:// Create an instance of the class that loads Excel files
$workbook = new COM("EasyXLS.ExcelDocument");
// Load the XLSM file with macro
$workbook->easy_LoadXLSXFile("C:\Samples\Excel with macro.xlsm");
// Remove macro from Excel file
$workbook->easy_removeVBMacros();
// Export the XLSX file without macro
$workbook->easy_WriteXLSXFile("C:\Samples\Excel without macro.xlsx");
Java:// Create an instance of the class that loads Excel files
$workbook = new java("EasyXLS.ExcelDocument");
// Load the XLSM file with macro
$workbook->easy_LoadXLSXFile("C:\Samples\Excel with macro.xlsm");
// Remove macro from Excel file
$workbook->easy_removeVBMacros();
// Export the XLSX file without macro
$workbook->easy_WriteXLSXFile("C:\Samples\Excel without macro.xlsx");
' Create an instance of the class that loads Excel filesset workbook = Server.CreateObject("EasyXLS.ExcelDocument")
' Load the XLSM file with macro
workbook.easy_LoadXLSXFile("C:\Samples\Excel with macro.xlsm")
' Remove macro from Excel file
workbook.easy_removeVBMacros()
' Export the XLSX file without macro
workbook.easy_WriteXLSXFile("C:\Samples\Excel without macro.xlsx")
' Create an instance of the class that loads Excel filesSet workbook = CreateObject("EasyXLS.ExcelDocument")
' Load the XLSM file with macro
workbook.easy_LoadXLSXFile("C:\Samples\Excel with macro.xlsm")
' Remove macro from Excel file
workbook.easy_removeVBMacros
' Export the XLSX file without macro
workbook.easy_WriteXLSXFile("C:\Samples\Excel without macro.xlsx")
' Create an instance of the class that loads Excel filesSet workbook = CreateObject("EasyXLS.ExcelDocument")
' Load the XLSM file with macro
workbook.easy_LoadXLSXFile("C:\Samples\Excel with macro.xlsm")
' Remove macro from Excel file
workbook.easy_removeVBMacros()
' Export the XLSX file without macro
workbook.easy_WriteXLSXFile("C:\Samples\Excel without macro.xlsx")
<!-- Create an instance of the class that loads Excel files --><cfobject type="java"class="EasyXLS.ExcelDocument"name="workbook"action="CREATE"><!-- Load the XLSM file with macro --><cfset ret = workbook.easy_LoadXLSXFile("C:\Samples\Excel with macro.xlsm")><!-- Remove macro from Excel file --><cfset ret = workbook.easy_removeVBMacros()><!-- Export the XLSX file without macro --><cfset ret = workbook.easy_WriteXLSXFile("C:\Samples\Excel without macro.xlsx")>
.NET:# Create an instance of the class that loads Excel files
workbook = ExcelDocument()
# Load the XLSM file with macro
workbook.easy_LoadXLSXFile("C:\\Samples\\Excel with macro.xlsm")
# Remove macro from Excel file
workbook.easy_removeVBMacros()
# Export the XLSX file without macro
workbook.easy_WriteXLSXFile("C:\\Samples\\Excel without macro.xlsx")
Java:# Create an instance of the class that loads Excel files
workbook = gateway.jvm.ExcelDocument()
# Load the XLSM file with macro
workbook.easy_LoadXLSXFile("C:\\Samples\\Excel with macro.xlsm")
# Remove macro from Excel file
workbook.easy_removeVBMacros()
# Export the XLSX file without macro
workbook.easy_WriteXLSXFile("C:\\Samples\\Excel without macro.xlsx")
Add data into sheets
EasyXLS™ enables you to populate the sheets with data. The supported data types are strings, numbers, dates, booleans, errors and formulas.