DataTable to CSV export

public static void ToCSV(this DataTable table, string delimiter, bool includeHeader)
        {

            StringBuilder result = new StringBuilder();


           
            if (includeHeader)
            {

                foreach (DataColumn column in table.Columns)
                {

                    result.Append(column.ColumnName);

                    result.Append(delimiter);

                }



                result.Remove(--result.Length, 0);

                result.Append(Environment.NewLine);

            }



            foreach (DataRow row in table.Rows)
            {

                foreach (object item in row.ItemArray)
                {

                    if (item is System.DBNull)

                        result.Append(delimiter);

                    else
                    {

                        string itemAsString = item.ToString();

                        // Double up all embedded double quotes

                        itemAsString = itemAsString.Replace("\"", "\"\"");



                        // To keep things simple, always delimit with double-quotes

                        // so we don't have to determine in which cases they're necessary

                        // and which cases they're not.

                        itemAsString = "\"" + itemAsString + "\"";



                        result.Append(itemAsString + delimiter);

                    }

                }



                result.Remove(--result.Length, 0);

                result.Append(Environment.NewLine);

            }


            using (StreamWriter writer = new StreamWriter(@"C:\log.csv", true))
            {
                writer.Write(result.ToString());
                
            }


            

        }
Example:
DataTable dataTableExportToCSV;
dataTableExportToCSV.ToCSV (",",false);

Description

Export datatable to CSV file

Details

  • Author: John
  • Submitted on: 1/29/2010 3:41:06 PM
  • Language: C#
  • Type: System.Data
  • Views: 7965

Double click on the code to select all.

 

;