0

这是我的代码,用于从我的桌面获取 Microsoft Excel 文件,文件的名称是:testExcel.xls,工作表的名称是:feuil1

提取步骤开始时发生异常

这是我的代码:

string pathToExcelFile = metroTextBoxFichierExcel.Text;
var excelFile = new ExcelQueryFactory(pathToExcelFile);

            excelFile.AddMapping<ViewEtudiantInscription>(x => x.CNE, "CNE");
            excelFile.AddMapping<ViewEtudiantInscription>(x => x.CIN, "CIN");
            excelFile.AddMapping<ViewEtudiantInscription>(x => x.Nom, "Nom");
            excelFile.AddMapping<ViewEtudiantInscription>(x => x.Prenom, "Prenom");
            excelFile.AddMapping<ViewEtudiantInscription>(x => x.Email, "Email");
            excelFile.AddMapping<ViewEtudiantInscription>(x => x.DateNaissance, "DateNaissance");
            excelFile.AddMapping<ViewEtudiantInscription>(x => x.Adresse, "Adresse");
            excelFile.AddMapping<ViewEtudiantInscription>(x => x.Telephone, "Telephone");
            excelFile.AddMapping<ViewEtudiantInscription>(x => x.idNiveau, "IdNiveau");
            excelFile.AddMapping<ViewEtudiantInscription>(x => x.AnneeUni, "AnneeUni");


            //excelFile.TrimSpaces = TrimSpacesType.Both;
            excelFile.ReadOnly = true;

            var etudiants = (from x in excelFile.Worksheet<ViewEtudiantInscription>("Feuil1")
                             select x);

            foreach (var etudiant in etudiants)
            {
                DateTime dd = etudiant.DateNaissance;
                Etudiant etud = new Etudiant
                {
                    Adresse = etudiant.Adresse,
                    CIN = etudiant.CIN,
                    CNE = etudiant.CNE,
                    DateNaissance = dd,
                    Email = etudiant.Email,
                    Nom = etudiant.Nom,
                    Prenom = etudiant.Prenom,
                    statut_etud = 1,
                    Telephone = etudiant.Telephone
                };
                Niveau niv = new Niveau
                {
                    IdNiveau = (int)etudiant.idNiveau
                };
                sm.AjouterEtudiant(etud, niv);
            }
            Actualiser();
        }
4

0 回答 0