What I understant you I guess this will help you:
[AcceptVerbs(HttpVerbs.Get)]
public FileResult ImportOrders()
{
DataTable dt = new DataTable();
dt = "get data here";
MemoryStream MyMemoryStream = null;
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(dt, "YourReport");
MyMemoryStream = new MemoryStream();
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(Response.OutputStream);
return File(MyMemoryStream, "application/vnd.ms-excel", "YourReport.xlsx");
}
}
So, here you don't need to convert JSON to excel.
Call this action directrly. It will directrly download output to excel.ectrly download output to excel.
else you can try this one, but this gonna convert JSON to CSV
function JSONToCSVConvertor(JSONData, ReportTitle, ShowLabel) {
var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;
var CSV = '';
if (ShowLabel) {
var row = "";
for (var index in arrData[0]) {
row += '<b>'+index + '</b>,';
}
row = row.slice(0, -1);
CSV += row + '\r\n';
}
for (var i = 0; i < arrData.length; i++) {
var row = "";
for (var index in arrData[i]) {
row += '"' + arrData[i][index] + '",';
}
row.slice(0, row.length - 1);
CSV += row + '\r\n';
}
if (CSV == '') {
alert("Invalid data");
return;
}
var fileName = '';
fileName += ReportTitle.replace(/ /g, "_");
var uri = 'data:text/csv;charset=utf-8,' + escape(CSV);
var link = document.createElement("a");
link.href = uri;
link.style = "visibility:hidden";
link.download = fileName + ".csv";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};
(Y)