我想解析CSV文件的數據沒有運氣,我已經嘗試了一大堆工具在線,沒有人能夠正確解析CSV文件。我感到困惑的是,我在這裏尋求幫助,因爲人們會認爲解析CSV數據會非常容易。解析CSV數據
CSV數據的格式是這樣的:
",95,54070,3635,""Test Reservation"",0,102,0.00,0.00,2014-12-31,""Name of customer"",""$12.34 + $10, special price"",""extra information"",,CustomerName,,,,,1234567890,[email protected],CustomerName,2014-12-31,23:59:59,16,0,60,2,120,0,NULL,NULL,NULL,"
當前代碼:
private void btnOpenFileDialog_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
using (StreamReader reader = new StreamReader(openFileDialog1.FileName))
{
string line;
while ((line = reader.ReadLine()) != null)
{
ParseCsvLine(line);
}
}
}
}
private void ParseCsvLine(string line)
{
if (line != string.Empty)
{
string[] result;
using (var csvParser = new TextFieldParser(new StringReader(line)))
{
csvParser.Delimiters = new string[] { "," };
result = csvParser.ReadFields();
}
foreach (var item in result)
{
Console.WriteLine(item + Environment.NewLine);
}
}
}
結果變量只有一個項目及其:
,95,54070,3635,"Test Reservation",0,102,0.00,0.00,2014-12-31,"Name of customer","$12.34 + $10, special price","extra information",,CustomerName,,,,,1234567890,[email protected],CustomerName,2014-12-31,23:59:59,16,0,60,2,120,0,NULL,NULL,NULL,
通過編寫解析器或使用庫。 – CodeCaster 2014-11-24 11:14:51
那麼,你的分隔符似乎是你的例子中的昏迷,你在分割命令中使用了一個選項卡。 – 2014-11-24 11:16:34