C# – How to export Sql Server Data to Excel File in C#

cexport-to-excelsqlwinforms

How can one read sql server data and write it to excel file in C#?
Do I need to use an external .Net library, or is it just OK using Visual Studio itself?

Any example and link will be well appreciated..

Note: Please send examples for Winforms, not asp.net

Best Answer

I would use some kind of OR-Mapper to read data from SQL such as

  • NHibernate
  • Entity Framework
  • Linq 2 SQL

For writing data to Excel you could bind hardly to any Excel Version by referencing the Excel Assemblies. The cleaner and more beautiful way is to use Open XML SDK for Office http://www.microsoft.com/download/en/details.aspx?id=5124