我正在使用C#創建一個包含200個標題的CSV文件。我從另一個包含150個標題的CSV文件中獲取數據。我的問題是我將如何根據其標題放置數據。例如,我在下面給出例子。在C#中比較和創建CSV文件
將與C#創建的CSV文件:
Name, Surname, Locality, DateOfbirth, Age
Joe, Smith, 60
Sam, Brown, 20
的CSV從
Name, Surname, Age
Joe, Smith, 60
Sam, Brown, 20
獲取數據這是一個示例代碼(實際的文件包含150報頭,並且新的CSV文件包含200個標頭)
string[] lines = System.IO.File.ReadAllLines(fileUrl);
using (System.IO.StreamWriter file = new System.IO.StreamWriter(fileUrl))
{
foreach (string line in lines)
{
if (line == lines[0])
{
//Changing the header of the first file
file.WriteLine("Name, Surname, Locality, DateOfBirth, Age");
}
else
{
string[] values = line.Split(',');
file.WriteLine(string.Format("{0},{1},{2},{3},{4}",
values[0], values[1], values[2], values[3], values[4]));
} //exception being thrown here since the array is out of range
}
}
你正在做'line.Split(',');'但我沒有看到任何逗號'實際的csv文件'? –
我需要將原始文件中的數據保存到新文件中。 @DavidS。上述表格僅用於圖形目的,以便人們不會對數據產生困惑 – chrisd
CSV文件的行在兩個文件中的順序是否相同?你一直都知道結構嗎? – AbstractChaos