salut à tous,
en cherchant sur le forum je suis tombé sur un sujet en vbnet qui permet d'exporter les données d'un datagridview vers excel http://www.developpez.net/forums/d78...xcel-checkbox/
J'ai essayé d'Adapter ce code en c#,Et là j'ai une erreur au niveau de foreach:
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
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 //Verifions si le datagrid est vid ou non if (dtGridInsertion.ColumnCount == 0){ MessageBox.Show("Aucune données à exporter !", "Erreur !", MessageBoxButtons.OK); } if (dtGridInsertion.RowCount == 0){ MessageBox.Show("Aucune données à exporter !", "Erreur !", MessageBoxButtons.OK); } //Créons un DataSet pour recuperer les données DataSet myDs,myDs2; //On ajoute des tables au ds myDs.Tables.Add();myDs2.Tables.Add(); //On ajoute des colonnes aux tables for (int i = 0; i <= dtGridInsertion.ColumnCount - 1; i++) {myDs.Tables[0].Columns.Add();} //On ajoute des lignes aux tables DataRow myDrw; for (int i = 0; i <= dtGridInsertion.RowCount - 1; i++) { myDrw = myDs.Tables[0].NewRow(); for (int j = 0; j <= dtGridInsertion.ColumnCount - 1; j++) { myDrw[j] = dtGridInsertion.Rows[i].Cells[j].Value; } myDs.Tables[0].Rows.Add(myDrw); } Microsoft.Office.Interop.Excel.ApplicationClass myExcel; Microsoft.Office.Interop.Excel.Workbook wBook; Microsoft.Office.Interop.Excel.Worksheet wSheet; wBook = myExcel.Workbooks.Add(); wSheet = (Microsoft.Office.Interop.Excel.Worksheet)wBook.ActiveSheet; System.Data.DataTable myDt = myDs.Tables[0]; System.Data.DataColumn myDc; System.Data.DataRow myDr; int col = 0; int row = 0; foreach(myDc in myDt.Columns) { col=col+1; if (col==1) myDc.ColumnName="CODE"; if (col==2) myDc.ColumnName="DATE INSERTION"; if (col==3) myDc.ColumnName="ANNONCEUR"; if (col==4) myDc.ColumnName="MARQUE"; if (col==5) myDc.ColumnName="PRODUIT"; if (col==6) myDc.ColumnName="SUPPORT"; if (col==7) myDc.ColumnName="MESSAGE"; if (col==8) myDc.ColumnName="LANGUE"; if (col==9) myDc.ColumnName="ACTION MEDIA"; if (col==10) myDc.ColumnName="DUREE"; if (col==11) myDc.ColumnName="COUT"; myExcel.Cells[1, col] = myDc.ColumnName; } foreach (myDr Dr in myDt.Rows) { row = row + 1; col = 0; foreach (myDc Dc in myDt.Columns) { col = col + 1; myExcel.Cells[row + 1, col] = myDr[myDc.ColumnName]; } } wSheet.Columns.AutoFit(); string myFileName="C:\\export.xls"; Boolean myFileOpen = false; try { System.IO.FileStream fileTemp= System.IO.File.OpenWrite(myFileName); fileTemp.Close(); } catch (Exception myEx) { myFileOpen = false; } if(System.IO.File.Exists(myFileName)) System.IO.File.Delete(myFileName); wBook.SaveAs(myFileName); myExcel.Workbooks.Open(myFileName); myExcel.Visible = true;
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 foreach(myDc in myDt.Columns) {quelqu'un a une idée?Error 2 Type and identifier are both required in a foreach statement ...
Merci d'avance!
Partager