How to create Excel file in ASP.NET Web Forms?
Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files.
This sample explains the creation of an Excel file with simple text, in ASP.NET Web Forms platform.
Steps to create an Excel file in ASP.NET Web Forms:
Step 1: Create a new C# ASP.NET Web Application project.
Create a new C# ASP.NET Web application project
Step 2: Select the Web Forms template.
Select the project template as Web Forms
Step 3: Install the Syncfusion.XlsIO.AspNet NuGet package as reference to your .NET Framework application from NuGet.org.
Install NuGet package to the project
Step 3: Add a new Web Form in the web application project.
- To add the new Web Form, right click on the project and select Add->New Item.
- Select the Web Form and name it as MainPage. Step 4: Include the following code snippet in MainPage.aspx to add a new button.
HTML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MainPage.aspx.cs" Inherits="CreateExcel.MainPage" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Create Excel" OnClick="OnButtonClicked" /> </div> </form> </body> </html>
Step 5: Include the following namespaces in MainPage.aspx.cs file.
C#
using Syncfusion.XlsIO; using System;
VB.NET
Imports Syncfusion.XlsIO Imports System
Step 6: Have a click event for the button in MainPage.aspx.cs file.
C#
protected void OnButtonClicked(object sender, EventArgs args) { }
VB.NET
Protected Sub OnButtonClicked(ByVal sender As Object, ByVal args As EventArgs) End Sub
Step 7: Include the following code snippet in click event of the button, to create an Excel file.
C#
//Create an instance of ExcelEngine using (ExcelEngine excelEngine = new ExcelEngine()) { //Instantiate the Excel application object IApplication application = excelEngine.Excel; //Set the default application version as Excel 2016 application.DefaultVersion = ExcelVersion.Excel2016; //Create a workbook with a worksheet IWorkbook workbook = application.Workbooks.Create(1); //Access first worksheet from the workbook instance IWorksheet worksheet = workbook.Worksheets[0]; //Insert sample text into cell “A1” worksheet.Range["A1"].Text = "Hello World"; //Save the workbook to disk in xlsx format workbook.SaveAs("Output.xlsx", Response, ExcelDownloadType.Open, ExcelHttpContentType.Excel2016); }
VB.NET
'Create an instance of ExcelEngine Using excelEngine As ExcelEngine = New ExcelEngine() 'Instantiate the Excel application object Dim application As IApplication = excelEngine.Excel 'Set the default application version as Excel 2016 application.DefaultVersion = ExcelVersion.Excel2016 'Create a new Excel workbook Dim workbook As IWorkbook = application.Workbooks.Create(1) 'Get the first worksheet in the workbook into IWorksheet Dim worksheet As IWorksheet = workbook.Worksheets(0) 'Insert sample text into cell A1 worksheet.Range("A1").Text = "Hello World" 'Save the workbook to disk in xlsx format workbook.SaveAs("Output.xlsx", Response, ExcelDownloadType.Open, ExcelHttpContentType.Excel2016) End Using
A complete working sample to create an Excel file in ASP.NET Web Forms application using Syncfusion Excel (XlsIO) library can be downloaded from CreateExcel.zip.
By executing the program, you will get the output Excel document as follows.
Output Excel document
Take a moment to peruse the documentation, where you can find basic worksheet data manipulation options along with features like Conditional Formatting, worksheet calculations through Formulas, adding Charts in worksheet or workbook, organizing and analyzing data through Tables and Pivot Tables, appending multiple records to worksheet using Template Markers, and most importantly PDF and Image conversions etc. with code examples. See Also: Create Excel file in Windows Forms Create Excel file in ASP.NET MVC Create Excel file in ASP.NET Core Click here to explore the rich set of Syncfusion Excel (XlsIO) library features.
|
Note:
A new version of Essential Studio for ASP.NET is available. Versions prior to the release of Essential Studio 2014, Volume 2 will now be referred to as a classic versions.The new ASP.NET suite is powered by Essential Studio for JavaScript providing client-side rendering of HTML 5-JavaScript controls, offering better performance, and better support for touch interactivity. The new version includes all the features of the old version, so migration is easy.
The Classic controls can be used in existing projects; however, if you are starting a new project, we recommend using the latest version of Essential Studio for ASP.NET. Although Syncfusion will continue to support all Classic Versions, we are happy to assist you in migrating to the newest edition.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls. If you have any queries or require clarifications, please let us know in the comments section below.
You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!