Click here to Skip to main content
15,902,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to export data from a data table into .CSV format,

my problem is data is exporting into .csv format, but all columns are displayed under one column

i need to display data in separate columns,

please help me here, its urgent...
Posted

1 solution

Hello ,
CSV Files are used for Excel Data.To Give The No Of Heading or show Seperate Data comma is being used for SHowing in seperate Cells.
Add a namespace System.IO
Kindly have a look at the Code Below


C#
using (StreamWriter streamWriter = new StreamWriter("Report.CSV"))
                {
                  
                    streamWriter.WriteLine("Sr No,Column/Heading 1,Column/Heading 2");
//Loop Through your Table Example
//for(int i=0;i<dt.rows.count,i++)>
//{
//  streamWriter.WriteLine(string.Format("{0},{1},{2}",i+1,
//dt.Rows[i]["ColumnName"],dt.Rows[i]["ColumnName"]));
//}
//To Open a Excel File
  System.Diagnostics.Process.Start("Excel.exe", "Report.CSV");
}//Close Of Using Tag

Hope It Helps You
Happy Coding
:-)
 
Share this answer
 
Comments
ClimerChinna 25-Aug-14 6:36am    
Thanks a lot,but i want to know how to save this file in required path
Neetin_1809 25-Aug-14 7:28am    
Any Idea For FileName?Otherwise it will be overwrite

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900