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