locked
Add Header Row Export To Excel RRS feed

  • Question

  • User-807418713 posted

    Hello

    This is my export to excel code

     Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=Month_Wise.xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-excel";
            using (StringWriter sw = new StringWriter())
            {
                HtmlTextWriter hw = new HtmlTextWriter(sw);
    
                //To Export all pages
    
                GridView1.DataSource = Session["KJBG"];
                GridView1.DataBind();
    
    
                GridView1.RenderControl(hw);
    
                //style to format numbers to string
                string style =
                    @"<style>
            .grid-sltrow {
                background: #ddd;
                font-weight: bold;
            }
    
            .SubTotalRowStyle {
                border: solid 1px Black;
                background-color: #F7D47D;
                font-weight: bold;
            }
    
            .GrandTotalRowStyle {
                border: solid 1px Gray;
                background-color: #000000;
                color: #ffffff;
                font-weight: bold;
            }
    
            .GroupHeaderStyle {
                border: solid 1px Black;
                background-color: #4A766E;
                color: #ffffff;
                font-weight: bold;
            }
    
            .serh-grid {
                width: 85%;
                border: 1px solid #6AB5FF;
                background: #fff;
                line-height: 14px;
                font-size: 11px;
                font-family: Verdana;
            }
        </style>";
                Response.Write(style);
                Response.Output.Write(sw.ToString());
                Response.Flush();
                Response.End();
            }
    

    I want to add one row in excel "Report Generated On Today's Date 27-Dec-2019"

    How To Add This in above code

    Thanking You

    Friday, December 27, 2019 10:32 AM

Answers

  • User-1038772411 posted

    Hello Gopi.MCA,

    To add current Date & Time into header you have to just below line that we had attached.

    string headerTable = @"<Table><tr><td>Report Header</td></tr><tr><td><font style='color: #e6a5fa;'><b> <u>Report Generated On "+ System.DateTime.Now.ToString("dd-MMM-yyyy hh:mm:ss tt") +"</u></b></font></td></tr></Table>";

    If you found any issue please let us know.

    Thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 27, 2019 1:36 PM

All replies

  • User-1038772411 posted


    Hello Gopi.MCA,

    The code added for the Add Header Row Export To Excel

    .cs page

                Response.ContentType = "application/vnd.ms-excel";
                Response.AddHeader("Content-Disposition", "attachment; filename=Month_Wise.xls;");
                StringWriter stringWrite = new StringWriter();            
                string headerTable = @"<Table><tr><td>Report Header</td></tr><tr><td><font style='color: #e6a5fa;'><b> <u>Report Generated On 2019-12-27 </u></b></font></td></tr></Table>";
                Response.Write(headerTable);
                Response.Write(stringWrite.ToString());
                Response.End();

    You can also apply inline css.

    If you still facing issue, then please share your code.

    Thanks.

    Friday, December 27, 2019 11:22 AM
  • User-807418713 posted

    Hello

    Thanks For Your Reply

    How to Add current Date & Time

    Thanking You

    Friday, December 27, 2019 11:28 AM
  • User-1038772411 posted

    Hello Gopi.MCA,

    To add current Date & Time into header you have to just below line that we had attached.

    string headerTable = @"<Table><tr><td>Report Header</td></tr><tr><td><font style='color: #e6a5fa;'><b> <u>Report Generated On "+ System.DateTime.Now.ToString("dd-MMM-yyyy hh:mm:ss tt") +"</u></b></font></td></tr></Table>";

    If you found any issue please let us know.

    Thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 27, 2019 1:36 PM