如何使用ajax调用将Datatable导出到Excel文件

时间:2017-03-29 07:37:19

标签: c# jquery asp.net ajax excel

我试过这个Export DataTable to Excel File当我通过SOAP API调用时,我能够下载excel文件,但是当我从ajax调用它时,我没有收到任何错误,我无法下载Excel文件

Ajax / JavaScript代码:

function getWorkOrders(wono) {
        $.ajax({
            dataType: "json",
            type: "POST",
            url: 'AdminWeb.asmx/GetWorkOrdersDetails',
            data: 'id=0&wono=' + wono,
            async: true,
            success: function (data) {
    },
            error: function (jqXHR, textStatus, errorThrown) {
            }
    });
    }

方法:

DataTable dtResult = DataAccess.GetDataTableFromStoredProcedure("sp_Get_WO", Parameters);

    //dt = city.GetAllCity();//your datatable
    string attachment = "attachment; filename=city.xls";
    HttpContext.Current.Response.ClearContent()
    HttpContext.Current.Response.AddHeader("content-disposition", attachment);
    HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
    string tab = "";
    foreach (DataColumn dc in dtResult.Columns)
    {
       HttpContext.Current.Response.Write(tab + dc.ColumnName);
        tab = "\t";
    }
    HttpContext.Current.Response.Write("\n");
    int i;
    foreach (DataRow dr in dtResult.Rows)
    {
        tab = "";
        for (i = 0; i < dtResult.Columns.Count; i++)
        {
            HttpContext.Current.Response.Write(tab + dr[i].ToString());
            tab = "\t";
        }
        HttpContext.Current.Response.Write("\n");
    }
    HttpContext.Current.Response.End();

0 个答案:

没有答案