Bonjour,
je n'arrive pas a ouvrir un fichier excel en VB, la procedure est la suivante : cliquer sur un bouton pour selectionner le fichier excel, choisir le fichier puis le programme doit l'ouvrir et le charger dans un dataGridView.
le code est ci-dessous
et voici l'interface graphique avec les messages de sortie
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
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using Excel = Microsoft.Office.Interop.Excel; using System.Data.OleDb; using System.Windows.Forms; namespace Support_Exploitation_Manager { public partial class Form1 : Form { //Classe lecture Excel selon la version public DataTable ReadExcel(string fileName, string fileExt) { string conn = string.Empty; DataTable dtexcel = new DataTable(); if (fileExt.CompareTo(".xls") == 0) conn = @"provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Extended Properties='Excel 8.0;HRD=Yes;IMEX=1';"; //Version interieur excel 2007 else conn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties='Excel 12.0;HDR=NO';"; //Version supperieur excel 2007 using (OleDbConnection con = new OleDbConnection(conn)) { try { OleDbDataAdapter oleAdpt = new OleDbDataAdapter("select * from [Sheet1$]", con); //Lecture de la feuille 1 oleAdpt.Fill(dtexcel); //Fichier Excel enregistrée dans la variable } catch { } } return dtexcel; } //Fin de la classe public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { textBox1.Text = "ok"; //Ouvrir fichier string filePath = string.Empty; string fileExt = string.Empty; OpenFileDialog file = new OpenFileDialog(); //Boite de dialogue ouverte if (file.ShowDialog() == System.Windows.Forms.DialogResult.OK) //choix du fichier { filePath = file.FileName; //Chemain d'accées au fichier Excel fileExt = Path.GetExtension(filePath); //Recupérer l'extension du fichier if (fileExt.CompareTo(".xls") == 0 || fileExt.CompareTo(".xlsx") == 0) { try { DataTable dtExcel = new DataTable(); dtExcel = ReadExcel(filePath, fileExt); //Lecture du fichier Excel textBox1.Text = filePath; dataGridView1.DataSource = dtExcel; dataGridView1.Refresh(); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } else { MessageBox.Show("Veuillez choisir l'extension .xls ou .xlsx", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Error); //Message d'erreur type de fichier } } //Fin ouverture Fichier } private void Teste_Click(object sender, EventArgs e) { this.Close(); } private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { } } }
Merci
Partager