Export to excel using jquery ajax call
asp.net-mvc-4, devexpress, export-to-excel, jquery
Solution
Since it doesn't appear you are sending any data instead of ajax try:
window.location= "@Url.Action("ExportToExcel", "Report")";
Or just use the url in `<a>` tag `href`
Problem
I am working on MVC4 project.I have Devexpress report toolbar where i have a custom button for export to excel as there inbuilt functionaly have cell merging issue. Anyways on click on that custom button .. i want to run export to excel code.. but its working working.. i mean its returning correct html but not asking for prompt to save file/download file,may be because of ajax call... Here is code for ajax call ``` function ReportToolbar_ItemClick(s, e) { debugger; if (e.item.name == 'btnCustomeExport') { // $.post('@Url.Action("ExportToExcel", "Report")'); $.ajax({ url: "@Url.Action("ExportToExcel", "Report")", type: "POST", success: function (data, textStatus, jqXHR) { //data: data from server alert('success'); }, error: function (jqXHR, textStatus, errorThrown) { alert('error'); } }); } } ``` and controller code : ``` public ActionResult ExportToExcel() { try { GridView GridView1 = new GridView(); Response.ClearContent(); Response.Buffer = true; Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "EmployeesData.xls")); Response.ContentType = "application/ms-excel"; StringWriter stringWriter = new StringWriter(); HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter); GridView1.AllowPaging = false; GridView1.DataSource = ReportExecutor.GetShopReportExportData(DateTime.Now, DateTime.Now); GridView1.DataBind(); //This will change the header background color GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF"); //This will apply style to gridview header cells for (int index = 0; index < GridView1.HeaderRow.Cells.Count; index++) { GridView1.HeaderRow.Cells[index].Style.Add("background-color", "#d17250"); } int index2 = 1; //This will apply style to alternate rows foreach (GridViewRow gridViewRow in GridView1.Rows) { gridViewRow.BackColor = Color.White; if (index2 <= GridView1.Rows.Count) { if (index2 % 2 != 0) { for (int index3 = 0; index3 < gridViewRow.Cells.Count; index3++) { gridViewRow.Cells[index3].Style.Add("background-color", "#eed0bb"); } } } index2++; } GridView1.RenderControl(htmlTextWriter); Response.Write(stringWriter.ToString()); Response.End(); return Json(new { successCode = "1" }); } catch (Exception e) { return Json(new { successCode = "0" }); } } ``` If i debug the code .. i do get result in `stringWriter` but still not able see save/download option ??