I want to write all the records from my table to an excel file. I have around 30000 rows in my table.
I am able to write 20000 rows to excel only. After the System out of memory
Exception is happening.
This is my code. Thanks in advance.
using (SqlConnection con = new SqlConnection(strconnection))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM tbl_ReportwithoutDup"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(dt, "Report1");
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename=Report1.xlsx");
using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
}
}
}
}
}