Import Excel file to List in .NET, Java and other programming languages
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.
// 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 importif (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 filesDim workbook As New ExcelDocument
' Import Excel file to ListDim rows = workbook.easy_ReadXLSXActiveSheet_AsList("C:\Samples\Tutorial09.xlsx")
' Confirm Excel file importIf (workbook.easy_getError().Equals("")) Then' Display imported List valuesDim 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))
NextNextElse
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 valuesfor (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 = gcnew ExcelDocument();
// Import Excel file to List
EasyXLS::Util::List ^rows =
workbook->easy_ReadXLSXActiveSheet_AsList("C:\\Samples\\Tutorial09.xlsx");
// Confirm Excel file importString ^sError = workbook->easy_getError();
if (sError->Equals(""))
{
// Display imported List values
EasyXLS::Util::List ^row;
StringBuilder ^str;
for (int rowIndex=0; rowIndex < rows->size(); rowIndex++)
{
row = safe_cast<EasyXLS::Util::List^>(rows->elementAt(rowIndex));
for (int cellIndex=0; cellIndex < row->size(); cellIndex++)
{
str = gcnew StringBuilder();
str->Append(String::Concat("At row ", (rowIndex + 1).ToString(),
", column ", (cellIndex + 1).ToString()));
str->Append(String::Concat(" the value is '",
row->elementAt(cellIndex)->ToString()));
Console::WriteLine(str);
}
}
}
else
Console::WriteLine(String::Concat(
"\nError importing Excel file C:\\Samples\\Tutorial09.xlsx \n", sError));
// 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 importif (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());
.NET:// 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 importif ($workbook->easy_getError() == "")
{
// Display imported List valuesfor ($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>";
}
}
}
elseecho"Error importing Excel file C:\Samples\Tutorial09.xlsx " .
$workbook->easy_getError();
Click here to see Continuous Code ListingJava:// Create an instance of the class that imports Excel files
$workbook = new java("EasyXLS.ExcelDocument");
// Import Excel file to List
$rows = $workbook->easy_ReadXLSXActiveSheet_AsList("C:\\Samples\\Tutorial09.xlsx");
// Confirm Excel file importif ($workbook->easy_getError() == "")
{
// Display imported List valuesfor ($rowIndex=0; $rowIndex<(int)(string)$rows->size(); $rowIndex++)
{
$row = $rows->elementAt($rowIndex);
for ($cellIndex=0; $cellIndex<(int)(string)$row->size(); $cellIndex++)
{
echo"At row ".($rowIndex + 1).", column ".($cellIndex + 1).
" the value is '".$row->elementAt($cellIndex)."'<br>";
}
}
}
elseecho"Error importing Excel file C:\Samples\Tutorial09.xlsx " .
$workbook->easy_getError();
Click here to see Continuous Code Listing
' Create an instance of the class that imports Excel filesset workbook = Server.CreateObject("EasyXLS.ExcelDocument")
' Import Excel file to Listset rows = workbook.easy_ReadXLSXActiveSheet_AsList("C:\Samples\Tutorial09.xlsx")
' Confirm Excel file importif workbook.easy_getError() = ""then' Display imported List valuesfor 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>")
nextnextelse
response.Write("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.