Export to Excel in asp.net MVC

In a scenario comes on asp.net MVC application comes where user need to export in excel , csv, xml file by asp.net MVC application as tabular data.

Detail
So on export button form will be submitted and execute controller’s POST Method.

So in POST method load all data and export in excel with below code, Also if any filter applied then that filter parameter need pass as parameter in post method and execute data based on that filter to before export in excel.

public ActionResult ExportData()
{
            var employeeDetail = from e in DB.employees.AsEnumerable()
                         select new
                             {
                                e.firstname,
                                e.lastname,
                                e.emailid,
                                e.addresss
                             };

            System.Web.UI.WebControls.GridView gridvw = new System.Web.UI.WebControls.GridView();
            gridvw.DataSource = employeeDetail.ToList().Take(7); //bind the datatable to the gridview
            gridvw.DataBind();
            Response.ClearContent();
            Response.AddHeader("content-disposition", "attachment;filename=employeeDetail.xls");
            Response.ContentType = "application/excel";
            StringWriter swr = new StringWriter();
            HtmlTextWriter tw = new HtmlTextWriter(swr);
            gridvw.RenderControl(tw);
            Response.Write(swr.ToString());

            Response.End();
            return View("About", employeeDetail);
        }

Please post a comment if you have any more questions.

Thanks,
Amit Patel
“Enjoy Programming”

About these ads

About amitpatelit
Having more than 9 years of experience in IT industry. - Strong knowledge and experience on various technologies and tools like ASP.NET, MVC, Entity framework, Razor,VB.NET, C#, WCF,WPF, MVC, Silver light, ASP, XML ,Java Script, HTML, CSS, AJAX, COM/DCOM,MS SQL Server, Enterprise Library, MSMQ, SQL Server Reporting Service, Crystal Reports. - Achieved 7800 points (Star level) in the official Microsoft ASP.NET forum. - MCPD and MCTS in Web, Window and distributed application.

2 Responses to Export to Excel in asp.net MVC

    • José Guerrero says:

      Hi, how are you, can you help me please, I am progamming in MVC and I need to export the data to excel, I copied your code but it’s not working, ¿what I have wrong?, I am using ajax.

      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
      this is the code that I have in the view
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      function Export()
      {
      $.ajax({
      url: ‘/Employees/Export’,
      type: ‘GET’,
      error: function (data) {
      if(data != “”)
      alert(“Ocurrio el siguiente error:” + data + “\nComuníquese con el administrador
      del sistema.”);
      else
      alert(“Ocurrio un error.\nComuníquese con el administrador del sistema.”);
      }
      });
      }

      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
      this is the code in c#, if you debug the code, it runs, but don’t see the file exported.
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      public void Export()
      {
      List lst = LstEmployee.Take(2).ToList();

      System.Web.UI.WebControls.GridView gridvw = new System.Web.UI.WebControls.GridView();
      gridvw.DataSource = lst;
      gridvw.DataBind();
      Response.ClearContent();
      Response.AddHeader(“content-disposition”, “attachment;filename=employeeDetail.xls”);
      Response.ContentType = “application/excel”;
      System.IO.StringWriter swr = new System.IO.StringWriter();
      System.Web.UI.HtmlTextWriter tw = new System.Web.UI.HtmlTextWriter(swr);
      gridvw.RenderControl(tw);
      Response.Write(swr.ToString());

      Response.End();
      }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 238 other followers

%d bloggers like this: