Import XLSX file in .NET, Java and other programming languages
EasyXLS™ library allows you to import data from an XLSX file or other sheet elements like cell formatting, charts, pivot tables, comments, images and others.
EasyXLS can be successfully used to also import large XLSX files having big volume of data with fast importing time.
EasyXLS allows loading an XLSX document. Once the XLSX 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 XLSX format or other format (XLS, XLSB, XML Spreadsheet, XML, TXT, CSV or HTML). It can be used to load an already formatted XLSX file as a template, add data and save back the file in XLSX format. In this way, a lot of written code that formats the XLSX 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 XLSX file. The source code shows how to load and import an XLSX file that is used as a base template, add more data to the XLSX file and save the new workbook. After that, the best techniques about importing data from XLSX file like importing Excel data to DataTable, GridView, DataGridView, DataSet, ResultSet, DataGrid and List are shown.
// Create an instance of the class that imports XLSX files
ExcelDocument workbook = new ExcelDocument();
// Import XLSX file
workbook.easy_LoadXLSXFile("C:\\Samples\\Excel to import.xlsx");
// 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 XLSX file
workbook.easy_WriteXLSXFile("C:\\Samples\\Excel.xlsx");
' Create an instance of the class that imports XLSX filesDim workbook As New ExcelDocument
' Import XLSX file
workbook.easy_LoadXLSXFile("C:\Samples\Excel to import.xlsx")
' 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 XLSX file
workbook.easy_WriteXLSXFile("C:\Samples\Excel.xlsx")
C++// Create an instance of the class that imports XLSX files
EasyXLS::IExcelDocumentPtr workbook;
hr = CoCreateInstance(__uuidof(EasyXLS::ExcelDocument),
NULL,
CLSCTX_ALL,
__uuidof(EasyXLS::IExcelDocument),
(void**) &workbook) ;
// Import XLSX file
workbook->easy_LoadXLSXFile("C:\\Samples\\Excel to import.xlsx");
// 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 XLSX file
workbook->easy_WriteXLSXFile("C:\\Samples\\Excel.xlsx");
Click here to see Continuous Code ListingC++.NET// Create an instance of the class that imports XLSX files
ExcelDocument ^workbook = gcnew ExcelDocument();
// Import XLSX file
workbook->easy_LoadXLSXFile("C:\\Samples\\Excel to import.xlsx");
// Get the table of the second worksheet
ExcelWorksheet ^xlsSecondTab =
safe_cast<ExcelWorksheet^>(workbook->easy_getSheet("Second tab"));
ExcelTable ^xlsSecondTable = xlsSecondTab->easy_getExcelTable();
// Write some 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 XLSX file
workbook->easy_WriteXLSXFile("C:\\Samples\\Excel.xlsx");
Click here to see Continuous Code Listing
// Create an instance of the class that imports XLSX files
ExcelDocument workbook = new ExcelDocument();
// Import XLSX file
FileInputStream file = new FileInputStream("C:\\Samples\\Excel to import.xlsx");
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 XLSX file
workbook.easy_WriteXLSXFile("C:\\Samples\\Excel.xlsx");
.NET:// Create an instance of the class that imports XLSX files
$workbook = new COM("EasyXLS.ExcelDocument");
// Import XLSX file
$workbook->easy_LoadXLSXFile("C:\\Samples\\Excel to import.xlsx");
// 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 XLSX file
$workbook->easy_WriteXLSXFile("C:\Samples\Excel.xlsx");
Click here to see Continuous Code ListingJava:// Create an instance of the class that imports XLSX files
$workbook = new java("EasyXLS.ExcelDocument");
// Import XLSX file
$workbook->easy_LoadXLSXFile("C:\\Samples\\Excel to import.xlsx");
// 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 XLSX file
$workbook->easy_WriteXLSXFile("C:\Samples\Excel.xlsx");
Click here to see Continuous Code Listing
' Create an instance of the class that imports XLSX filesset workbook = Server.CreateObject("EasyXLS.ExcelDocument")
' Import XLSX file
workbook.easy_LoadXLSXFile("C:\Samples\Excel to import.xlsx")
' 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 XLSX file
workbook.easy_WriteXLSXFile ("C:\Samples\Excel.xlsx")
' Create an instance of the class that imports XLSX filesSet workbook = CreateObject("EasyXLS.ExcelDocument")
' Import XLSX file
workbook.easy_LoadXLSXFile("C:\Samples\Excel to import.xlsx")
' 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 XLSX file
workbook.easy_WriteXLSXFile ("C:\Samples\Excel.xlsx")
' Create an instance of the class that imports XLSX filesset workbook = CreateObject("EasyXLS.ExcelDocument")
' Import XLSX file
workbook.easy_LoadXLSXFile("C:\Samples\Excel to import.xlsx")
' 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 XLSX file
workbook.easy_WriteXLSXFile ("C:\Samples\Excel.xlsx")
<!-- Create an instance of the class that imports XLSX files --><cfobject type="java"class="EasyXLS.ExcelDocument"name="workbook"action="CREATE">
<!-- Import XLSX file --><cfset success = workbook.easy_LoadXLSXFile("C:\Samples\Excel to import.xlsx")><!-- 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 XLSX file --><cfset ret = workbook.easy_WriteXLSXFile("C:\Samples\Excel.xlsx")>
.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.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.xlsx")
Click here to see Continuous Code Listing
The screen shots below provide an example of the imported XLSX file, modified and saved back to another XLSX file. The file has two worksheets (First Tab and Second Tab). The data is added to the second worksheet.
and:
Import data from XLSX 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 XLSX files
ExcelDocument workbook = new ExcelDocument();
// Import XLSX file
DataSet ds = workbook.easy_ReadXLSXActiveSheet_AsDataSet(
"C:\\Samples\\Excel file.xlsx");
' Create an instance of the class that imports XLSX filesDim workbook As New ExcelDocument
' Import XLSX fileDim ds As DataSet = workbook.easy_ReadXLSXActiveSheet_AsDataSet(
"C:\Samples\Excel file.xlsx")
C++// Create an instance of the class that imports XLSX files
EasyXLS::IExcelDocumentPtr workbook;
hr = CoCreateInstance(__uuidof(EasyXLS::ExcelDocument),
NULL,
CLSCTX_ALL,
__uuidof(EasyXLS::IExcelDocument),
(void**) &workbook);
// Import XLSX file
EasyXLS::IListPtr rows =
workbook->easy_ReadXLSXActiveSheet_AsList("C:\\Samples\\Excel file.xlsx");
C++.NET// Create an instance of the class that imports XLSX files
ExcelDocument ^workbook = gcnew ExcelDocument();
// Import XLSX file
DataSet ^ds = workbook->easy_ReadXLSXActiveSheet_AsDataSet(
"C:\\Samples\\Excel file.xlsx");
// Create an instance of the class that imports XLSX files
ExcelDocument workbook = new ExcelDocument();
// Import XLSX file
ResultSet rs = workbook.easy_ReadXLSXActiveSheet_AsResultSet(
"C:\\Samples\\Excel file.xlsx");
.NET:// Create an instance of the class that imports XLSX files
$workbook = new COM("EasyXLS.ExcelDocument");
// Import XLSX file
$rows = $workbook->easy_ReadXLSXActiveSheet_AsList("C:\\Samples\\Excel file.xlsx");
Java:// Create an instance of the class that imports XLSX files
$workbook = new java("EasyXLS.ExcelDocument");
// Import XLSX file
$rows = $workbook->easy_ReadXLSXActiveSheet_AsList("C:\\Samples\\Excel file.xlsx");
' Create an instance of the class that imports XLSX filesset workbook = Server.CreateObject("EasyXLS.ExcelDocument")
' Import XLSX fileSet rows = workbook.easy_ReadXLSXActiveSheet_AsList("C:\\Samples\\Excel file.xlsx")
' Create an instance of the class that imports XLSX filesSet workbook = CreateObject("EasyXLS.ExcelDocument")
' Import XLSX fileSet rows = workbook.easy_ReadXLSXActiveSheet_AsList("C:\\Samples\\Excel file.xlsx")
' Create an instance of the class that imports XLSX filesSet workbook = CreateObject("EasyXLS.ExcelDocument")
' Import XLSX fileSet rows = workbook.easy_ReadXLSXActiveSheet_AsList("C:\\Samples\\Excel file.xlsx")
<!-- Create an instance of the class that imports XLSX files --><cfobject type="java"class="EasyXLS.ExcelDocument"name="workbook"action="CREATE">
<!-- Import XLSX file--><cfset rs = workbook.easy_ReadXLSXActiveSheet_AsResultSet(
"C:\\Samples\\Excel file.xlsx")>
.NET:# Create an instance of the class that imports XLSX files
workbook = ExcelDocument()
# Import XLSX file
ds = workbook.easy_ReadXLSXActiveSheet_AsDataSet("C:\\Samples\\Excel file.xlsx")
Java:# Create an instance of the class that imports XLSX files
workbook = gateway.jvm.ExcelDocument()
# Import XLSX file
rows = workbook.easy_ReadXLSXActiveSheet_AsList("C:\\Samples\\Excel file.xlsx")
Import data from XLSX file having one sheet
Importing data from an XLSX file, if the XLSX 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 XLSX file having multiple sheets
There are three approaches for importing data from an XLSX file with multiple sheets.
I. Usually the first sheet is the active sheet inside an XLSX 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 ranges of cells. Importing only a range of cells is a very useful option especially for large XLSX 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 XLSX file to DataSet or ResultSet have parameters that permit importing only ranges of cells.
// Create an instance of the class that imports XLSX files
ExcelDocument workbook = new ExcelDocument();
// Import cell range data from XLSX file
DataSet ds = workbook.easy_ReadXLSXSheet_AsDataSet(
"C:\\Samples\\Excel file.xlsx", "Sheet1", "A1:B10");
' Create an instance of the class that imports XLSX filesDim workbook As New ExcelDocument
' Import cell range data from XLSX fileDim ds = workbook.easy_ReadXLSXSheet_AsDataSet(
"C:\Samples\Excel file.xlsx", "Sheet1", "A1:B10")
C++// Create an instance of the class that imports XLSX files
EasyXLS::IExcelDocumentPtr workbook;
hr = CoCreateInstance(__uuidof(EasyXLS::ExcelDocument),
NULL,
CLSCTX_ALL,
__uuidof(EasyXLS::IExcelDocument),
(void**) &workbook);
// Import cell range data from XLSX file
EasyXLS::IListPtr rows =
workbook->easy_ReadXLSXSheet_AsList_5(
"C:\\Samples\\Excel file.xlsx", "Sheet1", "A1:B10");
C++.NET// Create an instance of the class that imports XLSX files
ExcelDocument ^workbook = gcnew ExcelDocument();
// Import cell range data from XLSX file
DataSet ^ds = workbook->easy_ReadXLSXSheet_AsDataSet(
"C:\\Samples\\Excel file.xlsx", "Sheet1", "A1:B10");
// Create an instance of the class that imports XLSX files
ExcelDocument workbook = new ExcelDocument();
// Import cell range data from XLSX file
DataSet ds = workbook.easy_ReadXLSXSheet_AsResultSet(
"C:\\Samples\\Excel file.xlsx", "Sheet1", "A1:B10");
.NET:// Create an instance of the class that imports XLSX files
$workbook = new COM("EasyXLS.ExcelDocument");
// Import cell range data from XLSX file
$rows = $workbook->easy_ReadXLSXSheet_AsList_5(
"C:\\Samples\\Excel file.xlsx", "Sheet1", "A1:B10");
Java:// Create an instance of the class that imports XLSX files
$workbook = new java("EasyXLS.ExcelDocument");
// Import cell range data from XLSX file
$rows = $workbook->easy_ReadXLSXSheet_AsList(
"C:\\Samples\\Excel file.xlsx", "Sheet1", "A1:B10");
' Create an instance of the class that imports XLSX filesset workbook = Server.CreateObject("EasyXLS.ExcelDocument")
' Import cell range data from XLSX fileset rows = workbook.easy_ReadXLSXSheet_AsList_5(_
"C:\\Samples\\Excel file.xlsx", "Sheet1", "A1:B10")
// Create an instance of the class that imports XLSX files
EasyXLS::IExcelDocumentPtr workbook;
hr = CoCreateInstance(__uuidof(EasyXLS::ExcelDocument),
NULL,
CLSCTX_ALL,
__uuidof(EasyXLS::IExcelDocument),
(void**) &workbook);
// Import cell range data from XLSX file
EasyXLS::IListPtr rows =
workbook->easy_ReadXLSXSheet_AsList_5(
"C:\\Samples\\Excel file.xlsx", "Sheet1", "A1:B10");
' Create an instance of the class that imports XLSX filesSet workbook = CreateObject("EasyXLS.ExcelDocument")
' Import cell range data from XLSX fileSet rows = workbook.easy_ReadXLSXSheet_AsList_5( _
"C:\Samples\Excel file.xlsx", "Sheet1", "A1:B10")
' Create an instance of the class that imports XLSX filesSet workbook = CreateObject("EasyXLS.ExcelDocument")
' Import cell range data from XLSX fileSet rows = workbook.easy_ReadXLSXSheet_AsList_5( _
"C:\\Samples\\Excel file.xlsx", "Sheet1", "A1:B10")
<!-- Create an instance of the class that imports XLSX files --><cfobject type="java"class="EasyXLS.ExcelDocument"name="workbook"action="CREATE"><!-- Import cell range data from XLSX file--><cfset rows = workbook.easy_ReadXLSXSheet_AsResultSet(
"C:\\Samples\\Excel file.xlsx", "Sheet1", "A1:B10")>
.NET:# Create an instance of the class that imports XLSX files
workbook = ExcelDocument()
# Import cell range data from XLSX file
rows = workbook.easy_ReadXLSXSheet_AsList(
"C:\\Samples\\Excel file.xlsx", "Sheet1", "A1:B10")
Java:# Create an instance of the class that imports XLSX files
workbook = gateway.jvm.ExcelDocument()
# Import cell range data from XLSX file
rows = workbook.easy_ReadXLSXSheet_AsList(
"C:\\Samples\\Excel file.xlsx", "Sheet1", "A1:B10")
Import all XLSX file structures
EasyXLS allows you to import the whole XLSX 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 XLSX 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.