JohnMunich
JohnMunich

Reputation: 631

even row transposed to 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 rows and list them beside the odd rows, of course the empty lines after extraction should be removed, sth. like:

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

Is there any simple way to do it?

Upvotes: 3

Views: 198

Answers (1)

Jaroslav Jandek
Jaroslav Jandek

Reputation: 9563

There are multiple options, it depends 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 to a 2-dimensional array and insert the range at once:

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;

Upvotes: 1

Related Questions