我使用 EPPlus 生成 Excel。
这是我的行动
public ActionResult ExportReportToExcel()
{
var model = new ReportingViewModel();
int numOfInvolvedCompanies, numOfRefusedCompanies, numOfSuccessfullCompanies, numOfEmployeesInvolved, projectsCount;
model.Projects = db.GetProjectsReport(1, 1, out projectsCount, out numOfInvolvedCompanies, out numOfRefusedCompanies, out numOfSuccessfullCompanies, out numOfEmployeesInvolved);
model.AllProjectsReport.NumberOfCompanyInvolved = numOfInvolvedCompanies;
model.AllProjectsReport.NumberOfRefusedCompanies = numOfRefusedCompanies;
model.AllProjectsReport.NumberOfSuccessfullParticipated = numOfSuccessfullCompanies;
model.AllProjectsReport.NumberOfEmployeeInvolved = numOfEmployeesInvolved;
ExcelPackage excel = ExcelGenerator.GenerateReportingExcel(model);
string excelName = "Reporting";
using (var memoryStream = new MemoryStream())
{
try
{
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();
}
catch(Exception e)
{
throw;
}
}
return RedirectToAction("Reporting");
}
在尝试保存 excel 时,它会抛出 NullReferenceException excel.SaveAs(memoryStream) 行
这个案例有趣的一面是它在 2 天前还在工作。突然它停止工作,现在抛出这个错误。
暮色呼如
相关分类