I have solved this myself...
My code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OfficeOpenXml;
namespace Downloadxlsx
{
public partial class ReadExcel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void lkl_Click(object sender, EventArgs e)
{
using (ExcelPackage pck = new ExcelPackage())
{
List<string> list = new List<string>();
list.Add("Name");
list.Add("Designation");
list.Add("Dob");
list.Add("Address");
ExcelWorksheet ws = pck.Workbook.Worksheets.Add("SearchReport");
int col = 1;
for (int i = 0; i < list.Count; i++)
{
ws.Cells[1, col].Value = list[i].ToString();
col++;
}
Byte[] fileBytes = pck.GetAsByteArray();
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Cookies.Clear();
Response.Cache.SetCacheability(HttpCacheability.Private);
Response.CacheControl = "private";
Response.Charset = System.Text.UTF8Encoding.UTF8.WebName;
Response.ContentEncoding = System.Text.UTF8Encoding.UTF8;
Response.AppendHeader("Content-Length", fileBytes.Length.ToString());
Response.AppendHeader("Pragma", "cache");
Response.AppendHeader("Expires", "60");
Response.AppendHeader("Content-Disposition",
"attachment; " +
"filename=\"ExcelReport.xlsx\"; " +
"size=" + fileBytes.Length.ToString() + "; " +
"creation-date=" + DateTime.Now.ToString("R") + "; " +
"modification-date=" + DateTime.Now.ToString("R") + "; " +
"read-date=" + DateTime.Now.ToString("R"));
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.BinaryWrite(fileBytes);
Response.End();
}
}
}
}</string></string>