Using .NET to export data to Excel
Posted by scmay on August 6, 2009
Asked to transfer data from database in .NET VS2005 to Ms Excel (2007)
Tons of links/codes out there, but with many errors too.
Links that helped me:
How to Export Data to Excel from an ASP.NET Application + Avoid the File Format Differ Prompt – this summarizes a lot of the questions about exporting to Excel
Solutions that I came up with (with some improvisation)
Solution 1
This works, but always prompt with the error The file you are trying to open, ‘name.ext’, is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?
Solution to that issue is given in the link.
Solution 2 which uses OLEDB, improvised from the original code here. This looks good, but I haven’t done enough testing yet what happens when its opened in Excel 03 and client environment…will update this in due time.
* Tried pastebin, looks good. Lets hope my code remains permanent, and doesn’t disappear after a while.

