26 mai 2007
6
26
/05
/mai
/2007
10:20
Se connecter à un classeur Excel 2007 (*.xlsx) via ADO.NET
La chaine de connexion a la forme :
- Provider=Microsoft.ACE.OLEDB.12.0;
- Data Source=chemin complet vers le fichier Excel
- Extended Properties=""Excel 12.0;HDR=YES;"""
private void button2_Click(object sender, EventArgs e) { System.Data.DataTable oDataTable = new DataTable(); System.Data.OleDb.OleDbDataAdapter oOleDbDataAdapter = new System.Data.OleDb.OleDbDataAdapter(); oOleDbDataAdapter.SelectCommand = new System.Data.OleDb.OleDbCommand(); oOleDbDataAdapter.SelectCommand.Connection = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:Documents and SettingsromagnyMes documentsDIVERS.xlsx;Extended Properties=""Excel 12.0;HDR=YES;"""); oOleDbDataAdapter.SelectCommand.CommandType = CommandType.Text; oOleDbDataAdapter.SelectCommand.CommandText = "SELECT [ContactID],[ContactName],[ContactFirstName],[ContactEmail],[ContactCategoryID] FROM [Contact$]"; oOleDbDataAdapter.Fill(oDataTable); dataGridView1.DataSource = oDataTable; } |
http://www.connectionstrings.com/default.aspx?carrier=excel2007