Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hi,
 
I have been assigned a task where I am fetching some records from Database in data table and then I am exporting it to .csv file. the file is being written properly but the formatting is not there i mean everything is coming under on cell only.
Can smeone plsss help where m I going wrong ??
My code is gvn here:-
 
private static void CreateCSVFile(DataTable dt, string strFilePath, string webServerFileLink)
{
   // Create the CSV file to which grid data will be exported.
   StreamWriter sw = new StreamWriter(strFilePath, false);
 
   // First we will write the headers.              
   int iColCount = dt.Columns.Count;
 
   for (int i = 0; i < iColCount; i++)
   {
      sw.Write(dt.Columns[i]);
      if (i < iColCount - 1)
      {
         sw.Write(",");
      }
   }
 
   sw.Write(sw.NewLine);
 
   // Now write all the rows.
   foreach (DataRow dr in dt.Rows)
   {
      for (int i = 0; i < iColCount; i++)
      {
         if ((!Convert.IsDBNull(dr[i])) && (i == 7))
         {
            sw.Write(dr[i].ToString().Replace("\r", " "));
         }
         else
         {
            sw.Write(dr[i].ToString().Replace(",", " "));
         }
 
         if (i < iColCount - 1)
         {
            sw.Write(",");
         }
      }
 
      sw.Write(sw.NewLine);
   }
 
   sw.Close();
}
 
[Edit] Code formatting.
Posted 10-May-12 0:13am
Edited 10-May-12 0:26am
phil.o35.7K
v2
Comments
Zoltán Zörgő at 10-May-12 8:20am
   
You cannot skip null values! Treat them as empty strings.
SAKryukov at 10-May-12 14:58pm
   
Why doing it?
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I think this is a waste of your time. Who cares if CSV files are formatted well or not? Who works with them manually? They a mostly used to transfer data from one system to another or something like that. After all, create some software which represents any CSV in a form of some data grid or just use some available spreadsheet software. Or convert CSV to HTML.
 
Text files are not really designed to present table-like data. Your method is based on fixed-width font, which are rarely used. If you use tabs, it may look good in one editor and gets mangled in another one just because the tab widths are different. You are just dealing with dirt. Don't waste your time on it.
 
—SA
  Permalink  
v2
Comments
Monjurul Habib at 11-May-12 1:22am
   
well said, 5!
SAKryukov at 11-May-12 10:35am
   
Thank you, Monjurul.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

If you mean that when you open the CSV file in Excel, everuthing is in one cell. If that's the case, the likeliest reason is that you have a mismatch in the list separator. When opening the CSV, check that you're using the correct delimiter.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

I think that nobody cares whether CSV is format or not, As SAKryukov says. You can refer to How to Datatable to CSV[^].
 
Regards.
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 150
2 Richard MacCutchan 135
3 Andreas Gieriet 100
4 DamithSL 95
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,260


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 11 May 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100