Project Description
Hello Folks,

Raise your hands if you enjoy the power of "Zero Code" ? That's everyone. Thanks to ASP.NET 2.0. Most of the heavy lifting is done by the framework.

I would like to present to you a new custom control that I developed. This control "Exports" the contents of your "GridView" into Excel format with "Zero Code". That's right. Simply drag and drop and you are all set.

Note:

Copyright (c) Rajesh Krishnamohan. All rights reserved.
This software is provided "AS IS", without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.

Export Gridview to Excel

Features:

1) Styling : If you used "HeaderStyle", "RowStyle", "AlternateRowStyle" in your GridView, you can simply enable styling by setting the "ApplyStyleInExcel' property to "True". Note: CSS styling is not supported at this point future candidate

2) TimeStamp: If you set the "IncludeTimeStamp" to true, the filename is appended with a "time Stamp". These are very useful for time sensitive reports.

3) PageSize: If you are using Pagination in your GridView, you can choose to export the "Current" page, the "Entire Grid" all pages or set a specific number of records to export. Use "All" for all pages, "Current" for the current page and 20 for first 20 records.

4) Sorting: The Export supports sorting. In otherwords, if you sort your gridview and then export, the excel spreadsheet will also be sorted.

5) GridViewID: If you have more than one gridview in a page, you can specify which gridview you'd like to export by setting this property. You do not have to set any value if you just have one grid view.

6) FileName: You can set the filename that you want to export.

7) EnableHyperLinks: If set to "True" the exported excel file will have "Live" links. If set to "False", only the text will be exported. Alpha 1.1 release feature

*Following are NEW in BETA version**

8) Charset - Allows you to setup a Charset Multi-lingual support

9) ContentEncoding - Allows you to set the Encoding Multi-lingual support

10) ColumnsToExclude - Allows you to input the columns comma separated that you'd like to exclude in your export.

11) PreExport Event - For those of you who dynamically bind the GridView at runtime, use this new event to databind the GridView just before "Exporting"


Usage

Simply drag and drop the ExportToExcel button on your page and enjoy "Zero code" export feature.
Last edited Feb 7 2008 at 2:19 PM by sendemailtorajesh, version 8
Comments
JMPNW wrote  Sep 19 2007 at 3:49 PM 
Can you please post the source?

joyceodom wrote  Sep 24 2007 at 10:15 PM 
This works great, but I do have a slight issue. My first column is a template field that contains an html link (I needed the mouseover events):
<a href='Page.aspx?ID=<%# Eval ("ID") %>' onmouseover='dosomething;' onmouseout='dosomething;'> <%# Eval("ID") %> </a>
This causes problems when exporting.

sendemailtorajesh wrote  Sep 27 2007 at 6:37 PM 
joyceodom, thank you for your feedback, use the latest release and the new property I added called "EnableHperlinks" to take care of your situation. Hope that helps!

joyceodom wrote  Sep 27 2007 at 7:43 PM 
Awesome, works great!

glindo wrote  Sep 30 2007 at 9:26 PM 
Hi,
Any chance you'll be put up the source code anytime soon? I'd love to take a look at it.
Thanks,
Garfield.

sgt49 wrote  Oct 17 2007 at 5:48 PM 
Does it matter if it's used in a web site or web access project? I used it in my wap and the button is not enabled.

JMPNW wrote  Oct 25 2007 at 9:26 PM 
This is supposed to be an "open source" project web site. I'm going to vote to have this one removed since the author refuses to share the source.

sendemailtorajesh wrote  Oct 26 2007 at 2:18 PM 
JMPNW and others,

The source code is available now. I've been out on personal emergency and still trying to catch up with things. Thanks for your understanding....

emanlee wrote  Oct 30 2007 at 4:36 PM 
Could you please give a example on how to use your codes? Thanks a lot.
eman_lee@hotmail.com

sendemailtorajesh wrote  Oct 31 2007 at 5:59 PM 
emanlee, as far as using this component, go to the "Releases" tab and follow the instructions.

Mabyre wrote  Nov 13 2007 at 2:38 PM 
Impossible for an "Open Sources Project" that sources are not there.
So please post the source ! Otherwise I will not use your stuffs.

mrh wrote  Dec 4 2007 at 2:15 AM 
How to export images to excel? I know use the <img> tag, but the images are done once the doc is offline.
Please help! email: mrh_wang@yahoo.ca

sendemailtorajesh wrote  Dec 5 2007 at 4:11 PM 
Mabyre!! I am not sure what you are talking about. The source is available in the sources tab.

Are you a newbie to this site? Do you know how to navigate around codeplex.com? I suggest you read the CodePlex FAQs to figure out some basic stuff.

dwdrumz wrote  Feb 7 2008 at 3:46 PM 
This control is really nice. Thank you for all your hard work. I had a question concerning it's use. I have a form that has a gridview control that contains a nested gridview control as well (creates a master/detail form). When I use export to excel control in this type of scenario it throws an error ("System.Web.HttpException: Control 'grdSKU' of type 'GridView' must be placed inside a form tag with runat=server."). I am assuming this is due to the grids being nested (since it works perfectly fine in all my other pages that don't use nested grids). Is there any way to add this on as a future feature? Thanks!

sendemailtorajesh wrote  Feb 7 2008 at 6:17 PM 
I just fixed the issue with Nested Gridviews. I uploaded the latest component. Please download that and test if it works well. As always, thank you for your feedback. I'd really appreciate if you could let me know if this bug is fixed.

Thanks again

dwdrumz wrote  Feb 8 2008 at 2:23 PM 
The control no longer throws the error....but....it does not include the content from the nested grid. It leaves that content out and just renders the main grid. Any way to include that? Also, I am having the issue with the image control as well. I understand what is going on and really it's not a deal breaker to not have the image. But is there any way to add a feature to be able to set what the control replaces the image with? For instance, I use the image to denote a Yes/No situation. It would be a nice feature to specify to leave the image out and just show "Yes" in place of the image. Of course you run into situations where you have multiple columns that use images, so maybe then you could specify a string of comma delimited control names and their proper substitutions. Just an idea.

sendemailtorajesh wrote  Feb 8 2008 at 7:49 PM 
It works for me [the content of the nested gridview is also exported. Try to call the databind on the parent gridview in the "PreExport" event and see if that solves. Rest of the ideas noted down. Thanks!

dwdrumz wrote  Feb 8 2008 at 8:39 PM 
That's strange. I added a bind call for the parent grid on that event and still no luck. In fact, I don't even get the column name to render in the master grid where the child data is shown. I have everything on the aspx page as controls and I create some dynamic SQL on the page load and then do my bind there to normally register the page. Not sure what else to try at this point.

sendemailtorajesh wrote  Feb 9 2008 at 2:06 PM 
Since there are more than one gridviews in your example, did you set the "GridViewID" property of the "ExportToExcel" control? If you already did and the export is not working, will you please email me your code so that I can replicate this on my development environment? Sorry this is taking few iterations to get this straight, but trust me, it is worth the time saved :-) Thanks for your patience.

dwdrumz wrote  Feb 9 2008 at 5:06 PM 
I sent you a notification via this web site with a link to download my code. Thanks again for all your help. It is much appreciated.

tokke wrote  Feb 20 2008 at 11:45 AM 
Hi, Can we get the Beta version Source code? Currently available source code doesn't have beta functions in it. Thank u in advance :)

eclipse wrote  Feb 22 2008 at 4:17 PM 
Kudos to you, and thanks for sharing. This is one the most comprehensive Gridview to Excel Codesets I've seen to date. Can you please post the source Code to from the latest beta? I would like to extend some of the new functionality to export columns based upon the Visible property or the row cell.

sendemailtorajesh wrote  Feb 26 2008 at 1:28 AM 
tokke,eclipse,

Thanks for your patience....My hard disk crashed couple of weeks back and I am just rebuilding my PC. Please allow few more days before I post the "Beta" source code.

Thank you

tokke wrote  Mar 11 2008 at 9:26 AM 
Thank u for the new source code.

countrygirl731 wrote  Mar 14 2008 at 3:53 PM 
Hi rajesh,

Thank you for this wonderful code. I am having a problem I can't figure out. My exported file only contains grid headers. I am populating grdiview at runtime and use the preExport event to bind again, but when I do this I get the following error:

Invalid attempt to call FieldCount when reader is closed.

Any suggestions?

sendemailtorajesh wrote  Mar 15 2008 at 2:09 AM 
Will you please email me your code?

nmreddy wrote  Mar 25 2008 at 3:49 PM 
Hi Rajesh, Can you please help me to use this Control in AJAX Update panel..!

thank you,
-nm reddy

sendemailtorajesh wrote  Mar 25 2008 at 10:08 PM 
The trick is to place the "Export To Excel" control outside your "Update Panel". Try that and see if it works. If not, please send me your code and I'll take a look at it.

americop wrote  Apr 17 2008 at 11:34 PM 
Cool control but I'm a bit confused about how to use the PreExport Event. I have a page where I use a dropdown to select a from a list of canned queries that I then bind to the datagrid on my page. When I use the control on a page where the grid is not bound in code it works just fine, but when I use it on my page I only get headers in my .xls file. I think this is solved by use of the PreExport Event but I'm not sure how! Thanks for any help you can give.

sendemailtorajesh wrote  Apr 18 2008 at 2:15 PM 
If you are "Dynamically" populating your grid based on your workflow, you must use the "PreExport" event to rebuild the grid.

ptown wrote  May 9 2008 at 4:38 PM 

I added the Export-To-Excel button to a gridview control with a footer row and receive a message saying the " Control 'NewHRID' of type TextBox must be placed inside a form tag with runat=server. But When I add the Export-To-Excel button to a gridview control without a footer row, everything works fine.

Any suggestions?

ptown wrote  May 9 2008 at 4:55 PM 
The message saying the " Control 'NewHRID' of type TextBox must be placed inside a form tag with runat=server is refering to the gridview section below:

<asp:TemplateField HeaderText="HRID" SortExpression="HRID">
<EditItemTemplate>
<asp:TextBox ID="tbxHRID" runat="server" Text='<%# Bind("HRID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label31" runat="server" Text='<%# Bind("HRID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="NewHRID" runat="server" OnDataBinding="NewHRID_DataBinding"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>

Thank you,

lokus wrote  May 9 2008 at 9:09 PM 
I have a search page that doesn't have a gridview displayed until a user enters a search term and hits the search button. When adding this control to the page it is not active but visible. When the search results are returned the button is still inactive and visible. How do I make the button not visible until the gridview returns a result and how do I make the button active when a result is returned in a gridview? Thanks and excellent control!

sendemailtorajesh wrote  May 12 2008 at 1:32 PM 
ptown, I've seen this happening with hidden field as well. I haven't got a chance to fix this yet [work's keeping me busy]. Let me record this as a bug for now.

lokus, The reason why the button is disabled is because, you are building the grid after the "init" event of the export to excel control fires. What you can do is, after you build the grid, you can manually set the "Enabled" property to "true". Will that work for you?

lokus wrote  May 14 2008 at 8:55 PM 
sendemailtorajesh:
That worked but when clicking on the Export to Excel button I am receiving this error:
---------------------------
Windows Internet Explorer
---------------------------
Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.

Details: Error parsing near '<table rules="all" b'.
---------------------------
OK
---------------------------

lokus wrote  May 14 2008 at 9:15 PM 
sendemailtorajesh:
It seems there was an issue when using it with AJAX updatepanels and updateprogress. When I export the file now I only see gridview headers. In the PreExport event, I am doing gridview.DataBind(); but I am still only seeing the headers.

sendemailtorajesh wrote  May 16 2008 at 3:07 AM 
lokus, If you could send me your code, that would be helpful. It should be something simple. If you are dynamically building your grid, please make sure that your datasource returns data during post back.

MartynSt wrote  May 18 2008 at 9:51 PM 
Hi Sendemailtorajesh
Awesome Control, I have it working great on the normal GridView but i'm having problems with another Grid called "CompleteGridView", originally the Export to Excel control was not enabled, i made some changes in the source & it's now enabled but i'm getting this error message: Any help greatly appreciated - Thanks

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
KrishLabs.Web.Controls.ExportToExcel.ExportToExcel_OnClick(Object Sender, EventArgs e) +89
KrishLabs.Web.Controls.ExportToExcel.ExportToExcel_Click(Object sender, EventArgs e) +87
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
KrishLabs.Web.Controls.ExportToExcel.RaisePostBackEvent(String eventArgument) +96
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1746

sendemailtorajesh wrote  May 21 2008 at 1:35 PM 
Thanks. I am not certain about the changes you made to the source. Best option is to set a "break point" in the "OnClick" event and see why it fails. My guess is that the gridview rows collection is returning a null reference.

luismolina wrote  Jun 10 2008 at 1:38 PM 
hi, i have a problem, i have some data in the format 10001 or 00001, but when exported to excel it converts 00001 -> 1, how can i prevent this?

Jagarm wrote  Jul 11 2008 at 3:43 PM 
Helloo sendemailtorajesh,

This is an excellent control that you made. It works great for me, but when there are no controls such as Button, checkbox and other stuff. Is there any way around this?

Thanks

timtbone13 wrote  Aug 26 2008 at 4:18 PM 
Hi Rajesh,

I love the control, however i'm having trouble getting it to work for gridviews within Update Panels that are part of User Controls that are all within a tabcontainer on one single aspx page. I know sounds a little hairy, but actually everything works except the Export to Excel now breaks. I tried to set the GridViewID path to the Container/panel/usercontrol/gridview but when doing so disables the button.

Please let me know if i'm attacking this the wrong way or if i'm messing a setting of some sort.

Thanks

prgopal wrote  Sep 17 2008 at 2:01 PM 
Hi,

This is an excellent control that you made. It works great for me , but I need clarification for below given scenario

Assume that I got 50,000 records from database, but I have implemented paging and showing 100 recodes each time but I want export all the 50,000 records in one shot when I click the export to excel button.

Thanks

ARC2007 wrote  Sep 26 2008 at 4:17 PM 
Coool control!
Worked for me in the first shot without any issue...
Question: One of my grid column is string type , but data is longinteger (7319521234567), when it displays in the excel it is coming as 7.31952E+12. I can then manual format the excel to look right.. but i was wondering if anything we can do on the control get it right.

Thanks

mandar44 wrote  Nov 4 2008 at 3:48 PM 
Hi,

The problem ...
I am not able to export Gridview from a Modal Popup Window.
I can export from a normal window.

Need your help badly.

Regards,
Mandar

bolson50 wrote  Nov 14 2008 at 8:46 PM 
Hello
this is way cool but i am only have the column names export and not the data what i am doing wrong?
Private Sub ExportToExcel1_PreExport(ByVal Sender As Object, ByVal e As System.EventArgs) Handles ExportToExcel1.PreExport
ExportToExcel1.ExportFileName = txtRptname.Text
gvReport.DataBind()
End Sub

Updating...
© 2006-2009 Microsoft | About CodePlex | Privacy Statement | Terms of Use | Code of Conduct | CodePlex Blog | Version 2008.12.9.14291