User GuideFAQHow to export to Excel file in C# and VB.NET
How to export to Excel file in C# and VB.NET
EasyXLS™ library allows you to export data to an Excel file. The data in cells can be formatted using predefined formats or user-defined formats.
EasyXLS can be successfully used inclusively to export large Excel files having big volume of data with fast exporting time.
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
EasyXLS permits you to export Excel files without Excel installed, without Interop or any other additional software installed.
Step 1: Download and install EasyXLS Excel Library for .NET
To download the trial version of EasyXLS Excel Library, press the below button:
If you already own a license key, you may login and download EasyXLS from your account.
Step 2: Create a C# or VB.NET project
If don't have a project, create one as ASP.NET web application, windows forms app, console application, class library or service.
Step 3: Include EasyXLS library into project
EasyXLS.dll must be added as reference to your project. EasyXLS.dll can be found after installing EasyXLS, in "Dot NET version" folder.
Step 4: Run C# or VB.NET code that exports to Excel file
The below example is a common code about how to export to Excel file in C# and VB.NET. After that, best techniques about exporting data from various standard data structures like DataTable, GridView, DataGridView, DataSet, DataGrid and List are shown.
// Create an instance of the class that exports the Excel file, having one sheet
ExcelDocument workbook = new ExcelDocument(1);
// Set the sheet name
workbook.easy_getSheetAt(0).setSheetName("Exported data");
// Get the table of the worksheet that will keep the data
ExcelTable xlsTable =
((ExcelWorksheet)workbook.easy_getSheetAt(0)).easy_getExcelTable();
// Add data to cellsfor (int row=0; row<100; row++){
for (int column=0; column<5; column++){
xlsTable.easy_getCell(row,column).setValue(
"Data " + (row + 1) + ", " + (column + 1));
}
}
// Export Excel file
workbook.easy_WriteXLSXFile("C:\\Samples\\Export to Excel.xlsx");
// Dispose memory
workbook.Dispose();
' Create an instance of the class that exports the Excel file, having one sheetDim workbook As ExcelDocument = New ExcelDocument(1)
' Set the sheet name
workbook.easy_getSheetAt(0).setSheetName("Exported data")
' Get the table of the worksheet that will keep the dataDim xlsWorksheet As ExcelWorksheet = workbook.easy_getSheetAt(0)
Dim xlsTable As ExcelTable = xlsWorksheet.easy_getExcelTable()
' Add data to cellsFor row As Integer = 0 To 99
For column As Integer = 0 To 4
xlsTable.easy_getCell(row, column).setValue( _
"Data " & (row + 1) & ", " & (column + 1))
NextNext' Export Excel file
workbook.easy_WriteXLSXFile("C:\Samples\Export to Excel.xlsx")
' Dispose memory
workbook.Dispose()
1. Export DataTable to Excel in C# and VB.NET
EasyXLS allows exporting DataTable to Excel with formatting. The DataTable can be the result of a SQL database query, the data source of a GridView, DataGridView, DataGrid or any other data structure.
EasyXLS allows exporting DataGridView to Excel with formatting from windows applications and windows forms. The exported data can be with headers or without. The exported Excel file can have multiple sheets.
EasyXLS allows exporting DataSet to Excel with formatting from windows applications, ASP.NET pages or console applications. The exported Excel file can have multiple sheets.
EasyXLS allows exporting a list of data to Excel with formatting. It can be a ListView, ArrayList or any other list that contains values that must be exported to Excel.
EasyXLS allows exporting DataGrid to Excel with formatting from windows applications and windows forms. The exported data can be with headers or without. The exported Excel file can have multiple sheets.
EasyXLS allows exporting data to XLSX file with formatting from windows applications or ASP.NET pages. Similarly, as shown in the above code sample, you can export data to XLSX file using ExcelDocument.easy_WriteXLSXFile method.
EasyXLS allows exporting data to XLSB file with formatting from windows applications or ASP.NET pages. Similarly, as shown in the above code sample, you can export data to XLSB file using ExcelDocument.easy_WriteXLSBFile method.
EasyXLS allows exporting data to XLSM file with formatting from windows applications or ASP.NET pages. The first step is to create an XLSM or XLTM file in Microsoft Excel that has the macro code defined. The Excel file can be empty or already filled with sheets and some data like report header. This will be the Excel template file that will be loaded using ExcelDocument.easy_LoadXLSXFile method. Now, the data can be added to the Excel file similarly, as shown in the above code sample. When the Excel file is completely filled, you can export data to XLSM file using ExcelDocument.easy_WriteXLSXFile method, but the exported Excel file must have XLSM file extension.
EasyXLS allows exporting data to XLS file with formatting from windows applications or ASP.NET pages. Similarly, as shown in the above code sample, you can export data to XLS file using ExcelDocument.easy_WriteXLSFile method.