Bonjour, je voudrais récupérer une valeur (disons la cellule A1) d'un fichier excel (c:/test.xls) et la mettre dans une variable de mon prog c#.
Comment dois-je faire? (une fois que j'ai récupéré une valeur, le reste ira vite)
merci bcp
Bonjour, je voudrais récupérer une valeur (disons la cellule A1) d'un fichier excel (c:/test.xls) et la mettre dans une variable de mon prog c#.
Comment dois-je faire? (une fois que j'ai récupéré une valeur, le reste ira vite)
merci bcp
Tu peux passer par l'interop ou (beaucoup) plus simplement par ADO OLEDB.
Dans ce dernier cas, tu fabrique ta chaine de connextion ainsi :
et tu "attaques" ta feuille avec du SQL dans le style :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 string _connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=MyExcelFile.Xls;Extended Properties=\"Excel 12.0;HDR={1};IMEX=1\"";
Code : Sélectionner tout - Visualiser dans une fenêtre à part SELECT * FROM [SheetName]
Automation Office :
Y a d'autres méthodes comme l'a bien précisé Bluedeep, en voila une.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 public Microsoft.Office.Interop.Excel.ApplicationClass oExcelApp; public Microsoft.Office.Interop.Excel.Workbooks oBooks; public Microsoft.Office.Interop.Excel.Workbook oBook; public Microsoft.Office.Interop.Excel.Worksheet oSheet; ... string path = "C:\\bd.xls"; // Créer les objets Excel oExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass(); oExcelApp.Visible = false; // Ne pas l'afficher oBooks = oExcelApp.Workbooks; // Ouvrir le fichier Excel désiré object readOnly = true; oBook = oBooks.Open(path, Missing.Value, readOnly, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); // Feuille active oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oExcelApp.ActiveSheet; // Lecture de la cellule A1 string contenu = oSheet.get_Range("A1",Missing.Value).get_Value(Missing.Value).ToString();
J'ai avancé mais je n'y arrive toujours pas!!! J'ai beau chercher partout sur le net je ne trouve pas mon problème.
et il me met l'erreur suivante que je n'arrive pas à résoudre:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Data.OleDb; using System.Data; GridView GridView1 = new GridView(); string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\test.xls;Extended Properties='Excel 8.0;HDR=No;IMEX=1'"; OleDbConnection objConn = new OleDbConnection(connectionString); objConn.Open(); OleDbCommand objCmdSelect = new OleDbCommand("SELECT col1, col2 FROM [aaaaa]", objConn); OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(); objAdapter1.SelectCommand = objCmdSelect; DataSet objDataset1 = new DataSet(); objAdapter1.Fill(objDataset1); objConn.Close(); GridView1.DataSource = objDataset1.Tables[0].DefaultView; GridView1.DataBind();
'System.Windows.Controls.Grid' ne contient pas une définition pour 'DataSource' et aucune méthode d'extension 'DataSource' acceptant un premier argument de type 'System.Windows.Controls.Grid' n'a été trouvée (une directive using ou une référence d'assembly est-elle manquante*?)
Ben, là c'est un problème d'IHM, plus rien à voir avec Excel.
Tu es sur que tu utilises un DataGridView ?
Oui ça sent le coup du pas de DataGridView.
Tu l'as créé en dur, en code ton "GridView1" ?
Essaye-ca, en prenant soin de créer un DataGridView avant
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\test.xls;Extended Properties='Excel 8.0;HDR=No;IMEX=1'"; OleDbConnection objConn = new OleDbConnection(connectionString); objConn.Open(); OleDbCommand objCmdSelect = new OleDbCommand("SELECT col1, col2 FROM [aaaaa]", objConn); OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(); //objAdapter1.SelectCommand = objCmdSelect; DataTable dt = new DataTable(); da.Fill(dt); dataGridView1.AutoGenerateColumns = true; dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; dataGridView1.DataSource = dt;
Partager