Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I export data from Gridview to excel but it left zero where value start with zero.

What I have tried:

Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-excel";
    using (StringWriter sw = new StringWriter())
    {
        HtmlTextWriter hw = new HtmlTextWriter(sw);

        //To Export all pages
        GridView1.AllowPaging = false;


        GridView1.HeaderRow.BackColor = Color.White;
        foreach (TableCell cell in GridView1.HeaderRow.Cells)
        {
            cell.BackColor = GridView1.HeaderStyle.BackColor;

        }
        foreach (GridViewRow row in GridView1.Rows)
        {
            row.BackColor = Color.White;
            foreach (TableCell cell in row.Cells)
            {


                if (row.RowIndex % 2 == 0)
                {
                    cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
                }
                else
                {
                    cell.BackColor = GridView1.RowStyle.BackColor;
                }

                cell.CssClass = "textmode";
                //cell.Style.Add("style", "mso-number-format:\\@");

            }
        }

        GridView1.RenderControl(hw);

        //style to format numbers to string
        string style = @"<style> .textmode { } </style>";
        Response.Write(style);
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();
    }
Posted
Updated 4-Oct-17 0:04am

1 solution

This could help your issue.. Any corrections accepted.

this class represents the structure of the data of the lookup_client_name content
public class RowContent
{
public string SomeProperty{ get; set; }
public string PreserveValues{ get; set; }
}
gv.DataSource = db.lookup_client_name.
Select(i => new
{
SomeProperty = i.SomeProperty,
PreserveValues = "'" + i.PreserveValues + "'"
}).ToList();
 
Share this answer
 
Comments
Member 12962919 4-Oct-17 6:07am    
i already tried something like this. if i add '' in value then it save as it is. e.g '05'

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900