我生成excel文件的代码如下:
protected void btnReport_Click(object sender, EventArgs e)
{
try
{
Session["institute"] = "ABC";
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
SqlCommand cmd = new SqlCommand("select * from alumni_details where institute_name='" + Session ["institute"].ToString () + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GenerateXLSXFile(ds .Tables [0]);
}
catch (Exception)
{
throw;
}
}
当我调用此函数时生成 excel 报告的函数出现上述错误
private void GenerateXLSXFile(DataTable tbl)
{
dynamic excelPackage = new ExcelPackage();
dynamic excelWorksheet = excelPackage.Workbook.Worksheets.Add("DemoPage");
excelWorksheet.Cells("A1").LoadFromDataTable(tbl, true);
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=ExcelDemo.xlsx");
MemoryStream stream = new MemoryStream(excelPackage.GetAsByteArray());
Response.OutputStream.Write(stream.ToArray(), 0, stream.ToArray().Length);
Response.Flush();
Response.Close();
}