我有一些将数据从类保存到.csv文件的代码,但我不确定如何将它读回到类中,以便将其放入列表视图中。下面是该代码的保存:将.csv数据导入类
SaveFileDialog save = new SaveFileDialog();
save.Filter = "Excel|*.csv";
if (save.ShowDialog() == DialogResult.OK)
{
StreamWriter sw = new StreamWriter(save.FileName);
try
{
sw.WriteLine("Name" + ";" + "Authors" + ";" + "Pages" + ";" + "Date" + ";" + "Price" + ";" + "Copies");
foreach (Book b in bookList)
{
string aux = "";
aux = string.Join(";", b.Authors);//I know I will probably need to change ';' here because it will have trouble reading it
sw.WriteLine(b.Name + ";" + aux + ";" + b.Pages + ";" + b.Date.ToString("dd.MM.yyyy") + ";" + b.Price + ";" + b.Copies);
}
}
catch (IOException ert)
{
MessageBox.Show(ert.Message);
}
catch (Exception ew)
{
MessageBox.Show(ew.Message);
}
finally
{
sw.Close();
}
}
你忘了'c#-1.2'标签。该死的你5标签限制。 –
有免费的库可以在两个方向上做到这一点。 – Magus
尝试FileHelpers。这种东西非常有用。 http://filehelpers.sourceforge.net/ –