Export excel file Asp.net MVC and download it

Scenario

Some time we required to export some data to excel or any other format from our web page.

Implementation

Create a new MVC project in visual studio. Add a new class in Model folder name it product.cs

Product.cs Code

 public class Product
    {
        public int ProductID { get; set; }
        public string ProductName { get; set; }
        public decimal ProductPrice { get; set; }
        public string ProductComments { get; set; }
        public DateTime PublishDate { get; set; }
    }

Now add a new controller name it product controller.Add reference in controller.

 using 'yourprojectname'.Model; 
 using System.Web.UI.WebControls;
 using System.IO;
using System.Web.UI;

Controller Code

Create a view on which we place our Export to excel button or link

        public ActionResult ViewForExcel()
        {
            return View();
        }

ViewForExcel() View Code

<a href="#" onclick="ExportToExcel()">Export to excel</a>
<scripta>
    function ExportToExcel() {
            $.ajax({
                url: '@Url.Action("GenrateEXcel", "Product")',
                type: 'GET',
                async: true,
                success: function (Result) {
                    window.location = "/Product/GenrateEXcel";
                }
            });
        

    }
</scripta>

I make an ajax request to call GenrateEXcel function.

C# Code To generate Excel file

        [HttpGet]
        public ActionResult GenrateEXcel()
        {
            List ProductList = new List();
            ProductList.Add(new Product{ProductID=1,ProductComments="Baby product",ProductName="baby cart",ProductPrice=10.0m,PublishDate=DateTime.Now.AddHours(2)});
            ProductList.Add(new Product { ProductID = 2, ProductComments = "Women product", ProductName = "women Product", ProductPrice = 20.0m, PublishDate = DateTime.Now.AddHours(4) });
            ProductList.Add(new Product { ProductID = 3, ProductComments = "Men product", ProductName = "Men Product", ProductPrice = 30.0m, PublishDate = DateTime.Now.AddHours(-2) });
            ProductList.Add(new Product { ProductID = 4, ProductComments = "Electric product", ProductName = "Electric Product", ProductPrice = 10.0m, PublishDate = DateTime.Now.AddHours(4) });
            ProductList.Add(new Product { ProductID = 5, ProductComments = "Playing product", ProductName = "Playing Product", ProductPrice = 80.0m, PublishDate = DateTime.Now.AddHours(-4) });
            ProductList.Add(new Product { ProductID = 6, ProductComments = "Computer product", ProductName = "Computer Product", ProductPrice = 40.0m, PublishDate = DateTime.Now.AddHours(+2) });
            ProductList.Add(new Product { ProductID = 7, ProductComments = "Water product", ProductName = "Water Product", ProductPrice = 50.0m, PublishDate = DateTime.Now.AddHours(7) });
            
            GridView gv = new GridView();
            gv.DataSource = ProductList;
            gv.DataBind();
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment; filename=Product.xls");
            Response.ContentType = "application/ms-excel";
            Response.Charset = "";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            gv.RenderControl(htw);
            Response.Output.Write(sw.ToString());
            Response.End();
            return View();
        }

Now browse to /product/ViewForExcel and click on Export to excel link. Downloading file prompt will come. download the file and open it in excel