0

Hello here my code of my controller action:

public ActionResult ExportExcel()
{
    DataTable dt = new DataTable();

    FILL DataTable

    try
    {
    FileExcel(dt);
    return Json(new { successCode = "1" });
    }
    catch (Exception ex)
    {
        return Json(new { successCode = "0" });
    }
}

public void FileExcel(DataTable dt)
{
    GridView gv = new GridView();
    gv.DataSource = dt;
    gv.DataBind();

    HttpContext context = System.Web.HttpContext.Current;
    context.Response.ClearContent();
    context.Response.Buffer = true;
    context.Response.AddHeader("content-disposition", "attachment; filename=Ricerca.xls");
    context.Response.ContentType = "application/ms-excel";
    context.Response.Charset = "";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    gv.RenderControl(htw);
    context.Response.Output.Write(sw.ToString());
    context.Response.Flush();
    htw.Close();
    sw.Close();
    context.Response.End();
}

My Ajax Jquery function

function ExportExcel() {
    $.ajax({
        type: "POST",
        url: '/Home/ExportExcel/',    
        beforeSend: function () {
            AjaxStart('...Export');
        },
        success: function (data, textStatus, jqXHR) {
                 AjaxStop();
                 alert('success')
        },
        error: function (failure) {
            alert('error');               
        }
    });
}

if i call ajax function ExportExcel() through a simple link as

<a href="#" onclick="ExportExcel();">Export Excel Ajax</a>

it doesn't works !!!!!!

while if use a simple controller action as

@Html.ActionLink("Export Excel", "ExportExcel", "Home")

the excel file to save appears! i would like use first method (ajax) to manage the esporting delay

3 Answers 3

1

ajax does not work this way . You have the excel file's data available in the data variable in the success function but you won't be able to save it as a file . try adding this line to the success function and see for you self :

alert(data); 

you might be able to achieve what you want through an iframe with 0 width and height.

Sign up to request clarification or add additional context in comments.

Comments

0

If you are looking to trigger the download via JavaScript, try the answer from this question How to export to excel in Asp.Net MVC?.

function ExportExcel() {
    window.open('/Home/ExportExcel/');
}

Comments

0

We can get the Tabletools flash swf file through cdn provides the ability to save pdf files. We will be using Population table. please go through this website

http://www.infinetsoft.com/Post/How-to-export-jQuery-datatables-to-MS-excel-in-asp-net-MVC/1192#.VzCATYR97IU

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.