(1). Running example Diagram
1. The gridview diagram of the data to be exported:
2. generated Excel File
(2 ).Code
1. gridviewtoexcelfile. aspx code on the front-end page:
1 <% @ page Language = "C #" autoeventwireup = "true" Enableeventvalidation = "false" Codefile = "gridviewtoexcelfile. aspx. cs" inherits = "_ default" %>
2
3 <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5 <HTML xmlns = "http://www.w3.org/1999/xhtml">
6 7 <title> untitled page </title>
8 9 <body>
10 <Form ID = "form1" runat = "server">
11 <div>
12 <asp: gridview id = "gvfoods" runat = "server" backcolor = "lightgoldenrodyellow" bordercolor = "Tan" borderwidth = "1px" cellpadding = "2" forecolor = "black" gridlines = "None "enableviewstate =" true ">
13 <footerstyle backcolor = "Tan"/>
14 <selectedrowstyle backcolor = "darkslateblue" forecolor = "ghostwhite"/>
15 <pagerstyle backcolor = "palegoldenrod" forecolor = "darkslateblue" horizontalalign = "center"/>
16 17 <alternatingrowstyle backcolor = "palegoldenrod"/>
18 </ASP: gridview>
19 <br/>
20 <asp: button id = "button1" runat = "server" backcolor = "# c0c0ff" onclick = "button#click"
21 text = "create excel" width = "137px"/>
22 </div>
23 </form>
24 </body>
25
2. The page Background File gridviewtoexcelfile. aspx. CS code:
1 using system;
2 using system. Data;
3 using system. configuration;
4 using system. Web;
5 using system. Web. Security;
6 using system. Web. UI;
7 using system. Web. UI. webcontrols;
8 using system. Web. UI. webcontrols. webparts;
9 using system. Web. UI. htmlcontrols;
10 using system. IO;
11 using system. text;
12 using system. collections;
13
14 public partial class _ default: system. Web. UI. Page
15 {
16 protected void page_load (Object sender, eventargs E)
17 {
18 if (! Page. ispostback)
19 {
20 createstructure ();
21 This. gvfoods. datasource = This. createdata ();
22 This. gvfoods. databind ();
23}
24}
25 private datatable createstructure ()
26 {
27 datatable dt = new datatable ();
28 DT. Columns. Add (New datacolumn ("categoryid", typeof (INT )));
29 DT. Columns. Add (New datacolumn ("categoryname", typeof (string )));
30 DT. Columns. Add (New datacolumn ("price", typeof (INT )));
31 return DT;
32}
33 public dataset createdata ()
34 {
35 dataset DS = new dataset ();
36 datatable dt = This. createstructure ();
37
38 datarow drnew = DT. newrow ();
39 drnew = DT. newrow ();
40 drnew ["categoryid"] = 1;
41 drnew ["categoryname"] = "apple ";
42 drnew ["price"] = 2;
43 DT. Rows. Add (drnew );
44
45 drnew = DT. newrow ();
46 drnew ["categoryid"] = 2;
47 drnew ["categoryname"] = "banana ";
48 drnew ["price"] = 3;
49 DT. Rows. Add (drnew );
50
51 drnew = DT. newrow ();
52 drnew ["categoryid"] = 3;
53 drnew ["categoryname"] = "orange ";
54 drnew ["price"] = 1;
55 DT. Rows. Add (drnew );
56
57 drnew = DT. newrow ();
58 drnew ["categoryid"] = 4;
59 drnew ["categoryname"] = "radish ";
60 drnew ["price"] = 2;
61 DT. Rows. Add (drnew );
62
63 drnew = DT. newrow ();
64 drnew ["categoryid"] = 5;
65 drnew ["categoryname"] = "pen ";
66 drnew ["price"] = 3;
67 DT. Rows. Add (drnew );
68
69 drnew = DT. newrow ();
70 drnew ["categoryid"] = 6;
71 drnew ["categoryname"] = "penpencil ";
72 drnew ["price"] = 7;
73 DT. Rows. Add (drnew );
74
75 drnew = DT. newrow ();
76 drnew ["categoryid"] = 7;
77 drnew ["categoryname"] = "rsung ";
78 drnew ["price"] = 3;
79 DT. Rows. Add (drnew );
80
81 drnew = DT. newrow ();
82 drnew ["categoryid"] = 8;
83 drnew ["categoryname"] = "eraser ";
84 drnew ["price"] = 5;
85 DT. Rows. Add (drnew );
86
87 Ds. Tables. Add (DT );
88 return Ds;
89}
90 protected void button#click (Object sender, eventargs E)
91 {
92 stringwriter Sw = new stringwriter ();
93 htmltextwriter HTW = new htmltextwriter (SW );
94 this. gvfoods. rendercontrol (HTW );
95 string strhtml = Sw. tostring (). Trim ();
96
97 string excelfilename = "foodlist.xls ";
98 string filephysicialpathname = request. physicalapplicationpath;
99
100 // File Name of the generated Excel File
101 string objectexcelfilename = path. Combine (filephysicialpathname, excelfilename );
102
103 If (file. exists (objectexcelfilename ))
104 {
105 file. Delete (objectexcelfilename );
106}
107 filestream FS = new filestream (objectexcelfilename, filemode. Create );
108 binarywriter BW = new binarywriter (FS, encoding. getencoding ("gb18030 "));
109 BW. Write (strhtml );
110 BW. Close ();
111 FS. Close ();
112
113}
114 Public override void verifyrenderinginserverform (Control)
115 {
116 // base. verifyrenderinginserverform (control );
117}
118}
119
(3) Download Sample Code
Http://files.cnblogs.com/ChengKing/GridViewToExcel.rar