EasyXLS™ library allows you to export data to Excel file. It enables the user to set the cell values and the data type of the cell by using the simple methods from ExcelCell class.
The default data type for the cells can be determined automatically, but we recommend using a defined one such as string, numeric, date, boolean or error because the use of an automatic data type can be resource and time consuming when exporting large Excel files.
EasyXLS also allows to import data from Excel file. The cell values can be accessed by using methods from ExcelCell class.
The component supports the following data types:
String values
A string value is any finite sequence of characters (i.e., letters, numerals, symbols and punctuation marks).
Numeric values
A numeric value can be an integer, float or double number. The numeric values can be formatted with currency format, date format, accounting format, percentage format, fraction format or any custom format.
A date value is a calendar date that can include also the time. We recommend entering the date values in "MM/dd/yyyy HH:mm:ss" format (i.e. 12/23/2005 22:45:21) for optimization purposes. Later, to apply a different format, the cell can be formatted using the desired date format.
The below example shows how to export data to Excel file.
The source code samples can be integrated in:
- ASP.NET web pages - Windows applications - Windows Forms (WinForms) - Console applications - Windows service applications - ASP.NET MVC web applications - PHP and ASP web pages - Java applications
// Create an instance of the class that exports Excel files, having multiple sheets
ExcelDocument workbook = new ExcelDocument(2);
// Set the sheet names
workbook.easy_getSheetAt(0).setSheetName("First tab");
workbook.easy_getSheetAt(1).setSheetName("Second tab");
// Get the table of data for the first sheet
ExcelTable xlsFirstTable =
((ExcelWorksheet)workbook.easy_getSheetAt(0)).easy_getExcelTable();
// Add data in cells for report header
for (int column=0; column<5; column++)
{
xlsFirstTable.easy_getCell(0,column).setValue("Column " + (column + 1));
xlsFirstTable.easy_getCell(0,column).setDataType(DataType.STRING);
}
// Add data in cells for report values
for (int row=0; row<100; row++)
{
for (int column=0; column<5; column++)
{
xlsFirstTable.easy_getCell(row+1,column).setValue(
"Data " + (row + 1) + ", " + (column + 1));
xlsFirstTable.easy_getCell(row+1,column).setDataType(DataType.STRING);
}
}
// Export Excel file
workbook.easy_WriteXLSXFile("C:\\Samples\\Export data to Excel.xlsx");
' Create an instance of the class that exports Excel files, having multiple sheets
Dim workbook As New ExcelDocument(2)
' Set the sheet names
workbook.easy_getSheetAt(0).setSheetName("First tab")
workbook.easy_getSheetAt(1).setSheetName("Second tab")
' Get the table of data for the first sheet
Dim xlsFirstTab As ExcelWorksheet = workbook.easy_getSheetAt(0)
Dim xlsFirstTable = xlsFirstTab.easy_getExcelTable()
' Add data in cells for report header
For column As Integer = 0 To 4
xlsFirstTable.easy_getCell(0, column).setValue("Column " & (column + 1))
xlsFirstTable.easy_getCell(0, column).setDataType(DataType.STRING)
Next
' Add data in cells for report values
For row As Integer = 0 To 99
For column As Integer = 0 To 4
xlsFirstTable.easy_getCell(row + 1, column).setValue( _
"Data " & (row + 1) & ", " & (column + 1))
xlsFirstTable.easy_getCell(row + 1, column).setDataType(DataType.STRING)
Next
Next
' Export Excel file
workbook.easy_WriteXLSXFile("C:\Samples\Export data to Excel.xlsx")
C++
// Create an instance of the class that exports Excel files
EasyXLS::IExcelDocumentPtr workbook;
hr = CoCreateInstance(__uuidof(EasyXLS::ExcelDocument),
NULL,
CLSCTX_ALL,
__uuidof(EasyXLS::IExcelDocument),
(void**) &workbook) ;
// Create two sheets
workbook->easy_addWorksheet_2("First tab");
workbook->easy_addWorksheet_2("Second tab");
// Get the table of data for the first sheet
EasyXLS::IExcelWorksheetPtr xlsFirstTab =
(EasyXLS::IExcelWorksheetPtr)workbook->easy_getSheetAt(0);
EasyXLS::IExcelTablePtr xlsFirstTable = xlsFirstTab->easy_getExcelTable();
// Add data in cells for report header
char* cellValue = (char*)malloc(11*sizeof(char));
char* columnNumber = (char*)malloc(sizeof(char));
for (int column=0; column<5; column++)
{
strcpy(cellValue, "Column ");
_itoa(column+ 1, columnNumber , 10);
xlsFirstTable->easy_getCell(0,column)->setValue( strcat(cellValue, columnNumber));
xlsFirstTable->easy_getCell(0,column)->setDataType(DATATYPE_STRING);
}
// Add data in cells for report values
char* rowNumber = (char*)malloc(sizeof(char));
for (int row=0; row<100; row++)
{
for (int column=0; column<5; column++)
{
strcpy(cellValue, "Data ");
_itoa(column+ 1, columnNumber , 10);
_itoa(row + 1, rowNumber , 10);
strcat(cellValue, rowNumber);
strcat(cellValue, ", ");
strcat(cellValue, columnNumber);
xlsFirstTable->easy_getCell(row+1,column)->setValue(cellValue);
xlsFirstTable->easy_getCell(row+1,column)->setDataType(DATATYPE_STRING);
}
}
// Export Excel file
workbook->easy_WriteXLSXFile("C:\\Samples\\Export data to Excel.xlsx");
Click here to see Continuous Code ListingC++.NET
// Create an instance of the class that exports Excel files, having multiple sheets
ExcelDocument *workbook = new ExcelDocument(2);
// Set the sheet names
workbook->easy_getSheetAt(0)->setSheetName("First tab");
workbook->easy_getSheetAt(1)->setSheetName("Second tab");
// Get the table of data for the first sheet
ExcelWorksheet *xlsFirstTab =
__try_cast<ExcelWorksheet*>(workbook->easy_getSheetAt(0));
ExcelTable *xlsFirstTable = xlsFirstTab->easy_getExcelTable();
// Add data in cells for report header
for (int column=0; column<5; column++)
{
xlsFirstTable->easy_getCell(0,column)->setValue(
String::Concat("Column ",(column + 1).ToString()));
xlsFirstTable->easy_getCell(0,column)->setDataType(DataType::STRING);
}
// Add data in cells for report values
for (int row=0; row<100; row++)
{
for (int column=0; column<5; column++)
{
xlsFirstTable->easy_getCell(row+1,column)->setValue(String::Concat(
"Data ", (row + 1).ToString(), ", ", (column + 1).ToString()));
xlsFirstTable->easy_getCell(row+1,column)->setDataType(DataType::STRING);
}
}
// Export Excel file
workbook->easy_WriteXLSXFile("C:\\Samples\\Export data to Excel.xlsx");
Click here to see Continuous Code Listing
// Create an instance of the class that exports Excel files, having multiple sheets
ExcelDocument workbook = new ExcelDocument(2);
// Set the sheet names
workbook.easy_getSheetAt(0).setSheetName("First tab");
workbook.easy_getSheetAt(1).setSheetName("Second tab");
// Get the table of data for the first sheet
ExcelTable xlsFirstTable =
((ExcelWorksheet)workbook.easy_getSheetAt(0)).easy_getExcelTable();
// Add data in cells for report header
for (int column=0; column<5; column++)
{
xlsFirstTable.easy_getCell(0,column).setValue("Column " + (column + 1));
xlsFirstTable.easy_getCell(0,column).setDataType(DataType.STRING);
}
// Add data in cells for report values
for (int row=0; row<100; row++)
{
for (int column=0; column<5; column++)
{
xlsFirstTable.easy_getCell(row+1,column).setValue(
"Data " + (row + 1) + ", " + (column + 1));
xlsFirstTable.easy_getCell(row+1,column).setDataType(DataType.STRING);
}
}
// Export Excel file
workbook.easy_WriteXLSXFile("C:\\Samples\\Export data to Excel.xlsx");
// Create an instance of the class that exports Excel files
$workbook = new COM("EasyXLS.ExcelDocument");
// Create the worksheets
$workbook->easy_addWorksheet_2("First tab");
$workbook->easy_addWorksheet_2("Second tab");
// Get the table of data for the first sheet
$xlsFirstTable = $workbook->easy_getSheetAt(0)->easy_getExcelTable();
// Add data in cells for report header
for ($column=0; $column<5; $column++)
{
$xlsFirstTable->easy_getCell(0,$column)->setValue("Column " . ($column + 1));
$xlsFirstTable->easy_getCell(0,$column)->setDataType($DATATYPE_STRING);
}
// Add data in cells for report values
for ($row=0; $row<100; $row++)
{
for ($column=0; $column<5; $column++)
{
$xlsFirstTable->easy_getCell($row+1,$column)->setValue(
"Data ".($row + 1).", ".($column + 1));
$xlsFirstTable->easy_getCell($row+1,$column)->setDataType($DATATYPE_STRING);
}
}
// Export Excel file
$workbook->easy_WriteXLSXFile("C:\Samples\Export data to Excel.xlsx");
' Create an instance of the class that exports Excel files
set workbook = Server.CreateObject("EasyXLS.ExcelDocument")
' Create the worksheets
workbook.easy_addWorksheet_2("First tab")
workbook.easy_addWorksheet_2("Second tab")
' Get the table of data for the first sheet
Set xlsFirstTable = workbook.easy_getSheetAt(0).easy_getExcelTable()
' Add data in cells for report header
for column = 0 to 4
xlsFirstTable.easy_getCell(0,column).setValue("Column " & (column + 1))
xlsFirstTable.easy_getCell(0,column).setDataType(DATATYPE_STRING)
next
' Add data in cells for report values
for row = 0 to 99
for column = 0 to 4
xlsFirstTable.easy_getCell(row+1,column).setValue( _
"Data " & (row + 1) & ", " & (column + 1))
xlsFirstTable.easy_getCell(row+1,column).setDataType(DATATYPE_STRING)
next
next
' Export Excel file
workbook.easy_WriteXLSXFile ("C:\Samples\Export data to Excel.xlsx")
' Create an instance of the class that exports Excel files
Set workbook = CreateObject("EasyXLS.ExcelDocument")
' Create two sheets
workbook.easy_addWorksheet_2 ("First tab")
workbook.easy_addWorksheet_2 ("Second tab")
' Get the table of data for the first sheet
Set xlsFirstTable = workbook.easy_getSheetAt(0).easy_getExcelTable()
' Add data in cells for report header
For Column = 0 To 4
xlsFirstTable.easy_getCell(0, Column).setValue ("Column " & (Column + 1))
xlsFirstTable.easy_getCell(0, Column).setDataType (DataType.DATATYPE_STRING)
Next
' Add data in cells for report values
For row = 0 To 99
For Column = 0 To 4
xlsFirstTable.easy_getCell(row + 1, Column).setValue ( _
"Data " & (row + 1) & ", " & (Column + 1))
xlsFirstTable.easy_getCell(row + 1, Column).setDataType ( _
DataType.DATATYPE_STRING)
Next
Next
' Export Excel file
workbook.easy_WriteXLSXFile ("C:\Samples\Export data to Excel.xlsx")
' Create an instance of the class that exports Excel files
Set workbook = CreateObject("EasyXLS.ExcelDocument")
' Create two sheets
workbook.easy_addWorksheet_2("First tab")
workbook.easy_addWorksheet_2("Second tab")
' Get the table of data for the first sheet
Set xlsFirstTable = workbook.easy_getSheetAt(0).easy_getExcelTable()
' Add data in cells for report header
For Column = 0 To 4
xlsFirstTable.easy_getCell(0,column).setValue("Column " & (Column + 1))
xlsFirstTable.easy_getCell(0,column).setDataType(DATATYPE_STRING)
Next
' Add data in cells for report values
For row = 0 To 99
For column = 0 To 4
xlsFirstTable.easy_getCell(row+1,column).setValue( _
"Data " & (row + 1) & ", " & (column + 1))
xlsFirstTable.easy_getCell(row+1,column).setDataType(DATATYPE_STRING)
Next
Next
' Export Excel file
workbook.easy_WriteXLSXFile ("C:\Samples\Export data to Excel.xlsx")
.NET:
# Create an instance of the class that exports Excel files, having two sheets
workbook = ExcelDocument(2)
# Set the sheet names
workbook.easy_getSheetAt(0).setSheetName("First tab")
workbook.easy_getSheetAt(1).setSheetName("Second tab")
# Get the table of data for the first worksheet
xlsFirstTable = workbook.easy_getSheetAt(0).easy_getExcelTable()
# Add data in cells for report header
for column in range(5):
xlsFirstTable.easy_getCell(0,column).setValue("Column " + str(column + 1))
xlsFirstTable.easy_getCell(0,column).setDataType(DataType.STRING)
# Add data in cells for report values
for row in range(100):
for column in range(5):
xlsFirstTable.easy_getCell(row+1,column).setValue("Data " + str(row + 1) + ", " + str(column + 1))
xlsFirstTable.easy_getCell(row+1,column).setDataType(DataType.STRING)
# Export the XLSX file
workbook.easy_WriteXLSXFile("C:\\Samples\\Export data to Excel.xlsx")
Click here to see Continuous Code ListingJava:
# Create an instance of the class that exports Excel files, having two sheets
workbook = gateway.jvm.ExcelDocument(2)
# Set the sheet names
workbook.easy_getSheetAt(0).setSheetName("First tab")
workbook.easy_getSheetAt(1).setSheetName("Second tab")
# Get the table of data for the first worksheet
xlsFirstTable = workbook.easy_getSheetAt(0).easy_getExcelTable()
# Add data in cells for report header
for column in range(5):
xlsFirstTable.easy_getCell(0,column).setValue("Column " + str(column + 1))
xlsFirstTable.easy_getCell(0,column).setDataType(gateway.jvm.DataType.STRING)
# Add data in cells for report values
for row in range(100):
for column in range(5):
xlsFirstTable.easy_getCell(row+1,column).setValue("Data " + str(row + 1) + ", " + str(column + 1))
xlsFirstTable.easy_getCell(row+1,column).setDataType(gateway.jvm.DataType.STRING)
# Export the XLSX file
workbook.easy_WriteXLSXFile("C:\\Samples\\Export data to Excel.xlsx")
Click here to see Continuous Code Listing
The screen shot below represents the exported data to Excel file generated by the code sample above. The file has two worksheets (First Tab and Second Tab). The values and the data type of the cells from the first worksheet are set.
Export data to Excel file
EasyXLS allows you to export data to Excel file. The above code sample shows how to achieve this goal.