Datagrid to Excel export

Hi,

I have a datagrid placed on winform. I would like to export the whole grid to Excel file (into a single sheet). I found many code examples, but all of them are in ASP. Can anyone give me a code example in C# how I can export the datagrid to Excel in winform

Thank you.



Answer this question

Datagrid to Excel export

  • Dave Smalley

    Probably the easiest and most painless method is to either a) use CSV (comma seperated values) or to use an Excel OleDb Driver. Either methods would simply require that you create a OleDbConnection or OdbcConnection to the file. See www.connectionstrings.com  for more information. After that you merely manipulate it like you do any other Database connection using the appropriate OdbcCommand or OleDbCommand to insert rows , select data etc.




  • Datagrid to Excel export