How to create Excel file in ASP.NET MVC?
Syncfusion® Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. It also, converts Excel documents to PDF files. Using this library, you can create an Excel file in ASP.NET MVC.
Steps to create an Excel file in ASP.NET MVC, programmatically:
Step 1: Create a new ASP.NET web application project.
Create a new ASP.NET web application
Step 2: Complete the New ASP.NET Web Application – CreateXlsIOSample dialog:
- Select MVC.
- Click OK.
New ASP.NET Web Application dialog
Step 3: Install the Syncfusion.XlsIO.AspNet.Mvc5 NuGet package as reference to your .NET Framework application from NuGet.org.
Install NuGet package
Step 4: A default controller with the name HomeController.cs gets added upon the creation of the ASP.NET MVC project. Include the following namespace in that HomeController.cs file.
C#
using Syncfusion.XlsIO;
VB.NET
Imports Syncfusion.XlsIO
Step 5: A default action method named Index will be present in HomeController.cs. Right-click on this action method and select Go To View, where you will be directed to its associated view page Index.cshtml.
Step 6: Add a new button in the Index.cshtml as shown below.
CSHTML
@{Html.BeginForm("CreateDocument", "Home", FormMethod.Get); { <div> <input type="submit" value="Create Document" style="width:150px;height:27px" /> </div> } Html.EndForm(); }
VBHTML
@Html.BeginForm("CreateDocument", "Home", FormMethod.Get) <div> <input type="submit" value="Create Document" style="width:150px;height:27px" /> </div> Html.EndForm();
Step 7: Add a new action method CreateDocument in HomeController.cs and include the below code snippet to create an Excel file and download it.
C#
public void CreateDocument() { // Create an instance of ExcelEngine using (ExcelEngine excelEngine = new ExcelEngine()) { // Set the default application version as Excel 2016 excelEngine.Excel.DefaultVersion = ExcelVersion.Excel2016; // Create a workbook with a worksheet IWorkbook workbook = excelEngine.Excel.Workbooks.Create(1); // Access the 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("Sample.xlsx", ExcelSaveType.SaveAsXLS, HttpContext.ApplicationInstance.Response, ExcelDownloadType.Open); } }
VB.NET
Public Sub CreateDocument() 'Create an instance of ExcelEngine Using excelEngine As ExcelEngine = New ExcelEngine 'Set the default application version as Excel 2016 excelEngine.Excel.DefaultVersion = ExcelVersion.Excel2016 'Create a workbook with a worksheet Dim workbook As IWorkbook = excelEngine.Excel.Workbooks.Create(1) 'Access the first worksheet from the workbook instance 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("Sample.xlsx", ExcelSaveType.SaveAsXLS, HttpContext.ApplicationInstance.Response, ExcelDownloadType.Open) End Using End Sub
A complete working example of how to create an Excel file in ASP.NET MVC can be downloaded from Create Excel file in ASP.NET MVC.zip.
By executing the program, you will get the output Excel file as shown below:
Output Excel document
You can also open the existing Excel file and add more data using XlsIO. Please refer the online sample and UG documentation links for the same.
Refer here to explore the rich set of Syncfusion® Excel (XlsIO) library features.
See Also:
How to download Excel from Ajax call in ASP.NET MVC?
How to create an Excel file in ASP.NET Core?
How to create an Excel file in ASP.NET Web Forms?
How to create an Excel file in Xamarin?
How to create an Excel file in Windows Forms
How to create an Excel file in WPF
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.
I hope you enjoyed learning about how to create Excel file in ASP.NET MVC.
You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion® components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums, Support Tickets, or feedback portal. We are always happy to assist you!