Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

even row transposed to columns?

Tags:

c#

excel

vba

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?

like image 226
JohnMunich Avatar asked Nov 28 '25 03:11

JohnMunich


1 Answers

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;
like image 137
Jaroslav Jandek Avatar answered Nov 30 '25 18:11

Jaroslav Jandek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!