Hi @Nouman Shah,
I think you can populate data from A2 . Then put the icon on the first line according to the coordinate position.
var imagePath = Server.MapPath("favicon.ico");
FileInfo img = new FileInfo(imagePath);
ExcelPicture pic = ws.Drawings.AddPicture("img", img);
pic.SetPosition(1, 1);
Demo
<asp:Button Text="Export" OnClick="ExportExcel" runat="server" />
protected void ExportExcel(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Student"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
using (ExcelPackage pck = new ExcelPackage())
{
ExcelWorksheet ws = pck.Workbook.Worksheets.Add("SearchReport");
ws.Cells["A2"].LoadFromDataTable(dt, true, TableStyles.Medium15);
var imagePath = Server.MapPath("favicon.ico");
FileInfo img = new FileInfo(imagePath);
ExcelPicture pic = ws.Drawings.AddPicture("img", img);
pic.SetPosition(1, 1);
string excelName = "studentsRecord";
using (var memoryStream = new MemoryStream())
{
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=" + excelName + ".xlsx");
pck.SaveAs(memoryStream);
memoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
}
}
}
}
}
}
Best regards,
Lan Huang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.