This is a common task for ASP.NET developers. You have a Web application where you expose data from a database, Web service, or third-party API and you need an "Export to Excel" button. I have used different approaches to export to Excel from a Web application. Here's some options and guidance of when to use each one.
Note: I can't stop to mention the big no-no… using the Excel PIA to generate a spreadsheet server-side. This is not a good practice and it's not recommended or supported, so let's forget about this one.
Now, for those of you who are working with any of the first three approaches, you may have seen the prompt of file format differ each time you export to Excel.
You get this message because you are opening a file in Microsoft Office Excel 2007 that contains content that does not match the files extension.
I am not very annoyed about this, but I know some people are. By default, a user can decide whether to open the file when the warning message is displayed, but you can control user-notification either:
Here's a KB article that provides detailed steps for both options: When you open a file in Excel 2007, you receive a warning that the file format differs from the format that the file name extension specifies.
Here's a quick code sample in C# for approach 3 (Export from GridView) that you can use to export to Excel. I changed my registry following the steps explained in the previous article and it worked like a charm. No prompt!
Happy Friday!
PingBack from http://www.clickandsolve.com/?p=2418
Thank you for submitting this cool story - Trackback from DotNetShoutout
Hi,
Thanks for your Article. Its really nice one. Can you tell me how to export a dataset containing multiple tables to excel. I think this refers to your second approach.
Combine/Compress/MinifyJSandCSSfilesinASP.NETMVCHowtoExportDatatoExcelfromanASP.NET...
Combine/Compress/Minify JS and CSS files in ASP.NET MVC How to Export Data to Excel from an ASP.NET Application
Nicely written. Thanks for the article.
My DataSet contains value 002 whenever i download in DataSet to Excel. It shows only 2 but i need the following format 002 in Excel download file(Using ASP.Net-05, C#).
Is it possible. Please help me.
Nice article. Thanks for sharing it. Here's another very easy way how to export data to excel:
http://www.gemboxsoftware.com/GBSpreadsheet.htm
I've used this Gemox component for a while an it's really great component.
Great, but can we export 2 tables(gridviews) in to 1 excel file to 2 sheets?
Your code produces the following error:
RegisterForEventValidation can only be called during Render();
Nice article. Thanks. I do have a question on this. In our application we want users to be able to download a list of securities, their price dates and price (empty when downloading) in excel which I am able to do using grid view method above. But then the users are suppose to enter price of each row and upload it back to server where we insert them into DB. The problem is when we type a few prices and try to upload the file, it breaks saying invalid file format. We are using Jet oledb 4.0 to read the the uploaded excel file. The upload and database insert code works if I manually create an excel and type in the security, price date and price.
Any help would be appreciated.
i wann to know how to insert the image in to the excel sheet once i click the button .............
when exported to excel, it shows a pop-up with an 'Open' and 'Save' option. Click open - this opens the excel sheet with the content. Go to Files - 'Save as' option - you get to see 'Save as type' defaulted to
'Web Page(*.htm; *.html)'
How do we change the Default File Type to Excel?
Thanks in advance...
What is the purpose of
this.EnableViewState = false;
in the ExportToExcel() method?
Can an Excel file generated using the Open XML SDK 2.0 be sent to the client browser using 'Open/save' dialog box WITHOUT saving that file on the web server hard drive? (I create these files dynamically and aren't required on the server.)
Thank you.