Technical Aspects in IT

Stand on the shoulders of giants – http://scholar.google.com

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

Working with MS Excel(xls / xlsx) Using MDAC and Oledb – have not had a chance to look at this but it looks promising (VS2008)

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.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>