Import XLS file in .NET, Java and other programming languages
EasyXLS™ library allows you to import data from an XLS file or other sheet elements like cell formatting, charts, pivot tables, comments, images and others.
EasyXLS can be successfully used to also import large XLS files having big volume of data with fast importing time.
EasyXLS allows loading an XLS document. Once the XLS 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, etc), and save the file back in XLS binary format or other format (XLSX, XLSM, XLSB, XML Spreadsheet, XML, TXT, CSV or HTML). It can be used to load an already formatted XLS file as a template, add data and save back the file in XLS binary format. In this way, a lot of written code that formats the XLS file can be avoided.
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 XLS file. The source code shows how to load and import an XLS file that is used as a base template, add more data to the XLS file and save the new workbook. After that, the best techniques about importing data from XLS file like importing Excel data to DataTable, GridView, DataGridView, DataSet, ResultSet, DataGrid and List are shown.
// Create an instance of the class that imports XLS files
ExcelDocument workbook = new ExcelDocument();
// Import XLS file
workbook.easy_LoadXLSFile("C:\\Samples\\Excel to import.xls");
// 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 Tutorial36");
for (int column=0; column<5; column++)
{
xlsSecondTable.easy_getCell(1, column).setValue("Data " + (column + 1));
}
// Generate the XLS file
workbook.easy_WriteXLSFile("C:\\Samples\\Excel.xls");
' Create an instance of the class that imports XLS filesDim workbook As New ExcelDocument
' Import XLS file
workbook.easy_LoadXLSFile("C:\Samples\Excel to import.xls")
' 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 Tutorial36")
For column As Integer = 0 To 4
xlsSecondTable.easy_getCell(1, column).setValue("Data " & (column + 1))
Next' Generate the XLS file
workbook.easy_WriteXLSFile("C:\Samples\Excel.xls")
C++// Create an instance of the class that imports XLS files
EasyXLS::IExcelDocumentPtr workbook;
hr = CoCreateInstance(__uuidof(EasyXLS::ExcelDocument),
NULL,
CLSCTX_ALL,
__uuidof(EasyXLS::IExcelDocument),
(void**) &workbook) ;
// Import XLS file
workbook->easy_LoadXLSFile("C:\\Samples\\Excel to import.xls");
// Get the table of the second worksheet
EasyXLS::IExcelWorksheetPtr xlsSecondTab =
(EasyXLS::IExcelWorksheetPtr)workbook->easy_getSheet("Second tab");
EasyXLS::IExcelTablePtr xlsSecondTable = xlsSecondTab->easy_getExcelTable();
// Add more data to the second sheet
xlsSecondTable->easy_getCell_2("A1")->setValue("Data added by Tutorial36");
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);
xlsSecondTable->easy_getCell(1, column)->setValue(strcat(cellValue, columnNumber));
}
// Generate the XLS file
workbook->easy_WriteXLSFile("C:\\Samples\\Excel.xls");
Click here to see Continuous Code ListingC++.NET// Create an instance of the class that imports XLS files
ExcelDocument ^workbook = gcnew ExcelDocument();
// Import XLS file
workbook->easy_LoadXLSFile("C:\\Samples\\Excel to import.xls");
// Get the table of the second worksheet
ExcelWorksheet ^xlsSecondTab =
safe_cast<ExcelWorksheet^>(workbook->easy_getSheetAt(1));
ExcelTable ^xlsSecondTable = xlsSecondTab->easy_getExcelTable();
// Add more data to the second sheet
xlsSecondTable->easy_getCell("A1")->setValue("Data added by Tutorial36");
for (int column=0; column<5; column++)
{
xlsSecondTable->easy_getCell(1, column)->setValue(String::Concat("Data ",
(column + 1).ToString()));
}
// Generate the XLS file
workbook->easy_WriteXLSFile("C:\\Samples\\Excel.xls");
Click here to see Continuous Code Listing
// Create an instance of the class that imports XLS files
ExcelDocument workbook = new ExcelDocument();
// Import XLS file
FileInputStream file = new FileInputStream("C:\\Samples\\Excel to import.xls");
workbook.easy_LoadXLSFile(file);
// 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 Tutorial36");
for (int column=0; column<5; column++)
{
xlsSecondTable.easy_getCell(1, column).setValue("Data " + (column + 1));
}
// Generate the XLS file
workbook.easy_WriteXLSFile("C:\\Samples\\Excel.xls");
.NET:// Create an instance of the class that imports XLS files
$workbook = new COM("EasyXLS.ExcelDocument");
// Import XLS file
$workbook->easy_LoadXLSFile("C:\\Samples\\Excel to import.xls");
// 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 Tutorial37");
for ($column=0; $column<5; $column++)
{
$xlsSecondTable->easy_getCell(1, $column)->setValue("Data " . ($column + 1));
}
// Generate the XLS file
$workbook->easy_WriteXLSFile("C:\Samples\Excel.xls");
Click here to see Continuous Code ListingJava:// Create an instance of the class that imports XLS files
$workbook = new java("EasyXLS.ExcelDocument");
// Import XLS file
$workbook->easy_LoadXLSFile("C:\\Samples\\Excel to import.xls");
// 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 Tutorial37");
for ($column=0; $column<5; $column++)
{
$xlsSecondTable->easy_getCell(1, $column)->setValue("Data " . ($column + 1));
}
// Generate the XLS file
$workbook->easy_WriteXLSFile("C:\Samples\Excel.xls");
Click here to see Continuous Code Listing
' Create an instance of the class that imports XLS filesset workbook = Server.CreateObject("EasyXLS.ExcelDocument")
' Import XLS file
workbook.easy_LoadXLSFile("C:\Samples\Excel to import.xls")
' 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 Tutorial36")
for column=0 to 4
xlsSecondTable.easy_getCell(1, column).setValue("Data " & (column + 1))
next' Generate the XLS file
workbook.easy_WriteXLSFile ("C:\Samples\Excel.xls")
' Create an instance of the class that imports XLS filesSet workbook = CreateObject("EasyXLS.ExcelDocument")
' Import XLS file
workbook.easy_LoadXLSFile("C:\Samples\Excel to import.xls")
' 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 Tutorial36")
For Column = 0 To 4
xlsSecondTable.easy_getCell(1, Column).setValue ("Data " & (Column + 1))
Next' Generate the XLS file
workbook.easy_WriteXLSFile ("C:\Samples\Excel.xls")
' Create an instance of the class that imports XLS filesset workbook = CreateObject("EasyXLS.ExcelDocument")
' Import XLS file
workbook.easy_LoadXLSFile("C:\Samples\Excel to import.xls")
' 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 Tutorial36")
for column=0 to 4
xlsSecondTable.easy_getCell(1, column).setValue("Data " & (column + 1))
next' Generate the XLS file
workbook.easy_WriteXLSFile ("C:\Samples\Excel.xls")
<!-- Create an instance of the class that imports XLS files --><cfobject type="java"class="EasyXLS.ExcelDocument"name="workbook"action="CREATE">
<!-- Import XLS file --><cfset success = workbook.easy_LoadXLSFile("C:\Samples\Excel to import.xls")><!-- Get the table of the second worksheet --><cfset xlsSecondTable = workbook.easy_getSheet("Second tab").easy_getExcelTable()><!-- Add more data to the second sheet --><cfset xlsSecondTable.easy_getCell("A1").setValue("Data added by Tutorial36")><cfloop from="0" to="4" index="column"><cfset xlsSecondTable.easy_getCell(1,evaluate(column)).setValue("Data " &
evaluate(column + 1))></cfloop><!-- Generate the XLS file --><cfset ret = workbook.easy_WriteXLSFile("C:\Samples\Excel.xls")>
.NET:# Create an instance of the class that reads Excel files
workbook = ExcelDocument()
# Read XLS fileif workbook.easy_LoadXLSFile("C:\\Samples\\Excel to import.xls"):
# 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 Tutorial37")
for column in range(5):
xlsSecondTable.easy_getCell(1, column).setValue("Data " + str(column + 1))
# Export the new XLS file
workbook.easy_WriteXLSFile("C:\\Samples\\Excel.xls")
Click here to see Continuous Code ListingJava:# Create an instance of the class that reads Excel files
workbook = gateway.jvm.ExcelDocument()
# Read XLS fileif workbook.easy_LoadXLSFile("C:\\Samples\\Excel to import.xls"):
# 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 Tutorial37")
for column in range(5):
xlsSecondTable.easy_getCell(1, column).setValue("Data " + str(column + 1))
# Export the new XLS file
workbook.easy_WriteXLSFile("C:\\Samples\\Excel.xls")
Click here to see Continuous Code Listing
The screen shots below provide an example of the imported XLS file, modified and saved back to another XLS file. The file has two worksheets (First Tab and Second Tab). The data is added to the second worksheet.
and:
Import data from XLS 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 XLS files
ExcelDocument workbook = new ExcelDocument();
// Import XLS file
DataSet ds = workbook.easy_ReadXLSActiveSheet_AsDataSet("C:\\Samples\\Excel file.xls");
' Create an instance of the class that imports XLS filesDim workbook As New ExcelDocument
' Import XLS fileDim ds As DataSet = workbook.easy_ReadXLSActiveSheet_AsDataSet("C:\Samples\Excel file.xls")
// Create an instance of the class that imports XLS files
ExcelDocument ^workbook = gcnew ExcelDocument();
// Import XLS file
DataSet ^ds = workbook->easy_ReadXLSActiveSheet_AsDataSet("C:\\Samples\\Excel file.xls");
// Create an instance of the class that imports XLS files
ExcelDocument workbook = new ExcelDocument();
// Import XLS file
ResultSet rs = workbook.easy_ReadXLSActiveSheet_AsResultSet("C:\\Samples\\Excel file.xls");
.NET:// Create an instance of the class that imports XLS files
$workbook = new COM("EasyXLS.ExcelDocument");
// Import XLS file
$rows = $workbook->easy_ReadXLSActiveSheet_AsList("C:\\Samples\\Excel file.xls");
Java:// Create an instance of the class that imports XLS files
$workbook = new java("EasyXLS.ExcelDocument");
// Import XLS file
$rows = $workbook->easy_ReadXLSActiveSheet_AsList("C:\\Samples\\Excel file.xls");
' Create an instance of the class that imports XLS filesset workbook = Server.CreateObject("EasyXLS.ExcelDocument")
' Import XLS fileSet rows = workbook.easy_ReadXLSActiveSheet_AsList("C:\\Samples\\Excel file.xls")
// Create an instance of the class that imports XLS files
EasyXLS::IExcelDocumentPtr workbook;
hr = CoCreateInstance(__uuidof(EasyXLS::ExcelDocument),
NULL,
CLSCTX_ALL,
__uuidof(EasyXLS::IExcelDocument),
(void**) &workbook);
// Import XLS file
EasyXLS::IListPtr rows =
workbook->easy_ReadXLSActiveSheet_AsList("C:\\Samples\\Excel file.xls");
' Create an instance of the class that imports XLS filesSet workbook = CreateObject("EasyXLS.ExcelDocument")
' Import XLS fileSet rows = workbook.easy_ReadXLSActiveSheet_AsList("C:\\Samples\\Excel file.xls")
' Create an instance of the class that imports XLS filesSet workbook = CreateObject("EasyXLS.ExcelDocument")
' Import XLS fileSet rows = workbook.easy_ReadXLSActiveSheet_AsList("C:\\Samples\\Excel file.xls")
<!-- Create an instance of the class that imports XLS files --><cfobject type="java"class="EasyXLS.ExcelDocument"name="workbook"action="CREATE"><!-- Import XLS file--><cfset rs = workbook.easy_ReadXLSActiveSheet_AsResultSet("C:\\Samples\\Excel file.xls")>
.NET:# Create an instance of the class that imports XLS files
workbook = ExcelDocument()
# Import XLS file
ds = workbook.easy_ReadXLSActiveSheet_AsDataSet("C:\\Samples\\Excel file.xls")
Java:# Create an instance of the class that imports XLS files
workbook = gateway.jvm.ExcelDocument()
# Import XLS file
rows = workbook.easy_ReadXLSActiveSheet_AsList("C:\\Samples\\Excel file.xls")
Import data from XLS file having one sheet
Importing data from an XLS file, if the XLS file has only one sheet, is the easiest approach. The Excel data can be imported with one single line of code using ExcelDocument.easy_ReadXLSActiveSheet_AsDataSet method. The above code sample shows how to achieve this goal.
Import data from XLS file having multiple sheets
There are three approaches for importing data from an XLS file with multiple sheets.
I. Usually the first sheet is the active sheet inside an XLS file. If this is your case or if you are importing data from another active sheet use ExcelDocument.easy_ReadXLSActiveSheet_AsDataSet method.
EasyXLS enables you to import Excel data either from the entire sheet or from ranges of cells. Importing only a range of cells is a very useful option especially for large XLS 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 XLS file to DataSet or ResultSet have parameters that permit importing only ranges of cells.
// Create an instance of the class that imports XLS files
ExcelDocument workbook = new ExcelDocument();
// Import cell range data from XLS file
DataSet ds = workbook.easy_ReadXLSSheet_AsDataSet(
"C:\\Samples\\Excel file.xls", "Sheet1", "A1:B10");
' Create an instance of the class that imports XLS filesDim workbook As New ExcelDocument
' Import cell range data from XLS fileDim ds = workbook.easy_ReadXLSSheet_AsDataSet(
"C:\Samples\Excel file.xls", "Sheet1", "A1:B10")
// Create an instance of the class that imports XLS files
ExcelDocument ^workbook = gcnew ExcelDocument();
// Import cell range data from XLS file
DataSet ^ds = workbook->easy_ReadXLSSheet_AsDataSet(
"C:\\Samples\\Excel file.xls", "Sheet1", "A1:B10");
// Create an instance of the class that imports XLS files
ExcelDocument workbook = new ExcelDocument();
// Import cell range data from XLS file
DataSet ds = workbook.easy_ReadXLSSheet_AsResultSet(
"C:\\Samples\\Excel file.xls", "Sheet1", "A1:B10");
.NET:// Create an instance of the class that imports XLS files
$workbook = new COM("EasyXLS.ExcelDocument");
// Import cell range data from XLS file
$rows = $workbook->easy_ReadXLSSheet_AsList_5(
"C:\\Samples\\Excel file.xls", "Sheet1", "A1:B10");
Java:// Create an instance of the class that imports XLS files
$workbook = new java("EasyXLS.ExcelDocument");
// Import cell range data from XLS file
$rows = $workbook->easy_ReadXLSSheet_AsList(
"C:\\Samples\\Excel file.xls", "Sheet1", "A1:B10");
' Create an instance of the class that imports XLS filesset workbook = Server.CreateObject("EasyXLS.ExcelDocument")
' Import cell range data from XLS fileset rows = workbook.easy_ReadXLSSheet_AsList_5(_
"C:\\Samples\\Excel file.xls", "Sheet1", "A1:B10")
// Create an instance of the class that imports XLS files
EasyXLS::IExcelDocumentPtr workbook;
hr = CoCreateInstance(__uuidof(EasyXLS::ExcelDocument),
NULL,
CLSCTX_ALL,
__uuidof(EasyXLS::IExcelDocument),
(void**) &workbook);
// Import cell range data from XLS file
EasyXLS::IListPtr rows =
workbook->easy_ReadXLSSheet_AsList_5(
"C:\\Samples\\Excel file.xls", "Sheet1", "A1:B10");
' Create an instance of the class that imports XLS filesSet workbook = CreateObject("EasyXLS.ExcelDocument")
' Import cell range data from XLS fileSet rows = workbook.easy_ReadXLSSheet_AsList_5( _
"C:\Samples\Excel file.xls", "Sheet1", "A1:B10")
' Create an instance of the class that imports XLS filesSet workbook = CreateObject("EasyXLS.ExcelDocument")
' Import cell range data from XLS fileSet rows = workbook.easy_ReadXLSSheet_AsList_5( _
"C:\\Samples\\Excel file.xls", "Sheet1", "A1:B10")
<!-- Create an instance of the class that imports XLS files --><cfobject type="java"class="EasyXLS.ExcelDocument"name="workbook"action="CREATE"><!-- Import cell range data from XLS file--><cfset rows = workbook.easy_ReadXLSSheet_AsResultSet(
"C:\\Samples\\Excel file.xls", "Sheet1", "A1:B10")>
.NET:# Create an instance of the class that imports XLS files
workbook = ExcelDocument()
# Import cell range data from XLS file
rows = workbook.easy_ReadXLSSheet_AsList(
"C:\\Samples\\Excel file.xls", "Sheet1", "A1:B10")
Java:# Create an instance of the class that imports XLS files
workbook = gateway.jvm.ExcelDocument()
# Import cell range data from XLS file
rows = workbook.easy_ReadXLSSheet_AsList(
"C:\\Samples\\Excel file.xls", "Sheet1", "A1:B10")
Import all XLS file structures
EasyXLS allows you to import the whole XLS 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 XLS 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.