Import XLSM file in .NET, Java and other programming languages
EasyXLS™ library allows you to import data from an XLSM file or other sheet elements like cell formatting, charts, pivot tables, comments, images and others.
EasyXLS can be successfully used to also import large XLSM files having big volume of data with fast importing time.
EasyXLS allows loading an XLSM document. Once the XLSM is loaded in memory, the user can process data, add more features to the file (like formulas, cell formatting, comments, hyperlinks, images, data validations, page setup, charts, macro, etc), and save the file back in XLSM format or other format (XLS, XLSB, XML Spreadsheet, XML, TXT, CSV or HTML). It can be used to load an already formatted XLSM file as a template, add data and save back the file in XLSM format. In this way, a lot of written code that formats the XLSM file can be avoided and the macros are preserved.
EasyXLS permits you to import Excel files without Excel installed, without Interop, without OLEDB or any other additional software installed.
The below source code sample is a common code about how to import an XLSM file. The source code shows how to load and import an XLSM file that is used as a base template, add more data to the XLSM file and save the new workbook. After that, the best techniques about importing data from XLSM file like importing Excel data to DataTable, GridView, DataGridView, DataSet, ResultSet, DataGrid and List are shown.
// Create an instance of the class that imports XLSM files
ExcelDocument workbook = new ExcelDocument();
// Import XLSM file
workbook.easy_LoadXLSXFile("C:\\Samples\\Excel to import.xlsm");
// Get the table of the second worksheet
ExcelTable xlsSecondTable =
((ExcelWorksheet)workbook.easy_getSheet("Second tab")).easy_getExcelTable();
// Add more data to the second sheet
xlsSecondTable.easy_getCell("A1").setValue("Data added by Tutorial37");
for (int column=0; column<5; column++)
{
xlsSecondTable.easy_getCell(1, column).setValue("Data " + (column + 1));
}
// Generate the XLSM file
workbook.easy_WriteXLSXFileC:\\Samples\\Excel with macro.xlsm");
' Create an instance of the class that imports XLSM filesDim workbook As New ExcelDocument
' Import XLSM file
workbook.easy_LoadXLSXFile("C:\Samples\Excel to import.xlsm")
' Get the table of the second worksheetDim xlsSecondTab As ExcelWorksheet = workbook.easy_getSheet("Second tab")
Dim xlsSecondTable = xlsSecondTab.easy_getExcelTable
' Add more data to the second sheet
xlsSecondTable.easy_getCell("A1").setValue("Data added by Tutorial37")
For column As Integer = 0 To 4
xlsSecondTable.easy_getCell(1, column).setValue("Data " & (column + 1))
Next' Generate the XLSM file
workbook.easy_WriteXLSXFile("C:\Samples\Excel with macro.xlsm")
C++// Create an instance of the class that imports XLSM files
EasyXLS::IExcelDocumentPtr workbook;
hr = CoCreateInstance(__uuidof(EasyXLS::ExcelDocument),
NULL,
CLSCTX_ALL,
__uuidof(EasyXLS::IExcelDocument),
(void**) &workbook) ;
// Import XLSM file
workbook->easy_LoadXLSXFile("C:\\Samples\\Excel to import.xlsm");
// Get the table of the second worksheet
EasyXLS::IExcelWorksheetPtr xlsSecondTab =
(EasyXLS::IExcelWorksheetPtr)workbook->easy_getSheetAt(1);
EasyXLS::IExcelTablePtr xlsTable = xlsSecondTab->easy_getExcelTable();
// Add more data to the second sheet
xlsTable->easy_getCell_2("A1")->setValue("Data added by Tutorial37");
char* cellValue = (char*)malloc(11*sizeof(char));
char* columnNumber = (char*)malloc(sizeof(char));
for (int column=0; column<5; column++)
{
strcpy(cellValue, "Data ");
_itoa(column+ 1, columnNumber , 10);
xlsTable->easy_getCell(1, column)->setValue(strcat(cellValue, columnNumber));
}
// Generate the XLSM file
workbook->easy_WriteXLSXFile("C:\\Samples\\Excel with macro.xlsm");
Click here to see Continuous Code ListingC++.NET// Create an instance of the class that imports XLSM files
ExcelDocument ^workbook = gcnew ExcelDocument();
// Import XLSM file
workbook->easy_LoadXLSXFile("C:\\Samples\\Excel to import.xlsm");
// Get the table of the second worksheet
ExcelWorksheet ^xlsSecondTab =
safe_cast<ExcelWorksheet^>(workbook->easy_getSheet("Second tab"));
ExcelTable ^xlsSecondTable = xlsSecondTab->easy_getExcelTable();
// Add more data to the second sheet
xlsSecondTable->easy_getCell("A1")->setValue("Data added by Tutorial37");
for (int column=0; column<5; column++)
{
xlsSecondTable->easy_getCell(1, column)->setValue(
String::Concat("Data ", (column + 1).ToString()));
}
// Generate the XLSM file
workbook->easy_WriteXLSXFile("C:\\Samples\\Excel with macro.xlsm");
Click here to see Continuous Code Listing
// Create an instance of the class that imports XLSM files
ExcelDocument workbook = new ExcelDocument();
// Import XLSM file
FileInputStream file = new FileInputStream("C:\\Samples\\Excel to import.xlsm");
workbook.easy_LoadXLSXFile(file);
// Get the table of the second worksheet
ExcelTable xlsSecondTable =
((ExcelWorksheet)workbook.easy_getSheetAt(1)).easy_getExcelTable();
// Add more data to the second sheet
xlsSecondTable.easy_getCell("A1").setValue("Data added by Tutorial37");
for (int column=0; column<5; column++)
{
xlsSecondTable.easy_getCell(1, column).setValue("Data " + (column + 1));
}
// Generate the XLSM file
workbook.easy_WriteXLSXFile("C:\\Samples\\Excel with macro.xlsm");
.NET:// Create an instance of the class that imports XLSM files
$workbook = new COM("EasyXLS.ExcelDocument");
// Import XLSM file
$workbook->easy_LoadXLSXFile("C:\\Samples\\Excel to import.xlsm");
// Get the table of the second worksheet
$xlsSecondTable = $workbook->easy_getSheet("Second tab")->easy_getExcelTable();
// Add more data to the second sheet
$xlsSecondTable->easy_getCell_2("A1")->setValue("Data added by Tutorial36");
for ($column=0; $column<5; $column++)
{
$xlsSecondTable->easy_getCell(1, $column)->setValue("Data " . ($column + 1));
}
// Generate the XLSM file
$workbook->easy_WriteXLSXFile("C:\Samples\Excel with macro.xlsm");
Click here to see Continuous Code ListingJava:// Create an instance of the class that imports XLSM files
$workbook = new java("EasyXLS.ExcelDocument");
// Import XLSM file
$workbook->easy_LoadXLSXFile("C:\\Samples\\Excel to import.xlsm");
// Get the table of the second worksheet
$xlsSecondTable = $workbook->easy_getSheet("Second tab")->easy_getExcelTable();
// Add more data to the second sheet
$xlsSecondTable->easy_getCell("A1")->setValue("Data added by Tutorial36");
for ($column=0; $column<5; $column++)
{
$xlsSecondTable->easy_getCell(1, $column)->setValue("Data " . ($column + 1));
}
// Generate the XLSM file
$workbook->easy_WriteXLSXFile("C:\Samples\Excel with macro.xlsm");
Click here to see Continuous Code Listing
' Create an instance of the class that imports XLSM filesset workbook = Server.CreateObject("EasyXLS.ExcelDocument")
' Import XLSM file
workbook.easy_LoadXLSXFile("C:\Samples\Excel to import.xlsm")
' Get the table of the second worksheetset xlsSecondTable = workbook.easy_getSheet("Second tab").easy_getExcelTable()
' Add more data to the second sheet
xlsSecondTable.easy_getCell_2("A1").setValue("Data added by Tutorial37")
for column=0 to 4
xlsSecondTable.easy_getCell(1, column).setValue("Data " & (column + 1))
next' Generate the XLSM file
workbook.easy_WriteXLSXFile ("C:\Samples\Excel with macro.xlsm")
' Create an instance of the class that imports XLSM filesSet workbook = CreateObject("EasyXLS.ExcelDocument")
' Import XLSM file
workbook.easy_LoadXLSXFile("C:\Samples\Excel to import.xlsm")
' Get the table of the second worksheetSet xlsSecondTable = easy_getSheetAt(1).easy_getExcelTable()
' Add more data to the second sheet
xlsSecondTable.easy_getCell_2("A1").setValue ("Data added by Tutorial37")
For Column = 0 To 4
xlsSecondTable.easy_getCell(1, Column).setValue ("Data " & (Column + 1))
Next' Generate the XLSM file
workbook.easy_WriteXLSXFile ("C:\Samples\Excel with macro.xlsm")
' Create an instance of the class that imports XLSM filesset workbook = CreateObject("EasyXLS.ExcelDocument")
' Import XLSM file
workbook.easy_LoadXLSXFile("C:\Samples\Excel to import.xlsm")
' Get the table of the second worksheetSet xlsSecondTable = workbook.easy_getSheet("Second tab").easy_getExcelTable()
' Add more data to the second sheet
xlsSecondTable.easy_getCell_2("A1").setValue("Data added by Tutorial37")
for column=0 to 4
xlsSecondTable.easy_getCell(1, column).setValue("Data " & (column + 1))
next' Generate the XLSM file
workbook.easy_WriteXLSXFile ("C:\Samples\Excel with macro.xlsm")
<!-- Create an instance of the class that imports XLSM files --><cfobject type="java"class="EasyXLS.ExcelDocument"name="workbook"action="CREATE">
<!-- Import XLSM file --><cfset success = workbook.easy_LoadXLSXFile("C:\Samples\Excel to import.xlsm")><!-- Get the table of the second worksheet --><cfset xlsTable = workbook.easy_getSheetAt(1).easy_getExcelTable()><!-- Add more data to the second sheet --><cfset xlsTable.easy_getCell("A1").setValue("Data added by Tutorial37")><cfloop from="0"to="4"index="column"><cfset xlsTable.easy_getCell(1,evaluate(column)).setValue(
"Data " & evaluate(column + 1))></cfloop><!-- Generate the XLSM file --><cfset ret = workbook.easy_WriteXLSXFile("C:\Samples\Excel with macro.xlsm")>
.NET:# Create an instance of the class that reads Excel files
workbook = ExcelDocument()
# Read XLSX fileif workbook.easy_LoadXLSXFile("C:\\Samples\\Excel to import.xlsx"):
# Get the table of data for the second worksheet
xlsSecondTable = workbook.easy_getSheet("Second tab").easy_getExcelTable()
# Write some data to the second sheet
xlsSecondTable.easy_getCell("A1").setValue("Data added by Tutorial36")
for column in range(5):
xlsSecondTable.easy_getCell(1, column).setValue("Data " + str(column + 1))
# Export the new XLSX file
workbook.easy_WriteXLSXFile("C:\\Samples\\Excel with macro.xlsx")
Click here to see Continuous Code ListingJava:# Create an instance of the class that reads Excel files
workbook = gateway.jvm.ExcelDocument()
# Read XLSX fileif workbook.easy_LoadXLSXFile("C:\\Samples\\Excel to import.xlsx"):
# Get the table of data for the second worksheet
xlsSecondTable = workbook.easy_getSheet("Second tab").easy_getExcelTable()
# Write some data to the second sheet
xlsSecondTable.easy_getCell("A1").setValue("Data added by Tutorial36")
for column in range(5):
xlsSecondTable.easy_getCell(1, column).setValue("Data " + str(column + 1))
# Export the new XLSX file
workbook.easy_WriteXLSXFile("C:\\Samples\\Excel with macro.xlsx")
Click here to see Continuous Code Listing
The screen shots below provide an example of the imported XLSM file, modified and saved back to another XLSM file. The file has two worksheets (First Tab and Second Tab) and contains a VBA project and macros. The data is added to the second worksheet.
and:
Import data from XLSM file
EasyXLS allows you to import data from an Excel sheet or from the active Excel sheet. The entire sheet data or only data from ranges of cells can be imported.
// Create an instance of the class that imports XLSM files
ExcelDocument workbook = new ExcelDocument();
// Import XLSM file
DataSet ds = workbook.easy_ReadXLSXActiveSheet_AsDataSet("C:\\Samples\\Excel file.xlsm");
' Create an instance of the class that imports XLSM filesDim workbook As New ExcelDocument
' Import XLSM fileDim ds As DataSet = workbook.easy_ReadXLSXActiveSheet_AsDataSet("C:\Samples\Excel file.xlsm")
// Create an instance of the class that imports XLSM files
ExcelDocument ^workbook = gcnew ExcelDocument();
// Import XLSM file
DataSet ^ds = workbook->easy_ReadXLSXActiveSheet_AsDataSet("C:\\Samples\\Excel file.xlsm");
// Create an instance of the class that imports XLSM files
ExcelDocument workbook = new ExcelDocument();
// Import XLSM file
ResultSet rs = workbook.easy_ReadXLSXActiveSheet_AsResultSet("C:\\Samples\\Excel file.xlsm");
.NET:// Create an instance of the class that imports XLSM files
$workbook = new COM("EasyXLS.ExcelDocument");
// Import XLSM file
$rows = $workbook->easy_ReadXLSXActiveSheet_AsList("C:\\Samples\\Excel file.xlsm");
Java:// Create an instance of the class that imports XLSM files
$workbook = new java("EasyXLS.ExcelDocument");
// Import XLSM file
$rows = $workbook->easy_ReadXLSXActiveSheet_AsList("C:\\Samples\\Excel file.xlsm");
' Create an instance of the class that imports XLSM filesset workbook = Server.CreateObject("EasyXLS.ExcelDocument")
' Import XLSM fileSet rows = workbook.easy_ReadXLSXActiveSheet_AsList("C:\\Samples\\Excel file.xlsm")
// Create an instance of the class that imports XLSM files
EasyXLS::IExcelDocumentPtr workbook;
hr = CoCreateInstance(__uuidof(EasyXLS::ExcelDocument),
NULL,
CLSCTX_ALL,
__uuidof(EasyXLS::IExcelDocument),
(void**) &workbook);
// Import XLSM file
EasyXLS::IListPtr rows =
workbook->easy_ReadXLSXActiveSheet_AsList("C:\\Samples\\Excel file.xlsm");
' Create an instance of the class that imports XLSM filesSet workbook = CreateObject("EasyXLS.ExcelDocument")
' Import XLSM fileSet rows = workbook.easy_ReadXLSXActiveSheet_AsList("C:\\Samples\\Excel file.xlsm")
' Create an instance of the class that imports XLSM filesSet workbook = CreateObject("EasyXLS.ExcelDocument")
' Import XLSM fileSet rows = workbook.easy_ReadXLSXActiveSheet_AsList("C:\\Samples\\Excel file.xlsm")
<!-- Create an instance of the class that imports XLSM files --><cfobject type="java"class="EasyXLS.ExcelDocument"name="workbook"action="CREATE">
<!-- Import XLSM file--><cfset rs = workbook.easy_ReadXLSXActiveSheet_AsResultSet("C:\\Samples\\Excel file.xlsm")>
.NET:# Create an instance of the class that imports XLSM files
workbook = ExcelDocument()
# Import XLSM file
ds = workbook.easy_ReadXLSXActiveSheet_AsDataSet("C:\\Samples\\Excel file.xlsm")
Java:# Create an instance of the class that imports XLSM files
workbook = gateway.jvm.ExcelDocument()
# Import XLSM file
rows = workbook.easy_ReadXLSXActiveSheet_AsList("C:\\Samples\\Excel file.xlsm")
Import data from XLSM file having one sheet
Importing data from an XLSM file, if the XLSM file has only one sheet, is the easiest approach. The Excel data can be imported with one single line of code using ExcelDocument.easy_ReadXLSXActiveSheet_AsDataSet method. The above code sample shows how to achieve this goal.
Import data from XLSM file having multiple sheets
There are three approaches for importing data from an XLSM file with multiple sheets.
I. Usually the first sheet is the active sheet inside an XLSM file. If this is your case or if you are importing data from another active sheet use ExcelDocument.easy_ReadXLSXActiveSheet_AsDataSet method.
EasyXLS enables you to import Excel data either from the entire sheet or from a range of cells. Importing only a range of cells is a very useful option especially for large XLSM files because it reduces the speed of the import process.
In order to import multiple cell ranges at once from Excel sheet, the range parameter must be passed to the method as union of ranges (multiple ranges separated by comma).
All the methods that allow importing XLSM file to DataSet or ResultSet have parameters that permit importing only ranges of cells.
// Create an instance of the class that imports XLSM files
ExcelDocument workbook = new ExcelDocument();
// Import cell range data from XLSM file
DataSet ds = workbook.easy_ReadXLSMSheet_AsDataSet(
"C:\\Samples\\Excel file.xlsm", "Sheet1", "A1:B10");
' Create an instance of the class that imports XLSM filesDim workbook As New ExcelDocument
' Import cell range data from XLSM fileDim ds = workbook.easy_ReadXLSMSheet_AsDataSet(
"C:\Samples\Excel file.xlsm", "Sheet1", "A1:B10")
// Create an instance of the class that imports XLSM files
ExcelDocument ^workbook = gcnew ExcelDocument();
// Import cell range data from XLSM file
DataSet ^ds = workbook->easy_ReadXLSMSheet_AsDataSet(
"C:\\Samples\\Excel file.xlsm", "Sheet1", "A1:B10");
// Create an instance of the class that imports XLSM files
ExcelDocument workbook = new ExcelDocument();
// Import cell range data from XLSM file
DataSet ds = workbook.easy_ReadXLSMSheet_AsResultSet(
"C:\\Samples\\Excel file.xlsm", "Sheet1", "A1:B10");
.NET:// Create an instance of the class that imports XLSM files
$workbook = new COM("EasyXLS.ExcelDocument");
// Import cell range data from XLSM file
$rows = $workbook->easy_ReadXLSMSheet_AsList_5(
"C:\\Samples\\Excel file.xlsm", "Sheet1", "A1:B10");
Java:// Create an instance of the class that imports XLSM files
$workbook = new java("EasyXLS.ExcelDocument");
// Import cell range data from XLSM file
$rows = $workbook->easy_ReadXLSMSheet_AsList(
"C:\\Samples\\Excel file.xlsm", "Sheet1", "A1:B10");
' Create an instance of the class that imports XLSM filesset workbook = Server.CreateObject("EasyXLS.ExcelDocument")
' Import cell range data from XLSM fileset rows = workbook.easy_ReadXLSMSheet_AsList_5(_
"C:\\Samples\\Excel file.xlsm", "Sheet1", "A1:B10")
// Create an instance of the class that imports XLSM files
EasyXLS::IExcelDocumentPtr workbook;
hr = CoCreateInstance(__uuidof(EasyXLS::ExcelDocument),
NULL,
CLSCTX_ALL,
__uuidof(EasyXLS::IExcelDocument),
(void**) &workbook);
// Import cell range data from XLSM file
EasyXLS::IListPtr rows =
workbook->easy_ReadXLSMSheet_AsList_5(
"C:\\Samples\\Excel file.xlsm", "Sheet1", "A1:B10");
' Create an instance of the class that imports XLSM filesSet workbook = CreateObject("EasyXLS.ExcelDocument")
' Import cell range data from XLSM fileSet rows = workbook.easy_ReadXLSMSheet_AsList_5( _
"C:\Samples\Excel file.xlsm", "Sheet1", "A1:B10")
' Create an instance of the class that imports XLSM filesSet workbook = CreateObject("EasyXLS.ExcelDocument")
' Import cell range data from XLSM fileSet rows = workbook.easy_ReadXLSMSheet_AsList_5( _
"C:\\Samples\\Excel file.xlsm", "Sheet1", "A1:B10")
<!-- Create an instance of the class that imports XLSM files --><cfobject type="java"class="EasyXLS.ExcelDocument"name="workbook"action="CREATE"><!-- Import cell range data from XLSM file--><cfset rows = workbook.easy_ReadXLSMSheet_AsResultSet(
"C:\\Samples\\Excel file.xlsm", "Sheet1", "A1:B10")>
.NET:# Create an instance of the class that imports XLSM files
workbook = ExcelDocument()
# Import cell range data from XLSM file
rows = workbook.easy_ReadXLSMSheet_AsList(
"C:\\Samples\\Excel file.xlsm", "Sheet1", "A1:B10")
Java:# Create an instance of the class that imports XLSM files
workbook = gateway.jvm.ExcelDocument()
# Import cell range data from XLSM file
rows = workbook.easy_ReadXLSMSheet_AsList(
"C:\\Samples\\Excel file.xlsm", "Sheet1", "A1:B10")
Import all XLSM file structures
EasyXLS allows you to import the whole XLSM file with sheets, data inside sheets, formulas, cell formatting, comments, hyperlinks, images, data validation, page setup, macros, groups, filters, charts, pivot tables and pivot charts.
The first above code sample shows how to achieve this goal.
Import XLSM file to SQL table in C# and VB.NET
EasyXLS library can be used to import Excel data to database like SQL Server, MySQL, Oracle, MS Access or any other database.
EasyXLS library can be used to import Excel sheets into DataTable. The DataTable can be the used as data source of a GridView, DataGridView, DataGrid or for any other purposes.