Mahajan344 Mahajan344 - 1 year ago 278
jQuery Question

Export to excel using jquery ajax call

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) {
if ( == 'btnCustomeExport') {
// $.post('@Url.Action("ExportToExcel", "Report")');

url: "@Url.Action("ExportToExcel", "Report")",
type: "POST",
success: function (data, textStatus, jqXHR) {
//data: data from server
error: function (jqXHR, textStatus, errorThrown) {

and controller code :

public ActionResult ExportToExcel()
GridView GridView1 = new GridView();
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);

//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");


return Json(new { successCode = "1" });
catch (Exception e)
return Json(new { successCode = "0" });

If i debug the code .. i do get result in
but still not able see save/download option ??

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download