gpt4 book ai didi

c# - 甚至行转列?

转载 作者:行者123 更新时间:2023-11-30 18:05:48 28 4
gpt4 key购买 nike

我有一个具有以下结构的 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
...

我想提取所有的偶数行并列在奇数行旁边,当然提取后的空行要去掉,sth。喜欢:

  1. 行/行:aaa 10
  2. 行/行:bbb 3
  3. 行/行:ccc 4

有什么简单的方法吗?

最佳答案

有多种选择,这取决于你想做的额外操作......

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;

关于c# - 甚至行转列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5234906/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com