Format Cells When Exporting to Excel.

If you export gridview to excel, sometimes you will face format difference.

For example date field is 082008 on your gridview, but in excel file it becomes 82008. If the excel file is imported to another system or app, it could be a problem.

So here is a solution.

In the middle of your export function, write this:

// <– This block allows to change format cell as ‘text’. If not, some content changes its format.
// Ex: 082008 becomes 82008 in an excel file.
foreach (TableRow i in gridview1.Rows)
{
foreach (TableCell tc in i.Cells)
{
tc.Attributes.Add(“class”, “text”);
}
}
string style = @”<style> .text { mso-number-format:\@; } </style> “;
Response.Write(style);
// –>

And then you will see that the format is just same as gridview.!

Thanks to an article on http://forums.asp.net/p/1134560/1821199.aspx#1821199

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: