Click here to Skip to main content
13,201,763 members (75,496 online)
Click here to Skip to main content
Add your own
alternative version


5 bookmarked
Posted 10 May 2013

Simplest code to export a datatable into .csv format

, 10 May 2013
Rate this:
Please Sign up or sign in to vote.
Code to export datatable to CSV format.


Hello friends we all go for a complex coding for exporting the data into excel sheet. Some times we face a problem that we are not having the interop dll for export to excel. So here is a solution for all of us which will help in exporting the data into .csv format.

Using the code  

Here is a simple code.

var lines = new List<string>();

string[] columnNames = dataTable.Columns.Cast<datacolumn>().
                                  Select(column => column.ColumnName).

var header = string.Join(",", columnNames);

var valueLines = dt.AsEnumerable()
                   .Select(row => string.Join(",", row.ItemArray));            
lines.AddRange(valueLines );


This is the most simplest code that i have used. You all will also find this interesting.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sisir Patro
Software Developer
India India
Learn the base not only the requirement related things for quick fixing and make others learn the exact thing always...

You may also be interested in...


Comments and Discussions

QuestionBe bit more clear Pin
Neewbie20-Mar-14 3:01
memberNeewbie20-Mar-14 3:01 
AnswerRe: Be bit more clear Pin
sisir patro20-Mar-14 3:09
professionalsisir patro20-Mar-14 3:09 
GeneralRe: Be bit more clear Pin
Neewbie20-Mar-14 3:29
memberNeewbie20-Mar-14 3:29 
GeneralRe: Be bit more clear Pin
sisir patro20-Mar-14 3:35
professionalsisir patro20-Mar-14 3:35 
GeneralRe: Be bit more clear Pin
Neewbie20-Mar-14 3:50
memberNeewbie20-Mar-14 3:50 
QuestionGood one Pin
satyavnit27-Nov-13 19:27
membersatyavnit27-Nov-13 19:27 
GeneralI fixed your code Pin
Member 1032400821-Nov-13 10:45
memberMember 1032400821-Nov-13 10:45 
GeneralRe: I fixed your code Pin
sisir patro27-Nov-13 2:17
professionalsisir patro27-Nov-13 2:17 
Thanks a lot... Smile | :)

Thanks & Regards,
sisir patro

GeneralRe: I fixed your code Pin
Jeff Hare23-Jan-14 14:22
memberJeff Hare23-Jan-14 14:22 
SuggestionPlz Give Full COde Pin
click2karthikeyan20-Nov-13 1:24
memberclick2karthikeyan20-Nov-13 1:24 
QuestionYou did say that it was simple... Pin
Fedwards6-Aug-13 15:43
memberFedwards6-Aug-13 15:43 
AnswerRe: You did say that it was simple... Pin
sisir patro6-Aug-13 23:41
professionalsisir patro6-Aug-13 23:41 
GeneralMy vote of 1 Pin
Sharique Shueb9-Jul-13 16:15
memberSharique Shueb9-Jul-13 16:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171020.1 | Last Updated 10 May 2013
Article Copyright 2013 by Sisir Patro
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid