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.