1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
   |         private void button1_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog fileOpen = new System.Windows.Forms.OpenFileDialog())
            {
                fileOpen.Filter = "Fichiers coach|*.xlsx";
                fileOpen.InitialDirectory = @"c:\";
 
                if (fileOpen.ShowDialog() == DialogResult.OK)
                {
 
                    //const string filePath = @"C:\Users\hervanienne\Documents\classeur1.xlsx";
                    var list = new List<object>();
                    var connectionString = "Provider=Microsoft.Ace.OLEDB.12.0; Data Source =" + fileOpen.Filter;
                    connectionString += @"; Extended Properties =""Excel 12.0;HDR=YES;IMEX=1"";";
                    OleDbConnection connection = null;
                    try
                    {
                        using (connection = new OleDbConnection(connectionString))
                        {
                            connection.Open();
                            DataTable dataTable = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                            if (dataTable == null)
                                return;
                            var sheetsNames = from DataRow row in dataTable.Rows select row["TABLE_NAME"].ToString();
                            var cmdText = "SELECT * FROM [" + sheetsNames.FirstOrDefault() + "]";
                            var command = new OleDbCommand(cmdText, connection);
                            var reader = command.ExecuteReader();
                            if (reader == null || !reader.HasRows) return;
                            while (reader.Read())
                            {
                                var myEmploye = new Employe
                                {
                                    Id = reader[0] as string,
                                    Entreprise = reader[1] as string,
                                    Contact = reader[2] as string,
                                    Titre = reader[3] as string,
                                    Adresse = reader[4] as string,
                                    Ville = reader[5] as string,
                                    Region = reader[6] as string,
                                    CodePostal = reader[7] as string,
                                    Pays = reader[8] as string,
                                    Telephone = reader[9] as string,
                                    Telecopie = reader[10] as string,
                                    CA = reader[11] as string,
                                };
                                list.Add(myEmploye);
                            }
                        }
                    }
                    catch (Exception exc)
                    {
                        return;
                    }
                    finally
                    {
                        if (connection != null) connection.Close();
                    }
                }
            }
        } | 
Partager