Bonjour,
je developpes une windows application en C# avec visual studio 2005, et j'utilise une DataGridView.
je veux savoir s'il y a une fonction standard qui nous permet d'exporter le dataGridView vers un fichier Excel?
Merci d'avance
Bonjour,
je developpes une windows application en C# avec visual studio 2005, et j'utilise une DataGridView.
je veux savoir s'il y a une fonction standard qui nous permet d'exporter le dataGridView vers un fichier Excel?
Merci d'avance
Un copier / coller ne te suffirait pas ???
Bonjour,
J'ai eu le même probleme que toi il y a 4 jours.
Je n'ai pas trouver de fonction standart néanmoins, on peut faire l'exportation d'une datagrid vers excel.
plusieurs solutions :
La plus simple a vérifier
tu ecrit dans un fichier texte avec des \n et \t pour changer de case dont tu force l'extention en .xls
Marche en C/C++ avec printf
La complexe mais plus "pro" :
Tu utilise des modules de office et excel
Sa n'a pas marcher pour moi
Regarde sur google ou code projet
Soit une solution alternative :
tu transforme en xml et puis et le met dans excel
C'est ce qui ma convenu
Voici mon code qui en bien traffiquer car il ecrit tout en string dans excel
car j'arrive pas encore a distinguer le type du contenu de mes cellules
Bon courage
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133 public static void exportToExcel2(DataGridView source, string fileName) { System.IO.StreamWriter excelDoc; excelDoc = new System.IO.StreamWriter(fileName); const string startExcelXML = "<xml version>\r\n<Workbook " + "xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"\r\n" + " xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\n " + "xmlns:x=\"urn:schemas- microsoft-com:office:" + "excel\"\r\n xmlns:ss=\"urn:schemas-microsoft-com:" + "office:spreadsheet\">\r\n <Styles>\r\n " + "<Style ss:ID=\"Default\" ss:Name=\"Normal\">\r\n " + "<Alignment ss:Vertical=\"Bottom\"/>\r\n <Borders/>" + "\r\n <Font/>\r\n <Interior/>\r\n <NumberFormat/>" + "\r\n <Protection/>\r\n </Style>\r\n " + "<Style ss:ID=\"BoldColumn\">\r\n <Font " + "x:Family=\"Swiss\" ss:Bold=\"1\"/>\r\n </Style>\r\n " + "<Style ss:ID=\"StringLiteral\">\r\n <NumberFormat" + " ss:Format=\"@\"/>\r\n </Style>\r\n <Style " + "ss:ID=\"Decimal\">\r\n <NumberFormat " + "ss:Format=\"0.0000\"/>\r\n </Style>\r\n " + "<Style ss:ID=\"Integer\">\r\n <NumberFormat " + "ss:Format=\"0\"/>\r\n </Style>\r\n <Style " + "ss:ID=\"DateLiteral\">\r\n <NumberFormat " + "ss:Format=\"mm/dd/yyyy;@\"/>\r\n </Style>\r\n " + "</Styles>\r\n "; const string endExcelXML = "</Workbook>"; int rowCount = 0; int sheetCount = 1; excelDoc.Write(startExcelXML); excelDoc.Write("<Worksheet ss:Name=\"Sheet" + sheetCount + "\">"); excelDoc.Write("<Table>"); excelDoc.Write("<Row>"); for (int x = 0; x < source.ColumnCount; x++) { excelDoc.Write("<Cell ss:StyleID=\"BoldColumn\"><Data ss:Type=\"String\">"); excelDoc.Write(source.Columns[x].Name); excelDoc.Write("</Data></Cell>"); } excelDoc.Write("</Row>"); DataGridViewCheckBoxCell maCell = new DataGridViewCheckBoxCell(); for (int x = 0; x < source.RowCount; x++)//int x = 0; x < source.RowCount;x++ { rowCount++; //if the number of rows is > 64000 create a new page to continue output if (rowCount == 64000) { rowCount = 0; sheetCount++; excelDoc.Write("</Table>"); excelDoc.Write(" </Worksheet>"); excelDoc.Write("<Worksheet ss:Name=\"Sheet" + sheetCount + "\">"); excelDoc.Write("<Table>"); } excelDoc.Write("<Row>"); //ID=" + rowCount + " for (int y = 0; y < source.ColumnCount; y++) { System.Type rowType; Object tito = new object(); rowType = source[y, x].ValueType; tito = (System.Type)source[y, x].ValueType; string titi = Convert.ToString(source[y, x].Value); //maCell = (DataGridViewCheckBoxCell)this.dataGridViewItems.Rows[e.RowIndex].Cells[e.ColumnIndex]; string temp = "System.String"; switch (temp)//rowType.ToString() { case "System.String": string XMLstring = Convert.ToString(source[y, x].Value); XMLstring = XMLstring.Trim(); XMLstring = XMLstring.Replace("&", "&"); XMLstring = XMLstring.Replace(">", ">"); XMLstring = XMLstring.Replace("<", "<"); excelDoc.Write("<Cell ss:StyleID=\"StringLiteral\">" + "<Data ss:Type=\"String\">"); excelDoc.Write(XMLstring); excelDoc.Write("</Data></Cell>"); break; case "System.Boolean": excelDoc.Write("<Cell ss:StyleID=\"StringLiteral\">" + "<Data ss:Type=\"String\">"); excelDoc.Write(source[y, x].ToString()); excelDoc.Write("</Data></Cell>"); break; case "System.Int16": case "System.Int32": case "System.Int64": case "System.Byte": excelDoc.Write("<Cell ss:StyleID=\"Integer\">" + "<Data ss:Type=\"Number\">"); excelDoc.Write(source[y, x].ToString()); excelDoc.Write("</Data></Cell>"); break; case "System.Decimal": case "System.Double": excelDoc.Write("<Cell ss:StyleID=\"Decimal\">" + "<Data ss:Type=\"Number\">"); excelDoc.Write(source[y, x].ToString()); excelDoc.Write("</Data></Cell>"); break; case "System.DBNull": excelDoc.Write("<Cell ss:StyleID=\"StringLiteral\">" + "<Data ss:Type=\"String\">"); excelDoc.Write(""); excelDoc.Write("</Data></Cell>"); break; //default: //throw (new Exception(rowType.ToString() + " not handled.")); } } excelDoc.Write("</Row>"); } excelDoc.Write("<Row>"); DateTime Date2 = new DateTime(); Date2 = DateTime.Now; string dateS = Date2.ToString("MM_dd_yyyy_hh_mm"); excelDoc.Write("<Cell ss:StyleID=\"StringLiteral\">" + "<Data ss:Type=\"String\">"); excelDoc.Write(dateS); excelDoc.Write("</Data></Cell>"); excelDoc.Write("</Row>"); excelDoc.Write("</Table>"); excelDoc.Write(" </Worksheet>"); excelDoc.Write(endExcelXML); excelDoc.Close(); }
Vous avez un bloqueur de publicités installé.
Le Club Developpez.com n'affiche que des publicités IT, discrètes et non intrusives.
Afin que nous puissions continuer à vous fournir gratuitement du contenu de qualité, merci de nous soutenir en désactivant votre bloqueur de publicités sur Developpez.com.
Partager