Thursday 14 November 2013

Export Data to Excel in ASP.NET C#

I am using third party tool to export data to excel. The third party tool is EPPlus. Click here to download the dll and add refference to your porject.

Now you can add the following code to your button click of Export to Excel.

List<Institutes> ExportData = new List<Institutes>();
  ExportData = objReport.GetInstitutesData();

  using (ExcelPackage pck = new ExcelPackage())
  {
   ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Institute Report");
   ws.Cells["A2"].LoadFromCollection(ExportData, true);
   ws.Cells["A1"].Value = "Institute Report";
   Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
   Response.AddHeader("content-disposition", "attachment;  filename=InstituteReport.xlsx");
   Response.BinaryWrite(pck.GetAsByteArray());
   Response.End();
  }
 

No comments:

Post a Comment