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
|
try
{
// On crée une connexion vers le classeur excel
OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+dlg.FileName+";Extended Properties=Excel 8.0;");
// On crée 5 OleDbCommand et 5 OleDbDataReader: un pour chaque colonne de données
// à savoir: P, lambda1, mu1, lambda2, mu2.
OleDbCommand P = new OleDbCommand("select P from [Feuil1$]", myConnection);
OleDbCommand lambda1 = new OleDbCommand("select lambda1 from [Feuil1$]", myConnection);
OleDbCommand mu1 = new OleDbCommand("select mu1 from [Feuil1$]", myConnection);
OleDbCommand lambda2 = new OleDbCommand("select lambda2 from [Feuil1$]", myConnection);
OleDbCommand mu2 = new OleDbCommand("select mu2 from [Feuil1$]", myConnection);
OleDbDataReader reader_P;
OleDbDataReader reader_lambda1;
OleDbDataReader reader_mu1;
OleDbDataReader reader_lambda2;
OleDbDataReader reader_mu2;
// ouvrir la connexion
myConnection.Open();
// lancer les lectures
reader_P = P.ExecuteReader(); // lire la colonne P
reader_lambda1 = lambda1.ExecuteReader(); // lire la colonne lambda1
reader_lambda2 = lambda2.ExecuteReader(); // lire la colonne lambda2
reader_mu1 = mu1.ExecuteReader(); // lire la colonne mu1
reader_mu2 = mu2.ExecuteReader(); // lire la colonne mu2
// afficher les données dans le dataGridView
for (int i = 0; i < numberOfRows; i++){
reader_P.Read();
dataGridView1.Rows[i].Cells[0].Value = reader_P.GetValue(0);
reader_lambda1.Read();
dataGridView1.Rows[i].Cells[1].Value = reader_lambda1.GetValue(0);
reader_mu1.Read();
dataGridView1.Rows[i].Cells[2].Value = reader_mu1.GetValue(0);
reader_lambda2.Read();
dataGridView1.Rows[i].Cells[3].Value = reader_lambda2.GetValue(0);
reader_mu2.Read();
dataGridView1.Rows[i].Cells[4].Value = reader_mu2.GetValue(0);
}
// fermer les reader
reader_P.Close();
reader_lambda1.Close();
reader_lambda2.Close();
reader_mu1.Close();
reader_mu2.Close();
// fermer la connexion
myConnection.Close();
}
catch (OleDbException ex) {} |
Partager