Articles in this section
Category / Section

How to create Excel file in ASP.NET MVC?

6 mins read

Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files. Using this library, you can create Excel file in ASP.NET MVC.   

Steps to create Excel file in ASP.NET MVC, programmatically:

Step 1: Create a new ASP.NET web application project.

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.

Complete the New ASP.NET Web Application

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 the Syncfusion.XlsIO.AspNet.Mvc5

Install NuGet package

Step 4: A default controller with name HomeController.cs gets added on creation of 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 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 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 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:

Create Excel File

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

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer the link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.


Conclusion

I hope you enjoyed learning about how to create Excel file in ASP.NET MVC.

You can refer to our ASP.NET MVC XIsIO feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET MVC XIsIO example to understand how to create and manipulate data.

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 forumsDirect-Trac, or feedback portal. We are always happy to assist you!


Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment