我有一些將數據從類保存到.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/ –