The below example is a common code about how to export to XLSX file. After that, more code samples about exporting data from various standard data structures like DataTable, GridView, DataSet and ResultSet are shown.
// Create an instance of the class that exports XLSX file, 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 the first worksheet that will keep the data
ExcelTable xlsFirstTable =
((ExcelWorksheet)workbook.easy_getSheetAt(0)).easy_getExcelTable();
// Add data to headerfor (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 to cellsfor (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 XLSX file
workbook.easy_WriteXLSXFile("C:\\Samples\\Export to Excel.xlsx");
// Confirm generation
String sError = workbook.easy_getError();
if (sError.Equals(""))
Console.Write("\nFile successfully created. Press Enter to Exit...");
else
Console.Write("\nError encountered: " + sError + "\nPress Enter to Exit...");
' Create an instance of the class that exports XLSX file, having multiple sheetsDim 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 the first worksheet that will keep the dataDim xlsFirstTab As ExcelWorksheet = workbook.easy_getSheetAt(0)
Dim xlsFirstTable = xlsFirstTab.easy_getExcelTable()
' Add data to headerFor 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 to cellsFor 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)
NextNext' Export XLSX file
workbook.easy_WriteXLSXFile("C:\Samples\Export to Excel.xlsx")
C++// Create a pointer to the interface that exports XLSX file
EasyXLS::IExcelDocumentPtr workbook;
hr = CoCreateInstance(__uuidof(EasyXLS::ExcelDocument),
NULL,
CLSCTX_ALL,
__uuidof(EasyXLS::IExcelDocument),
(void**) &workbook) ;
if(SUCCEEDED(hr)){
// Create the worksheets
workbook->easy_addWorksheet_2("First tab");
workbook->easy_addWorksheet_2("Second tab");
// Get the table of the first worksheet that will keep the data
EasyXLS::IExcelWorksheetPtr xlsFirstTab =
(EasyXLS::IExcelWorksheetPtr)workbook->easy_getSheetAt(0);
EasyXLS::IExcelTablePtr xlsFirstTable = xlsFirstTab->easy_getExcelTable();
// Add data to headerchar* 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 to cellschar* 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 XLSX file
workbook->easy_WriteXLSXFile("C:\\Samples\\Export to Excel.xlsx");
//Confirm generation
_bstr_t sError = workbook->easy_getError();
if (strcmp(sError, "") == 0){
printf("\nFile successfully created. Press Enter to Exit...");
}
else{
printf("\nError encountered: %s", (LPCSTR)sError);
}
// Dispose memory
workbook->Dispose();
}
else{
printf("Object is not available!");
}
Click here to see Continuous Code ListingC++.NET// Create an instance of the class that exports XLSX file, having two sheets
ExcelDocument ^workbook = gcnew 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
ExcelWorksheet ^xlsFirstTab =
safe_cast<ExcelWorksheet^>(workbook->easy_getSheetAt(0));
ExcelTable ^xlsFirstTable = xlsFirstTab->easy_getExcelTable();
// Add data in cells for report headerfor (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 valuesfor (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 XLSX file
workbook->easy_WriteXLSXFile("C:\\Samples\\Export to Excel.xlsx");
Click here to see Continuous Code Listing
// Create an instance of the class that exports XLSX file, 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 the first worksheet that will keep the data
ExcelTable xlsFirstTable =
((ExcelWorksheet)workbook.easy_getSheetAt(0)).easy_getExcelTable();
// Add data to headerfor (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 to cellsfor (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 XLSX file
workbook.easy_WriteXLSXFile("C:\\Samples\\Export to Excel.xlsx");
// Confirm generationif (workbook.easy_getError().equals(""))
System.out.println("File successfully created.");
else
System.out.println("Error encountered: " + workbook.easy_getError());
.NET:// Create an instance of the class that exports XLSX file
$workbook = new COM("EasyXLS.ExcelDocument");
// Create the worksheets
$workbook->easy_addWorksheet_2("First tab");
$workbook->easy_addWorksheet_2("Second tab");
// Get the table of the first worksheet that will keep the data
$xlsFirstTable = $workbook->easy_getSheetAt(0)->easy_getExcelTable();
// Add data to headerfor ($column=0; $column<5; $column++)
{
$xlsFirstTable->easy_getCell(0,$column)->setValue("Column " . ($column + 1));
$xlsFirstTable->easy_getCell(0,$column)->setDataType($DATATYPE_STRING);
}
// Add data to cellsfor ($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 XLSX file
$workbook->easy_WriteXLSXFile("C:\\Samples\\Export to Excel.xlsx");
// Confirm generationif ($workbook->easy_getError() == "")
echo"File successfully created.";
elseecho"Error encountered: " . $workbook->easy_getError();
Click here to see Continuous Code ListingJava:// Create an instance of the class that exports Excel files
$workbook = new java("EasyXLS.ExcelDocument");
// Create two sheets
$workbook->easy_addWorksheet("First tab");
$workbook->easy_addWorksheet("Second tab");
// Get the table of data for the first worksheet
$xlsFirstTable = $workbook->easy_getSheetAt(0)->easy_getExcelTable();
// Add data in cells for report headerfor ($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 valuesfor ($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 the XLSX file
$workbook->easy_WriteXLSXFile("C:\Samples\Tutorial04 - export data to Excel.xlsx");
// Confirm export of Excel fileif ($workbook->easy_getError() == "")
echo"File successfully created.";
elseecho"Error encountered: " . $workbook->easy_getError();
Click here to see Continuous Code Listing
' Create an instance of the class that exports XLSX fileset workbook = Server.CreateObject("EasyXLS.ExcelDocument")
' Create the worksheets
workbook.easy_addWorksheet_2("First tab")
workbook.easy_addWorksheet_2("Second tab")
' Get the table of the first worksheet that will keep the dataSet xlsFirstTable = workbook.easy_getSheetAt(0).easy_getExcelTable()
' Add data to headerfor column = 0 to 4
xlsFirstTable.easy_getCell(0,column).setValue("Column " & (column + 1))
xlsFirstTable.easy_getCell(0,column).setDataType(DATATYPE_STRING)
next' Add data to cellsfor 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)
nextnext' Export XLSX file
workbook.easy_WriteXLSXFile("C:\\Samples\\Export to Excel.xlsx");
' Confirm generationif workbook.easy_getError() = ""then
response.write("File successfully created.")
else
response.write("Error encountered: " + workbook.easy_getError())
end if
' Create an instance of the class that exports XLSX fileSet workbook = CreateObject("EasyXLS.ExcelDocument")
' Create the worksheets
workbook.easy_addWorksheet_2("First tab")
workbook.easy_addWorksheet_2("Second tab")
' Get the table of the first worksheet that will keep the dataSet xlsFirstTable = workbook.easy_getSheetAt(0).easy_getExcelTable()
' Add data to headerFor Column = 0 To 4
xlsFirstTable.easy_getCell(0,column).setValue("Column " & (Column + 1))
xlsFirstTable.easy_getCell(0,column).setDataType(DATATYPE_STRING)
Next' Add data to cellsFor 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)
NextNext' Export XLSX file
workbook.easy_WriteXLSXFile "C:\\Samples\\Export to Excel.xlsx"' Confirm generationdim sError
sError = workbook.easy_getError()
if sError = ""then
WScript.StdOut.Write(vbcrlf & _
"File successfully created. Press Enter to exit...")
else
WScript.StdOut.Write(vbcrlf & "Error: " & sError)
end if
<!-- Create an instance of the class that exports XLSX file --><cfobject type="java"class="EasyXLS.ExcelDocument"name="workbook"action="CREATE"><!-- Create the worksheets --><cfset ret = workbook.easy_addWorksheet("First tab")><cfset ret = workbook.easy_addWorksheet("Second tab")><!-- Get the table of the first worksheet that will keep the data --><cfset xlsFirstTable = workbook.easy_getSheetAt(0).easy_getExcelTable()><!-- Add data to header --><cfloop from="0"to="4"index="column"><cfset xlsFirstTable.easy_getCell(0,evaluate(column)).setValue(
"Column " & evaluate(column + 1))><cfset xlsFirstTable.easy_getCell(0,evaluate(column)).setDataType(
DataType.STRING)></cfloop><!-- Add data to cells --><cfloop from="0"to="99"index="row"><cfloop from="0"to="4"index="column"><cfset xlsFirstTable.easy_getCell(
evaluate(row + 1),evaluate(column)).setValue(
"Data " & evaluate(row + 1) & ", " & evaluate(column + 1))><cfset xlsFirstTable.easy_getCell(
evaluate(row + 1),evaluate(column)).setDataType(
DataType.STRING)></cfloop>
</cfloop><!-- Export XLSX file --><cfset ret = workbook.easy_WriteXLSXFile("C:\\Samples\\Export to Excel.xlsx")><!-- Confirm generation --><cfset sError = workbook.easy_getError()><cfif (sError IS "")><cfoutput>
File successfully created.
</cfoutput>
<cfelse>
<cfoutput>
Error encountered: #sError#
</cfoutput>
</cfif>
.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 headerfor 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 valuesfor 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\\Tutorial04 - 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 headerfor 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 valuesfor 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\\Tutorial04 - export data to Excel.xlsx")
Click here to see Continuous Code Listing
The screen shot below represents the exported data to XLSX 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.
Formatting cells
EasyXLS™ enables you to format cells, rows and columns in order to set the fonts and colors.