title: C# Appending data in columns in an existing CSV file from datatable
body: <p>I'm trying to append data to an existing CSV file for use in excel.</p>
  
  <p>The amount of data I'm retrieving is very high so I cant just fill the datatable then write it to a CSV. At the moment I'm filling each column of the table with data, writing that to a file, then clearing the table and restarting.</p>
  
  <p>This works in so far as it adds the data to the CSV in separate columns but it adds the new data under the existing in the next column.</p>
  
  <p>This is how the program writes to the CSV:</p>
  
  <pre><code>  public void Output_CSV()
      {
          StringBuilder sb = new StringBuilder();
  
          foreach (DataRow row in DataTable.Rows)
          {
              IEnumerable&lt;string&gt; fields = row.ItemArray.Select(field =&gt; field.ToString());
              sb.AppendLine(string.Join(",", fields));
          }
  
          File.AppendAllText(outPut, sb.ToString());
  
          sb.Clear();
      }
  </code></pre>
  
  <p>This code adds the all the data in the table, including the blank cells in the previous column, adding the current data in underneath. Is there a way of selecting a column in the table and adding the rows from that column to the CSV. </p>