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

Simplest code to export a datatable into .csv format

By , 10 May 2013
 

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)

About the Author

sisirp88
Software Developer
India India
Member
I love learning...

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130513.1 | Last Updated 10 May 2013
Article Copyright 2013 by sisirp88
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid