-2
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(@"" + textBox2.Text + @"\" + filename.TrimStart() + ".csv", true))  

          {

           if (!exists)
           {
             writer.WriteLine(DateTime.Now.ToLongDateString());
             writer.WriteLine("REG.,BR.,BR.NAME,AC TYPE,PRODUCT,NO.OF ACS,ORG.CURRENCY BALANCE,ORG CURRENCY,BALANCE LKR");
             writer.WriteLine(text.Replace("|", ","));
           }
            writer.WriteLine(text.Replace("|", ","));

////true is append parameter. I use this code to create Excel files. I want add new column and fill each cell with auto increment numbers.

pnuts
  • 58,317
  • 11
  • 87
  • 139
dilhan
  • 19
  • 1
  • 7

1 Answers1

0

As you didn't include the appropriate infos I take it that text includes all lines that you want to use and , is being used as the separator instead of the more commonly used ; .

The following splits this complete text into multiple lines and creates an "autoincrement" number that is appended as the last column.

using (System.IO.StreamWriter writer = new System.IO.StreamWriter(@"" + textBox2.Text + @"\" + filename.TrimStart() + ".csv", true))  

{

    if (!exists)
    {
        writer.WriteLine(DateTime.Now.ToLongDateString());
        writer.WriteLine("REG.,BR.,BR.NAME,AC TYPE,PRODUCT,NO.OF ACS,ORG.CURRENCY BALANCE,ORG CURRENCY,BALANCE LKR");
    }

    var textArray = text.Replace("|", ",").split(Environment.NewLine);
    int number = 0;

    foreach (string text in textArray)
    {
        number ++;
        write.WriteLine(text + "," + number.ToString());
    }
Thomas
  • 2,886
  • 3
  • 34
  • 78