生成CSV文件foreach项并在此列表视图中列出

时间:2016-04-27 10:32:58

标签: c# wpf linq

我正在使用wpf c#解决方案,我想使用LinqToCsv为listview中的每个项目编写一个CSV文件,CSV将由数据库中的字段填充。我没有找到解决方案,现在我的代码生成一个csv文件,包括所有数据,而不是每个项目的数据。 这是我的代码和我的列表视图的屏幕,如果有人可以帮助我! http://imgur.com/HmOmIjN

private void Rechercher_btn(object sender, RoutedEventArgs e)
    {
        TOPSAGEEntities db = new TOPSAGEEntities();

        var query = from fournisseur in db.F_COMPTET
                    join email in db.F_ECHEANCES on fournisseur.CT_Intitule equals email.ECH_Intitule
                    //where email.cbModification.ToString().Contains(DatePicker.Text)
                    select new { fournisseur = email.ECH_Intitule, email = fournisseur.CT_EMail, date = email.cbModification};

        var infoEcheances = from i in db.F_ECHEANCES
                            select new {i.ECH_DateEch, i.CG_Num, i.ECH_Piece, i.ECH_RefPiece, i.ECH_Montant, i.ECH_Libelle};



        CsvFileDescription outpCsvFileDescription = new CsvFileDescription
        {
            SeparatorChar = ',',
            FirstLineHasColumnNames = true
        };

        CsvContext cc = new CsvContext();
        string myPath = @"C:\Users\DefaultAccount\Desktop\Projet Top Of Travel\FichiersCSV\";
        string finalPath = myPath + "test.csv";

        ArrayList myList = new ArrayList(listView.Items);

        foreach (var list in myList)
        {
            cc.Write(infoEcheances, finalPath, outpCsvFileDescription);
        }

        //MessageBox.Show(DatePicker.SelectedDate.ToString());
        listView.DataContext = query.ToList().Distinct();
    }

1 个答案:

答案 0 :(得分:0)

cc圈内移​​动myPathfinalPathforeach

CsvFileDescription outpCsvFileDescription = new CsvFileDescription
{
    SeparatorChar = ',',
    FirstLineHasColumnNames = true
};

ArrayList myList = new ArrayList(listView.Items);
int counter = 1;

foreach (var list in myList)
{
    CsvContext cc = new CsvContext();
    string myPath = @"C:\Users\DefaultAccount\Desktop\Projet Top Of Travel\FichiersCSV\";
    string filename = string.Format("test{0}.csv", counter);
    string finalPath = System.IO.Path.Combine(myPath, filename);

    cc.Write(infoEcheances, finalPath, outpCsvFileDescription);

    counter++;
}