title: How to export data to Excel using ASP.NET MVC 5 without reloading the page?
body: <p>The process of exporting the data into Excel is working. But I don't need to reload the page. When I try to press the Export Data to Excel the page is reloading. How to avoid this? Please, help. Thank you.</p>
  
  <p>C#:</p>
  
  <pre><code>public ActionResult Export(int id) {
      Excel.Application application = new Excel.Application();
      Excel.Workbook workbook = application.Workbooks.Add(System.Reflection.Missing.Value);
      Excel.Worksheet worksheet = workbook.ActiveSheet;
      var export = _context.Employees.Where(x =&gt; x.Id == id)ToList();
  
      worksheet.Cells[1, 1] = "Name";
      worksheet.Cells[1, 2] = "Age";
      worksheet.Cells[1, 3] = "Position";
      worksheet.Cells[1, 4] = "Address";
      worksheet.Cells[1, 5] = "Contact";
  
      foreach (var e in export)
      {
          worksheet.Cells[row, 1] = e.Name;
          worksheet.Cells[row, 2] = e.Age;
          worksheet.Cells[row, 3] = e.Position;
          worksheet.Cells[row, 4] = e.Address;
          worksheet.Cells[row, 5] = e.Contact;
  
          row++;
      }
  
      workbook.SaveAs(@"C:\Excel\sample.xls");
      workbook.Close();
      Marshal.ReleaseComObject(workbook);
  
      application.Quit();
      Marshal.FinalReleaseComObject(application);
  
      return View();
  }
  </code></pre>
  
  <p>Ajax:</p>
  
  <pre><code>&lt;script&gt;
      $(document).ready(function() {
          $(".js-export").click(function(e) {
              var link = $(e.target);
               $.ajax({
                      url: "/home/export/" + link.attr("data-export-id"),
                      method: "GET"
                      })
          });
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>Event:</p>
  
  <pre><code>&lt;a href="#" class="js-export btn btn-danger btn-xs" data-export-id="@item.Id"&gt;Export to Excel&lt;/a&gt;
  </code></pre>