Click here to Skip to main content
15,908,661 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Every one,

I am filling Grid with some date(from database),and exporting into excel.After exporting in to excel, the value 07896542 shown as 7896542 i.e. left side zero trimming. Please help me, how can I format excel column while exporting to excel. see the following code.

Thanks in advance.


if (Excel == "YES")
{
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid dgGrid = new DataGrid();
dgGrid = new DataGrid();
dgGrid.DataSource = dtinvoice;
dgGrid.HeaderStyle.Font.Bold = false;
dgGrid.DataBind();
dgGrid.RenderControl(hw);
Response.AddHeader("Content-Disposition", "attachment;filename=Executivereport.xls");
Response.ContentType = "application/vnd.ms-excel ;charset=utf-8";
Response.ContentEncoding = System.Text.Encoding.Unicode;
Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
this.EnableViewState = false;
Response.Write(tw.ToString());
Response.End();
}
Posted
Updated 13-Aug-13 20:20pm
v2
Comments
idenizeni 14-Aug-13 11:53am    
Excel tries to help and format things that look like numbers. This is what removes the leading zeros. You need to tell excel to treat them like text. See my solution below for examples on how to force excel to treat the values as text.

1 solution

Have you tried to prefix your value with a single quote? Like so...

'07896542

If this doesn't work then try wrapping your value in quotes and placing an equal sign in front of it. Like so...

="07896542"
 
Share this answer
 
v2

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