title: How to export data in a Excel Sheet on a button click using ASP.NET MVC
body: <p>I am trying to export data on a button click in ASP.NET MVC. I have written a <code>JSONResult ExportDataInExcel()</code> Class and here I have written code to export data which I am getting from <code>MyService</code>.</p>
  
  <p>I am able to get all the data but this data is not getting exported to excel sheet on button click. I want that when I click on my button then it should export the data into Excel Sheet. But currently my data is getting displayed on View and not getting exported to Excel Sheet. Could any one please help me understand what is that I am missing?</p>
  
  <p>Controller:</p>
  
  <pre><code>public JsonResult ExportDataInExcel(int A, int B, string C)
  {
      Response.Clear();
      Response.Buffer = true;
      Response.ContentType = "application/vnd.ms-excel";
      Response.AddHeader("Content-disposition", "attachment; filename=Test.xls");
      Response.Charset = "";
  
      MyService.ServiceInfoClient objMyService = new MyService.ServiceInfoClient();
  
      System.IO.StringWriter stringWriter = new System.IO.StringWriter();
      HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
  
      GridView ExcelGrid = new GridView();
      ExcelGrid.DataSource = from data in objMyService.ExportTestData(lId,wId,cid)
          select new
          {
              A=data.a,
              B=data.b,
              C=data.c,
              D=data.d,
              E=data.e,
              F=data.f,
              G=data.g
          };
      ExcelGrid.DataBind();
      ExcelGrid.HeaderStyle.Font.Bold = true;
      ExcelGrid.HeaderStyle.ForeColor = System.Drawing.Color.White; ;
      ExcelGrid.HeaderStyle.BackColor = System.Drawing.Color.Green;
      ExcelGrid.RenderControl(htmlTextWriter);
  
      Response.Output.Write(stringWriter.ToString());
      Response.Flush();
      Response.End();
  
      return Json(ExcelGrid);
  }
  </code></pre>
  
  <p>View</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      function ExportTestDet(){
          if ($("#ddl1").val() == '0') {
              alert('Please select');
              return false;
          }
  
          if ($("#ddl2").val() == '0') {
              alert('Please select');
              return false;
          }
  
          $.ajax({
              type: 'POST',
              url: '@Url.Action("ExportDataInExcel")',
              datatype: 'JSON',
              data: { LId: $("#ddl1").val(), WID: $("#ddl2").val() },
              success: function (data) {
                  $("#DynamicContent").html(data);
                  $("#tbl").show();
                  alert("Exported");
              },
              error: function (ex) {
                  alert('Failed to export data: ' + ex.responseText);
              }
          });
      }
  &lt;/script&gt;
  
  &lt;div&gt;
      &lt;input type="submit" value="Export Excel" onclick="return ExportTestDet()" /&gt;
  &lt;/div&gt;
  </code></pre>