Click here to Skip to main content
Click here to Skip to main content

Tagged as

Simplest code to export a datatable into .csv format

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

Introduction 

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).
                                  ToArray();

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

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

File.WriteAllLines("excel.csv",lines);</datacolumn></string>

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

License

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

Share

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...
Follow on   LinkedIn

Comments and Discussions

 
QuestionBe bit more clear PinmemberNeewbie20-Mar-14 4:01 
AnswerRe: Be bit more clear Pinprofessionalsisir patro20-Mar-14 4:09 
GeneralRe: Be bit more clear PinmemberNeewbie20-Mar-14 4:29 
GeneralRe: Be bit more clear Pinprofessionalsisir patro20-Mar-14 4:35 
GeneralRe: Be bit more clear PinmemberNeewbie20-Mar-14 4:50 
QuestionGood one Pinmembersatyavnit27-Nov-13 20:27 
GeneralI fixed your code [modified] PinmemberMember 1032400821-Nov-13 11:45 
GeneralRe: I fixed your code Pinprofessionalsisir patro27-Nov-13 3:17 
GeneralRe: I fixed your code PinmemberJeff Hare23-Jan-14 15:22 
SuggestionPlz Give Full COde Pinmemberclick2karthikeyan20-Nov-13 2:24 
Plz Give Full Code
 
thanks In Advance
QuestionYou did say that it was simple... PinmemberFedwards6-Aug-13 16:43 
AnswerRe: You did say that it was simple... Pinprofessionalsisir patro7-Aug-13 0:41 
GeneralMy vote of 1 PinmemberSharique Shueb9-Jul-13 17:15 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141220.1 | Last Updated 10 May 2013
Article Copyright 2013 by sisir patro
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid