Datagrid Girl

Marcie, ASP.NET Datagrid Blogger Girl

Datagrid to Excel Formatting Tip

When I signed up for a blog account here, I thought all my entries would be about Datagrids. But I've been neglecting what is really my favorite topic, so tonight I'll share a fantastic tip that came to me today from a visitor to my site, Jim Cristofono.

I've been doing a lot recently with Datagrids and Excel, and Jim and I have corresponded on this topic a bit. One limitation (or so I thought), of using the RenderControl method to export a Datagrid to Excel is that you have little or no control over the formatting once the data gets to Excel. For instance, large numbers get converted to the (ugly) x.xxxxxxE-yy syntax.

Jim came up with an super clever workaround for this problem--code below:

Response.Cache.SetExpires(DateTime.Now.AddSeconds(1));
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;
Response.Write("<html
xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
Response.Write("\r\n");
Response.Write("<style>  .mystyle1 " + "\r\n" + "
{mso-style-parent:style0;mso-number-format:\""+@"\@"+"\""+";} " +
"\r\n" +
"</style>");
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
DataGrid1.RenderControl(hw);
Response.AppendHeader
("content-disposition","attachment;filename=x.xls");
Response.Write(tw.ToString());
Response.End();
private void DataGrid1_ItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
int i=0;
if (e.Item.ItemType == ListItemType.Item | e.Item.ItemType ==
ListItemType.AlternatingItem)
   {
   foreach (cRecordTemplate MyRec in arrRec)
    {
    if (MyRec.FieldType=="dbChar")
       e.Item.Cells[i].Attributes.Add("class", "mystyle1");
    i++;
    }
   }
}

Happy Datagridding!

Comments

jbarber said:

Can you recommend a good site that just simply explains how to export data from a datagrid in VB.net to an excel file from a command button?
# July 29, 2003 4:24 PM

swingsetacid said:

i think this snippet of code would be a lot more helpful if you more of the code, including declaration of delegates and how to iterate through the items in your item_created eventhandler. arrRec is never defined and it's difficult to understand why you're doing that without more information.

thanks!
# November 18, 2003 6:56 PM

shiv said:

its has helped me lot in my project
# December 4, 2003 1:49 AM

Dan Abramson said:

How do I center text in an Excel Spreadsheet using VB.net from a regular form. I have the data being populated from my datagrid but am unable to center it or give it a decimal or currency format.

Any help is greatly appreicated.

Sincerely,
Dan Abramson
danvbman@aol.com
# February 5, 2004 11:50 AM

Gopi Koganti said:

for text format use
mso-number-format:"\@";

for currency format use
mso-number-format:"\0022$\0022\#\,\#\#0\.00";

to center text use
text-align:center;

For example, to center and format ALL cells to text
following line should be added using response.write() at top of the page.

<style> td {mso-number-format:"\@"; text-align:center;} </style>

Hopefully that should give you an idea.

GK
# February 5, 2004 6:52 PM

jserra said:

Any thoughts on how to get the results to display in a new browser window (pop-up style)?

thanks!

js
# March 8, 2004 7:50 PM

revenge said:

just what i have been after
# March 16, 2004 2:50 AM

matt said:

Very useful tips!
Ok, how do you make sure that the cell border is on for all cells?
# July 2, 2004 4:19 AM

TrackBack said:

# December 10, 2004 9:29 AM

TrackBack said:

# December 10, 2004 9:36 AM

TrackBack said:

# February 3, 2005 8:24 AM

Arnab said:

Any thoughts on how to get the results to display in a new browser window (pop-up style)?

# May 25, 2007 1:49 AM

2lits said:

Hi, do you know how to add pivot when exporting datagrid to excel

# May 29, 2007 8:58 AM

2lits said:

Hi this is one cool site.. Can you help me create a pivot table on the create xls? thanks

# May 29, 2007 9:19 AM

Aparna said:

Thanks a million . Your code fragment helped me a lot.

# June 22, 2007 7:32 AM

Aman Sharma said:

Thanks for the example. I was in desparate need for this solution.

Thanks aTon!!

God bless you

# June 25, 2007 10:20 AM

shiv said:

This coding is use full but please explain that arrRec.

# July 20, 2007 2:49 AM

Dicontas - Centralized Services for Email Filtering said:

When you feel discouraged because of the terrifying amount of effort it involves to discover what you are searching for, cheer up because you\'re one step closer to your goal.

# September 11, 2007 11:57 PM

Joe said:

Thanks. This really helped. The comment from Gopi Koganti was important as well

# September 18, 2007 11:46 AM

haris101 said:

Hi, I don't understand following line:

foreach (cRecordTemplate MyRec in arrRec)

What are cRecordTemplate and arrRec?

# October 1, 2007 3:20 AM

suresh said:

am facing a problem while exporting data from a Datagrid (Infragistics) to excel.

The grid has some dropdowns and data error validations. I want that when I export them to excel the drop downs must be created and the data validation like max length, numeric characters etc must be checked in the excel sheet itself. I am easily able to create the Excel Workbook. But before writing it to a file I want to put drop downs in certain cells.

# October 31, 2007 7:13 AM

vijay said:

Hi,

i have a scenario where i am exporting variable having value 3.200, but when it comes to excel it is displayed as 3.2 only. last two zeros are removed.

how to format this?

Thanks in advance for your time.

# December 31, 2007 2:07 AM

Amol Sontakke said:

am facing a problem while exporting data from a Datagrid (Infragistics) to excel.

The grid has some dropdowns and data error validations. I want that when I export them to excel the drop downs must be created and the data validation like max length, numeric characters etc must be checked in the excel sheet itself. I am easily able to create the Excel Workbook. But before writing it to a file I want to put drop downs in certain cells.

# January 2, 2008 5:42 AM

Ashi Shah said:

i m Ashi Shah and i hope this will help me

# March 17, 2008 3:36 AM

Prabhu said:

Hi,

very nice solution, I was desperately looking for this solution.. thanks a million...

# March 20, 2008 2:50 AM

jack ni said:

# June 16, 2008 8:33 AM

Ashish said:

i have Gridview on my form i want exactly excel like features on my asp.net page for format data in Gridview. just like Google Documents Excel provides.

Please help

# September 27, 2008 1:07 AM

Madhu said:

Is it possible to set the currency symbol

# July 6, 2009 6:58 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)