Even rows wrapped in columns?

I have a txt file with the following structure:

1. row/line: aaa
2. row/line: 10
3. row/line: bbb
4. row/line: 3
5. row/line: ccc
6. row/line: 4
...

I want to extract all the even lines and list them next to the odd lines, of course, empty lines should be deleted after extraction, sth. as:

  • line / line: aaa 10
  • line / line: bbb 3
  • line / line: ccc 4

Is there an easy way to do this?

+3
source share
1 answer

There are several options, it depends on what additional operations you want to do ...

int row = 2;
using (StreamReader sr = new StreamReader("data.txt"))
{
    while (sr.Peek() >= 0)
    {
        string c1 = sr.ReadLine();
        string c2 = sr.ReadLine();

        oSheet.Cells[row, 1] = c1;
        oSheet.Cells[row, 2] = c2;

        row++;
    }
}

You can also read the data in a two-dimensional array and insert the range right away:

string[,] cells = new string[numberOfRows, 2];

cells[0, 0] = "Row0 Column0";
cells[0, 1] = "Row0 Column1";

cells[1, 0] = "Row1 Column0";
cells[1, 1] = "Row1 Column1";

//...

oSheet.get_Range("A1", "B8").Value2 = cells;
+1
source

All Articles