1

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.

C#:

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 => 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();
}

Ajax:

<script>
    $(document).ready(function() {
        $(".js-export").click(function(e) {
            var link = $(e.target);
             $.ajax({
                    url: "/home/export/" + link.attr("data-export-id"),
                    method: "GET"
                    })
        });
    });
</script>

Event:

<a href="#" class="js-export btn btn-danger btn-xs" data-export-id="@item.Id">Export to Excel</a>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Paul George
  • 21
  • 3
  • 9

2 Answers2

0

Instead using ActionResult you can use JsonResult and can return only url of excel file :-

    $.ajax({
            type: 'GET',
            url: "/home/export/" + link.attr("data-export-id"),
            dataType: 'json',                
            success: function (data) {                  
               console.log(data.ExcelUrl);
            }
        });

Your code in controller :-

 public JsonResult Export(int id)
        {   
          //Exel generation code  here showing in your post. 
          return Json(new { ExcelUrl = "New Excel URL path" }, JsonRequestBehavior.AllowGet);
       }
Anand Systematix
  • 632
  • 5
  • 15
  • Hi Anand, yes good suggestion. What is New Excel URL path? I can't understand. – Paul George Feb 09 '17 at 05:56
  • Thanks, Its your excel file path like "C:\Excel\sample.xls". Please mark it as answer if it solve your issue, so other can use it. – Anand Systematix Feb 09 '17 at 05:57
  • //Get excel URL Here , you can get excel url here that you return from JsonResult method. As I get it like "data.ExcelUrl" – Anand Systematix Feb 09 '17 at 06:04
  • One more thing Anand. Please, can you edit the example above? Thank you. The ajax part. – Paul George Feb 09 '17 at 06:07
  • You should be sending file stream instead of file path unless the file path is shared one or in cloud blob storage. How will the client be able to access the C:\ drive or whatever drive of the server where you save the file? – Developer Feb 09 '17 at 06:16
  • @Developer you are talking about file stream. Can you provide a sample? – Paul George Feb 09 '17 at 06:55
0

Call the action via $.ajax and return FileResult in the action:

public FileResult Export(int id) 
 {
    //.........your file creation logic goes here ....//

    byte[] fileBytes = File.ReadAllBytes(filePath);
    return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
 }
Developer
  • 6,240
  • 3
  • 18
  • 24