title: How to Concatenate an Excel File Using a DataTable?
body: <p>I have a DataTable being generated using the C# NI DAQmx code. I want to take this DataTable and put it in an excel file when a CheckBox is checked. The DAQmx code records this data 'x' number of samples at a time. When this number is high, the program is slow, but it still works. I want to record a low number of samples at a time, and then save that data into an excel file.</p>
  
  <p>In my current code, the data in the excel file is constantly overwritten. This is not desirable, as I need all recorded data. </p>
  
  <p>Currently the data will actively record when the box is checked, but it will not concatenate. I have tried many searches and explored many methods for this, but I haven't quite been able to adapt anything for my needs. </p>
  
  <p>Relevant code will be included below. Any help is appreciated, thanks.</p>
  
  <p>Note: Data does not have to be a .xlsx file. It can be a .csv</p>
  
  <p>This code is the DataTable generation via DAQmx:</p>
  
  <pre><code>    private void DataToDataTable(AnalogWaveform&lt;double&gt;[] sourceArray, ref DataTable dataTable)
      {
          // Iterate over channels
          int currentLineIndex = 0;
          string test = currentLineIndex.ToString();
  
          foreach (AnalogWaveform&lt;double&gt; waveform in sourceArray)
          {
              for (int sample = 0; sample &lt; waveform.Samples.Count; ++sample)
              {
                  if (sample == 50)
                      break;
  
                  dataTable.Rows[sample][currentLineIndex] = waveform.Samples[sample].Value;
              }
              currentLineIndex++;       
          }
      }
  
      public void InitializeDataTable(AIChannelCollection channelCollection, ref DataTable data)
      {
          int numOfChannels = channelCollection.Count;
          data.Rows.Clear();
          data.Columns.Clear();
          dataColumn = new DataColumn[numOfChannels];
          int numOfRows = 50;
  
          for (int currentChannelIndex = 0; currentChannelIndex &lt; numOfChannels; currentChannelIndex++)
          {
              dataColumn[currentChannelIndex] = new DataColumn()
              {
                  DataType = typeof(double),
                  ColumnName = channelCollection[currentChannelIndex].PhysicalName
              };
          }
  
          data.Columns.AddRange(dataColumn);
  
          for (int currentDataIndex = 0; currentDataIndex &lt; numOfRows ; currentDataIndex++) 
          {
              object[] rowArr = new object[numOfChannels];
              data.Rows.Add(rowArr);
          }
      }
  </code></pre>
  
  <p>This is my current method of saving to an Excel file:</p>
  
  <pre><code>        private void Excel_cap_CheckedChanged(object sender, EventArgs e)
      {
  
                     int i = 0;
          for (excel_cap.Checked = true; excel_cap.Checked == true; i ++) {
  
              {
                  StringBuilder sb = new StringBuilder();
  
                  IEnumerable&lt;string&gt; columnNames = dataTable.Columns.Cast&lt;DataColumn&gt;().
                                                    Select(column =&gt; column.ColumnName);
                  sb.AppendLine(string.Join(",", columnNames));
  
                  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(filename_box.Text, sb.ToString());
  
              }
          }
      }
  </code></pre>