i am exporting data from sql server to excel now i want to add logo at first row of excel sheet/work book?

Nouman Shah 0 Reputation points
2023-02-09T16:48:55.81+00:00

i am exporting data from sql server tables to excel workbook now i want to add logo at first row of each excel sheet

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,598 questions
Excel
Excel
A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
2,175 questions
SQL Server Reporting Services
SQL Server Reporting Services
A SQL Server technology that supports the creation, management, and delivery of both traditional, paper-oriented reports and interactive, web-based reports.
3,016 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,341 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 30,176 Reputation points Microsoft External Staff
    2023-02-10T05:27:03.3+00:00

    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();
                                    }
    
                                }
                            }
                          
                        }
                    }
                }
            }
    

    User's image

    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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.