I have tried this Export DataTable to Excel File I am able to download excel file when I am calling through SOAP API but when I am calling it from ajax I am not getting any error and I am not able to download the Excel File.
Ajax/JavaScript Code:
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) {
}
});
}
Method:
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();