我正在尝试从JSON对象数组在控制器上生成Excel文件。 我只传递了一个JSON对象,而我使用的相同方法似乎不适用于List或JSON数组。
AJAX生成对象:
$.ajax({
type: "GET",
url: '/home/GetInfoToExportCalgary',
success: function (data) {
//console.log(data);
CalgaryBookingInfo = data;
for (var i = 0; i < CalgaryBookingInfo.length; i++) {
if (CalgaryBookingInfo[i].length != 0) {
//console.log(CalgaryBookingInfo[i][0].Company + " has bookings")
var SumTime = 0;
for (var j = 0; j < CalgaryBookingInfo[i].length; j++)
{
CalgaryNotNullBookings.push({
Company: CalgaryBookingInfo[i][j].Company,
Location: CalgaryBookingInfo[i][j].Location,
PaidHours: CalgaryBookingInfo[i][j].HoursPaid,
Start: CalgaryBookingInfo[i][j].Start,
End: CalgaryBookingInfo[i][j].End,
Time: CalgaryBookingInfo[i][j].Time,
Total: SumTime + CalgaryBookingInfo[i][j].Time
});
SumTime += CalgaryBookingInfo[i][j].Time;
}
CalgaryNotNullBookings.push({
Company: "",
Location: "",
PaidHours: "",
Start: "",
End: "",
Time: "",
Total: SumTime
});
}
}
console.log(CalgaryNotNullBookings);
//buildHtmlTable(RetreivedClients);
}
})
AJAX发送数组:
在这里,我只是发送一个POST,其中包含我预先生成的数据并单击一次按钮即可调用。
function ExportCalgary(data) {
$.ajax({
type: "POST",
url: '/home/ExportCalgary',
data: data,
success: function (data) {
console.log("huehuehuehuehueh");
if (data.status) {
//Refresh the calender
//location.reload();
$('#myModalSave').modal('hide');
}
},
error: function () {
alert('Failed');
}
})
}
控制器开机自检功能:
[HttpPost]
public JsonResult ExportCalgary(List e)
{
var status = false;
//string[] columns = { "Company", "Location", "PaidHours", "Start", "End", "Time" , "Total" };
//byte[] filecontent = ExcelExportHelper.ExportExcel(dataToConvert, "Technology", true, columns);
//return File(filecontent, ExcelExportHelper.ExcelContentType, "CalgaryClientInfo_" + DateTime.Now.Year+"_" + DateTime.Now.Month + ".xlsx");
ExcelPackage excel = new ExcelPackage();
var workSheet = excel.Workbook.Worksheets.Add("Sheet1");
workSheet.TabColor = System.Drawing.Color.Black;
workSheet.DefaultRowHeight = 12;
//Header of table
//
workSheet.Row(1).Height = 20;
workSheet.Row(1).Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
workSheet.Row(1).Style.Font.Bold = true;
workSheet.Cells[1, 1].Value = "Company";
workSheet.Cells[1, 2].Value = "Location";
workSheet.Cells[1, 3].Value = "PaidHours";
workSheet.Cells[1, 4].Value = "Start";
workSheet.Cells[1, 5].Value = "End";
workSheet.Cells[1, 6].Value = "Time";
workSheet.Cells[1, 7].Value = "Total";
//Body of table
//
int recordIndex = 2;
for(int i = 0; i < e.Count; i++)
{
workSheet.Cells[recordIndex, 1].Value = e[i].Company;
workSheet.Cells[recordIndex, 2].Value = e[i].Location;
workSheet.Cells[recordIndex, 3].Value = e[i].PaidHours;
workSheet.Cells[recordIndex, 4].Value = e[i].Start;
workSheet.Cells[recordIndex, 5].Value = e[i].End;
workSheet.Cells[recordIndex, 6].Value = e[i].Time;
workSheet.Cells[recordIndex, 7].Value = e[i].Total;
recordIndex++;
}
workSheet.Column(1).AutoFit();
workSheet.Column(2).AutoFit();
workSheet.Column(3).AutoFit();
workSheet.Column(4).AutoFit();
workSheet.Column(5).AutoFit();
workSheet.Column(6).AutoFit();
workSheet.Column(7).AutoFit();
string excelName = "CalgaryClientInfo_" + DateTime.Now.Year+"_" + DateTime.Now.Month;
using (var memoryStream = new MemoryStream())
{
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=" + excelName + ".xlsx");
excel.SaveAs(memoryStream);
memoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
status = true;
}
return new JsonResult { Data = new { status = status } };
}
当前,当我调试时,列表“ ClientBookingInfo e”返回NULL,我不确定是怎么回事。 可能与JSON的编码或数据类型有关吗? 我是否缺少一些行,这些行指定要发送的JSON将是数组?
这也是类ClientBookingInfo:
public class ClientBookingInfo
{
public string Company { get; set; }
public string Location { get; set; }
public int PaidHours { get; set; }
public DateTime Start { get; set; }
public DateTime End { get; set; }
public int Time { get; set; }
public double Total { get; set; }
}
提前致谢! :)