Bonjour tout le monde !
Voilà j'essaie d'exporter ma datagridview sur un fichier excel mais j'ai un problème que j'arrive pas à résoudre
Ce code m'exporte bien ma première colonne dans mon fichier excel lorsque je lui donne l'index de la colonne en dur.
Code C# : 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 foreach (DataGridViewRow uneLigne in dgvPlanningDataGridView.Rows) { oRng = oSheet.get_Range(("A" + (uneLigne.Index + 2).ToString()), ("H" + (uneLigne.Index + 2).ToString())); foreach (DataGridViewColumn uneColonne in dgvPlanningDataGridView.Columns) { try { oSheet.Cells[uneLigne.Index + 2, 1] = uneLigne.Cells[0].Value.ToString().Trim(); } catch (Exception e) { MessageBox.Show(e.ToString()); } col++; } }
Mais des que j'essaie de faire passer l'index de la colonne par une variable pour obtenir toutes les colonnes du dgv et bien j'obtiens une erreur:La référence d'objet n'est pas définie
Code C# : 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 foreach (DataGridViewRow uneLigne in dgvPlanningDataGridView.Rows) { oRng = oSheet.get_Range(("A" + (uneLigne.Index + 2).ToString()), ("H" + (uneLigne.Index + 2).ToString())); foreach (DataGridViewColumn uneColonne in dgvPlanningDataGridView.Columns) { try { oSheet.Cells[uneLigne.Index + 2, col+1] = uneLigne.Cells[col].Value.ToString().Trim(); } catch (Exception e) { MessageBox.Show(e.ToString()); } col++; } }
Pouvez vous m'éclaircir ?
Merci d'avance !
Partager