I use MVC 4 / C#. (VS 2013) I don't know how to solve the problem with export to excel with ajax call. Does anyone has some working example on this ? Please, if you have working example. Here's the code which I couldn't make it work with ajax call:
var exportXLSDeadlinesParent = function () {
if ($('#hiddenParentCount').val() > 0) {
$.ajax({
url: '../../Administration/UserManagement/exportUserProp',
type: 'POST',
data: 'typeToExport=PARENT',
success: function (data) {
},
error: function (xhr, err) {
alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
alert("responseText: " + xhr.responseText);
}
});
}
};
And here is controller action:
[HttpPost]
public virtual void exportUserProp(string typeToExport, string validFrom = null)
{
var query = ModelPropSessions:
using (ExcelPackage pck = new ExcelPackage())
{
ExcelWorksheet ws = pck.Workbook.Worksheets.Add(string.Format("{0} - {1}", "historyData", typeToExport));
ws.Cells[1, 1].Value = "ID user";
ws.Cells[1, 2].Value = "Agent number";
ws.Cells[1, 3].Value = "Deadline type";
for (int z = 0; z < query.Count(); z++)
{
ws.Cells[z + 2, 1].Value = query[z].USERID;
ws.Cells[z + 2, 2].Value = query[z].USER_INT_NUM;
ws.Cells[z + 2, 3].Value = query[z].DESC;
}
string myFile = "FileToExport";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=" + myFile);
Response.BinaryWrite(pck.GetAsByteArray());
Response.End();
}
}