甚至行转换为列?

时间:2011-03-08 15:56:13

标签: c# excel-vba vba excel

我有一个具有以下结构的txt文件:

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

我想提取所有偶数行并将它们列在奇数行旁边,当然应该删除提取后的空行,某事。像:

  1. 行/行:aaa 10
  2. 行/行:bbb 3
  3. 行/行:ccc 4
  4. 有没有简单的方法呢?

1 个答案:

答案 0 :(得分:1)

有多种选择,它取决于您想要做的其他操作......

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++;
    }
}

您还可以将数据读取到二维数组并立即插入范围:

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;