Import Excel file to List in .NET, Java and other programming languages
Concept
EasyXLS™ library allows you to import Excel data to List. The data can be imported from an Excel sheet or from the active Excel sheet. The entire sheet data or only data from ranges of cells can be imported.
The list contains the Excel rows, each row being an EasyXLS.Util.List object containing the cell values. EasyXLS.Util.List extends System.Collections.ArrayList for .NET version and java.util.Vector for Java version.
EasyXLS can be successfully used to also import large Excel files having big volume of data with fast importing time.
EasyXLS permits you to import Excel files without Excel installed, without Interop, without OLEDB or any other additional software installed.
Concept in action
The below example shows how to import Excel to List in .NET, Java, PHP, ASP, C++, VB6, VBS and Coldfusion.
// Create an instance of the class that imports Excel files
ExcelDocument workbook = new ExcelDocument();
// Import Excel file to List
EasyXLS.Util.List rows =
workbook.easy_ReadXLSXActiveSheet_AsList("C:\\Samples\\Tutorial09.xlsx");
// Confirm Excel file import
if (workbook.easy_getError() == "")
{
// Display imported List values
EasyXLS.Util.List row;
for (int rowIndex=0; rowIndex < rows.size(); rowIndex++)
{
row = (EasyXLS.Util.List)rows.elementAt(rowIndex);
for (int cellIndex=0; cellIndex < row.size(); cellIndex++)
{
Console.WriteLine("At row " + (rowIndex + 1) +
", column " + (cellIndex + 1)
+ " the value is '" + row.elementAt(cellIndex));
}
}
}
else
Console.Write("Error importing Excel file C:\\Samples\\Tutorial09.xlsx " +
workbook.easy_getError());
' Create an instance of the class that imports Excel files
Dim workbook As New ExcelDocument
' Import Excel file to List
Dim rows = workbook.easy_ReadXLSXActiveSheet_AsList("C:\\Samples\\Tutorial09.xlsx")
' Confirm Excel file import
If (workbook.easy_getError().Equals("")) Then
' Display imported List values
Dim row
For rowIndex As Integer = 0 To rows.size() - 1
row = rows.elementAt(rowIndex)
For cellIndex As Integer = 0 To row.size() - 1
Console.WriteLine(vbCrLf & "At row " & (rowIndex + 1) & ", column " _
& (cellIndex + 1) & " the value is '" & row.elementAt(cellIndex))
Next
Next
Else
Console.Write(vbCrLf & _
"Error importing Excel file C:\\Samples\\Tutorial09.xlsx " & _
workbook.easy_getError())
End If
C++
// Create an instance of the class that imports Excel files
EasyXLS::IExcelDocumentPtr workbook;
hr = CoCreateInstance(__uuidof(EasyXLS::ExcelDocument),
NULL,
CLSCTX_ALL,
__uuidof(EasyXLS::IExcelDocument),
(void**) &workbook);
// Import Excel file to List
EasyXLS::IListPtr rows =
workbook->easy_ReadXLSXActiveSheet_AsList("C:\\Samples\\Tutorial09.xlsx");
// Confirm Excel file import
_bstr_t sError = workbook->easy_getError();
if (strcmp(sError, "") == 0)
{
// Display imported List values
for (int rowIndex=0; rowIndex<rows->size(); rowIndex++)
{
EasyXLS::IListPtr row = (EasyXLS::IListPtr) rows->elementAt(rowIndex);
for (int cellIndex=0; cellIndex<row->size(); cellIndex++)
{
printf("At row %d, column %d the value is '%s'\n",
(rowIndex+ 1), (cellIndex+ 1),
(LPCSTR)((_bstr_t)row->elementAt(cellIndex)));
}
}
printf("\nPress Enter to exit ...");
}
else
{
printf("\nError importing Excel file C:\\Samples\\Tutorial09.xlsx %s\n",
(LPCSTR)sError);
}
C++.NET
// Create an instance of the class that imports Excel files
ExcelDocument *workbook = new ExcelDocument();
// Import Excel file to List
EasyXLS::Util::List *rows =
workbook->easy_ReadXLSXActiveSheet_AsList("C:\\Samples\\Tutorial09.xlsx");
// Confirm Excel file import
if (workbook->easy_getError()->Equals(""))
{
// Display imported List values
EasyXLS::Util::List *row;
for (int rowIndex=0; rowIndex < rows->size(); rowIndex++)
{
row = __try_cast<EasyXLS::Util::List*>(rows->elementAt(rowIndex));
for (int cellIndex=0; cellIndex < row->size(); cellIndex++)
{
Console::Write(String::Concat("At row ", (rowIndex + 1).ToString(),
", column ", (cellIndex + 1).ToString()));
Console::WriteLine(String::Concat(" the value is '",
row->elementAt(cellIndex)->ToString()));
}
}
}
else
Console::WriteLine(String::Concat(
"Error importing Excel file C:\\Samples\\Tutorial09.xlsx ",
workbook->easy_getError()));
// Create an instance of the class that imports Excel files
ExcelDocument workbook = new ExcelDocument();
// Import Excel file to List
EasyXLS.Util.List rows =
workbook.easy_ReadXLSXActiveSheet_AsList("C:\\Samples\\Tutorial09.xlsx");
// Confirm Excel file import
if (workbook.easy_getError().equals(""))
{
// Display the values
EasyXLS.Util.List row;
for (int rowIndex=0; rowIndex < rows.size(); rowIndex++)
{
row = (EasyXLS.Util.List)rows.elementAt(rowIndex);
for (int cellIndex=0; cellIndex < row.size(); cellIndex++)
{
System.out.println("At row " + (rowIndex + 1) +
", column " + (cellIndex + 1) +
" the value is '" + row.elementAt(cellIndex));
}
}
}
else
System.out.println("Error importing Excel file C:\\Samples\\Tutorial09.xlsx " +
workbook.easy_getError());
// Create an instance of the class that imports Excel files
$workbook = new COM("EasyXLS.ExcelDocument");
// Import Excel file to List
$rows = $workbook->easy_ReadXLSXActiveSheet_AsList("C:\\Samples\\Tutorial09.xlsx");
// Confirm Excel file import
if ($workbook->easy_getError() == "")
{
// Display imported List values
for ($rowIndex=0; $rowIndex<$rows->size(); $rowIndex++)
{
$row = $rows->elementAt($rowIndex);
for ($cellIndex=0; $cellIndex<$row->size(); $cellIndex++)
{
echo "At row ".($rowIndex + 1).", column ".($cellIndex + 1).
" the value is '".$row->elementAt($cellIndex)."'<br>";
}
}
}
else
echo "Error importing Excel file C:\Samples\Tutorial09.xlsx " .
$workbook->easy_getError();
' Create an instance of the class that imports Excel files
set workbook = Server.CreateObject("EasyXLS.ExcelDocument")
'Import Excel file to List
Set rows = workbook.easy_ReadXLSXActiveSheet_AsList("C:\Samples\Tutorial09.xlsx")
' Confirm Excel file import
if workbook.easy_getError() = "" then
' Display imported List values
for rowIndex = 0 to rows.size() - 1
Set row = rows.elementAt(rowIndex)
for cellIndex = 0 to row.size - 1
response.write("At row " & (rowIndex + 1) & _
", column " & (cellIndex + 1) & _
" the value is '" & row.elementAt(cellIndex) & "'< br>")
next
next
else
response.Write("Error importing Excel file C:\Samples\Tutorial09.xlsx " & _
workbook.easy_getError())
end if
' Create an instance of the class that imports Excel files
Set workbook = CreateObject("EasyXLS.ExcelDocument")
'Import Excel file to List
Set rows = workbook.easy_ReadXLSXActiveSheet_AsList("C:\Samples\Tutorial09.xlsx")
' Confirm Excel file import
If workbook.easy_getError() = "" Then
' Display imported List values
For rowIndex = 0 To rows.Size() - 1
Set row = rows.elementAt(rowIndex)
For cellIndex = 0 To row.Size - 1
Me.Text1 = Me.Text1 & _
"At row " & (rowIndex + 1) & ", column " & (cellIndex + 1) & _
" the value is '" & row.elementAt(cellIndex) & "'" & vbCrLf
Next
Next
Else
Me.Text1 = Me.Text1 & _
vbCrLf & "Error importing Excel file C:\Samples\Tutorial09.xlsx " & _
vbCrLf & workbook.easy_getError()
End If
' Create an instance of the class that imports Excel files
set workbook = CreateObject("EasyXLS.ExcelDocument")
'Import Excel file to List
Set rows = workbook.easy_ReadXLSXActiveSheet_AsList("C:\Samples\Tutorial09.xlsx")
' Confirm Excel file import
if workbook.easy_getError() = "" then
' Display imported List values
For rowIndex = 0 To rows.Size() - 1
Set row = rows.elementAt(rowIndex)
For cellIndex = 0 To row.Size - 1
WScript.StdOut.WriteLine("At row " & (rowIndex + 1) & _
", column " & (cellIndex + 1) & _
" the value is '" & row.elementAt(cellIndex) & "'")
Next
Next
else
WScript.StdOut.Write(vbcrlf & _
"Error importing Excel file C:\Samples\Tutorial09.xlsx " & _
workbook.easy_getError())
end if
EasyXLS enables you to import Excel data to List 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 Excel 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 Excel to List have parameters that permit importing only ranges of cells.