Create Excel file in .NET, Java and other programming languages
EasyXLS™ library allows you to create Excel documents. The data in cells can be formatted using predefined formats or user-defined formats.
EasyXLS can be successfully used inclusively to create large Excel files having big volume of data with fast exporting time.
The first step is to create an instance of the ExcelDocument class, which is the main class for pure code-based Excel document handling. This class is the key used to create new Excel files and read Excel files.
EasyXLS permits you to create Excel files without Excel installed, without Interop or any other additional software installed.
It also provides a method, easy_getError, for debugging purposes, which renders information on errors or warnings that occur during writing or reading Excel files.
EasyXLS library 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
The below source code sample shows how to create Excel file programmatically. After that, more code samples about how to create and save Excel file from various standard data structures like DataTable, DataSet, ResultSet and List are shown.
// Create an instance of the class that creates 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");
// Create Excel file
workbook.easy_WriteXLSXFile("C:\\Samples\\Excel file.xlsx");
// Confirm the creation of the Excel file
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...");
// Dispose memory
workbook.Dispose();
' Create an instance of the class that creates Excel files, 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")
' Create Excel file
workbook.easy_WriteXLSXFile("C:\Samples\Excel file.xlsx")
' Confirm the creation of the Excel fileDim sError As String = workbook.easy_getError()
If (sError.Equals("")) Then
Console.Write(vbCrLf & "File successfully created. Press Enter to Exit...")
Else
Console.Write(vbCrLf & "Error encountered: " & sError & vbCrLf & _
"Press Enter to Exit...")
End If' Dispose memory
workbook.Dispose()
C++// Create a pointer to the interface that creates Excel files
EasyXLS::IExcelDocumentPtr workbook;
hr = CoCreateInstance(__uuidof(EasyXLS::ExcelDocument),
NULL,
CLSCTX_ALL,
__uuidof(EasyXLS::IExcelDocument),
(void**) &workbook);
if(SUCCEEDED(hr)){
// Create two sheets
workbook->easy_addWorksheet_2("First tab");
workbook->easy_addWorksheet_2("Second tab");
// Create Excel file
workbook->easy_WriteXLSXFile("C:\\Samples\\Excel file.xlsx");
// Confirm the creation of the Excel file
_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 creates Excel files, having multiple sheets
ExcelDocument ^workbook = gcnew ExcelDocument(2);
// Set the sheet names
workbook->easy_getSheetAt(0)->setSheetName("First tab");
workbook->easy_getSheetAt(1)->setSheetName("Second tab");
// Create Excel file
workbook->easy_WriteXLSXFile("C:\\Samples\\Excel file.xlsx");
// Confirm the creation of the Excel file
String ^sError = workbook->easy_getError();
if (sError->Equals(""))
Console::Write("\nFile successfully created. Press Enter to Exit...");
else
Console::Write(String::Concat("\nError encountered: ", sError,
"\nPress Enter to Exit..."));
// Dispose memory
delete workbook;
Click here to see Continuous Code Listing
// Create an instance of the class that creates 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");
// Create Excel file
workbook.easy_WriteXLSXFile("C:\\Samples\\Excel file.xlsx");
// Confirm the creation of the Excel fileif (workbook.easy_getError() == "")
System.out.println("File successfully created.");
else
System.out.println("Error encountered: " + workbook.easy_getError());
// Dispose memory
workbook.Dispose();
.NET:// Create an instance of the class that creates Excel files
$workbook = new COM("EasyXLS.ExcelDocument");
// Create two sheets
$workbook->easy_addWorksheet_2("First tab");
$workbook->easy_addWorksheet_2("Second tab");
// Create Excel file
$workbook->easy_WriteXLSXFile("C:\Samples\Excel file.xlsx");
// Confirm the creation of the Excel fileif ($workbook->easy_getError() == "")
echo"File successfully created.";
else echo"Error encountered: " . $workbook->easy_getError();
// Dispose memory
$workbook->Dispose();
Click here to see Continuous Code ListingJava:// Create an instance of the class that creates Excel files
$workbook = new java("EasyXLS.ExcelDocument");
// Create two sheets
$workbook->easy_addWorksheet("First tab");
$workbook->easy_addWorksheet("Second tab");
// Create Excel file
$workbook->easy_WriteXLSXFile("C:\Samples\Excel file.xlsx");
// Confirm the creation of Excel fileif ($workbook->easy_getError() == "")
echo"File successfully created.";
elseecho"Error encountered: " . $workbook->easy_getError();
// Dispose memory
$workbook->Dispose();
Click here to see Continuous Code Listing
' Create an instance of the class that creates Excel filesset workbook = Server.CreateObject("EasyXLS.ExcelDocument")
' Create two sheets
workbook.easy_addWorksheet_2("First tab")
workbook.easy_addWorksheet_2("Second tab")
' Create Excel file
workbook.easy_WriteXLSXFile ("C:\Samples\Excel file.xlsx")
' Confirm the creation of the Excel fileif workbook.easy_getError() = ""then
response.write("File successfully created.")
else
response.write("Error encountered: " + workbook.easy_getError())
end if' Dispose memory
workbook.Dispose
<!-- Create an instance of the class that creates Excel files --><cfobject type="java"class="EasyXLS.ExcelDocument"name="workbook"action="CREATE">
<!-- Create two sheets --><cfset ret = workbook.easy_addWorksheet("First tab")><cfset ret = workbook.easy_addWorksheet("Second tab")><!-- Create Excel file --><cfset ret = workbook.easy_WriteXLSXFile("C:\Samples\Excel file.xlsx")><!-- Confirm the creation of the Excel file --><cfset sError = workbook.easy_getError()><cfif (sError IS "")><cfoutput>
File successfully created.
</cfoutput><cfelse><cfoutput>
Error encountered: #sError#
</cfoutput></cfif><!-- Dispose memory --><cfset workbook.Dispose()>
.NET:# Create an instance of the class that creates 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")
# Create the Excel file
workbook.easy_WriteXLSXFile("C:\\Samples\\Excel file.xlsx")
# Confirm the creation of Excel file
sError = workbook.easy_getError()
if sError == "":
print("\nFile successfully created.\n\n")
else:
print("\nError encountered: " + sError + "\n\n")
# Dispose memory
gc.collect()
Click here to see Continuous Code ListingJava:# Create an instance of the class that creates 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")
# Create the Excel file
workbook.easy_WriteXLSXFile("C:\\Samples\\Excel file.xlsx")
# Confirm the creation of Excel file
sError = workbook.easy_getError()
if sError == "":
print("\nFile successfully created.\n")
else:
print("\nError encountered: " + sError + "\n\n")
# Dispose memory
gc.collect()
Click here to see Continuous Code Listing
The screen shot below represents the created Excel document generated by the code sample above. The file has two worksheets (First Tab and Second Tab).