Tuesday, June 27, 2017

Export ASP.NET GridView To Excel and Preserve Leading Zeros

Hi,
There was a question raised on how to export data from ASP.NET Gridview to Microsoft Excel and preserve leading zeros of numeric values. A workaround that we have is to insert an   character before the cell value if that value starts with zero (0). So if the value starts with zero such as 0001, the result would be " 0001". Thus when exported to Excel, the leading zero is kept.
cell.Text = string.Format(" {0}", cell.Text);
A suggested solution is to insert a Tab character ( ) but this fails on some occasions. After doing some research, replacing the tab character with   or   would also fix the issue.
cell.Text = string.Format(" {0}", cell.Text);
//or
cell.Text = string.Format(" {0}", cell.Text);

ASP.NET GridView Export Excel Report
Cheers!

0 comments:

Post a Comment