We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Create Excel file in ASP.NET MVC


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.

 

2X faster development

The ultimate ASP.NET MVC UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
Roman
Nov 20, 2017

if I already have a file in excel on my server as I call it and add more data

Reply
Prasanth M [Syncfusion]
Nov 23, 2017

Hi Roman,

Yes, you can open the existing Excel file and add more data using XlsIO.

Please refer the online sample and UG documentation link for the same.

Online Sample Link: http://mvc.syncfusion.com/demos/web/xlsio/default

UG Documentation Link: https://help.syncfusion.com/file-formats/xlsio/loading-and-saving-workbook


Regards,

Prasanth

Mustafa Torun
Dec 18, 2017

my man, save my day.

thnx a lot sycfusion.

Reply
Mohan Chandran [Syncfusion]
Dec 19, 2017

Hi Mustafa,

Thank you for updating us.

Regards,

Mohan Chandran.

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile