Monday, March 11, 2019

Gridview Save as excel without dropping leading 0's (zeros) C# sap.net

 protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=ForecastData.xls");
        Response.Charset = "";
        Response.Write("
");

        Response.Write("ForecastData");
        Response.Write("
");
        //Response.Write("Date:-  " + DateTime.Today.ToShortDateString());
        Response.Write("

");


        // If you want the option to open the Excel file without saving then
        // comment out the line below
        //Response.Cache.SetCacheability(HttpCacheability.No Cache);
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new
        HtmlTextWriter(stringWrite);
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            GridViewRow row = GridView1.Rows[i];
            //Apply text style to each Row
            row.Attributes.Add("class", "textmode");
        }
        GridView1.RenderControl(htmlWrite);
        string style = @"";
        Response.Write(style);
        Response.ContentType = "application/text";
        Response.Write(stringWrite.ToString());
        Response.End();

    }
code by Neha