title: How can I write a string array to Excel file, with a tab delimiter?
body: <p>I'm creating a small app that reads a tab delimited text file, makes a few changes, and then creates an Excel 2007 .xlsx file. I'm having trouble figuring out how to take the lines from a string array and write them to the Excel file, using the tabs to break the line up into columns. I hope that made sense. </p>
  
  <p>I have <code>string Lines[]</code> that contains something like this:</p>
  
  <pre><code>Item1\tItem2\tItem3\tItem4
  ItemA\tItemB\tItemC\tItemD
  Item5\tItem6\tItem7\tItem8
  </code></pre>
  
  <p>I'd like to create an Excel file that looks like this:</p>
  
  <pre><code>A      B      C      D
  Item1  Item2  Item3  Item4
  ItemA  ItemB  ItemC  ItemD
  Item5  Item6  Item7  Item8
  </code></pre>
  
  <p>I tried the following, but it just puts the first line from <code>Lines[]</code> into each row, and doesn't separate into columns:</p>
  
  <pre><code>string Lines[] = GetLines();
  
  Excel.Application xlApp;
  Excel.Workbook xlWb;
  Excel.Worksheet xlWs;
  object misValue = System.Reflection.Missing.Value;
  
  xlApp = new Excel.Application();
  xlWb = xlApp.Workbooks.Add(misValue);
  xlWs = (Excel.Worksheet)xlWb.Worksheets.get_Item(1);
  
  Excel.Range c1 = (Excel.Range)xlWs.Cells[2, 1];
  Excel.Range c2 = (Excel.Range)xlWs.Cells[2 + lines.Length, 1];
  
  Excel.Range range = xlWs.get_Range(c1, c2);
  
  range.Value = lines;
  range.TextToColumns(                 
      range,
      Microsoft.Office.Interop.Excel.XlTextParsingType.xlDelimited,
      Microsoft.Office.Interop.Excel.XlTextQualifier.xlTextQualifierNone,
      false,
      true    // This is flag to say it is tab delimited
  );
  
  xlApp.Visible = true;
  </code></pre>
  
  <p>Any advice would be appreciated! Thank you!</p>
  
  <p>Here's the output I'm currently getting:</p>
  
  <pre><code>A                           B    C    D
  Item1\tItem2\tItem3\tItem4
  Item1\tItem2\tItem3\tItem4
  Item1\tItem2\tItem3\tItem4
  </code></pre>
  
  <p><strong>EDIT: I've updated my code with @jiverson's suggestion and the line is now separated into columns in Excel, but the first line from <code>Lines[]</code> still appears in every row in Excel. Why?</strong></p>
  
  <p><strong>EDIT #2: Here's the updated working code:</strong></p>
  
  <pre><code>Excel.Application xlApp;
  Excel.Workbook xlWb;
  Excel.Worksheet xlWs;
  object misValue = System.Reflection.Missing.Value;
  
  xlApp = new Excel.Application();
  xlWb = xlApp.Workbooks.Add(misValue);
  xlWs = (Excel.Worksheet)xlWb.Worksheets.get_Item(1);
  
  int currentRow = 2;
  
  string[] lines = GetLines();
  
  for (int i = 0; i &lt; lines.Length; i++)
  {
      string line = lines[i]; //get the current line
  
      string[] values = line.Split('\t'); //split the line at the tabs
  
      //
      // .. i do some things to specific values here ..
      //
  
      lines[i] = String.Join("\t", values); //put the updated line back together
  
      Excel.Range currentRange = (Excel.Range)xlWs.Cells[currentRow, 1]; //get the next row
  
      currentRange.Value = lines[i];  //write the line to Excel
  
      currentRow++;
  }
  
  Excel.Range c1 = (Excel.Range)xlWs.Cells[2, 1]; //get the first cell
  Excel.Range c2 = (Excel.Range)xlWs.Cells[2 + lines.Length, 1]; //get the last cell
  
  Excel.Range range = xlWs.get_Range(c1, c2);  //set the range as the used area
  
  range.TextToColumns( //split the row into columns
      range,
      Excel.XlTextParsingType.xlDelimited,
      Excel.XlTextQualifier.xlTextQualifierNone,
      false,
      true    // This is flag to say it is tab delimited
  );
  </code></pre>