(一) . 运行示例图
1. 待导出数据的gridview图:
2. 生成的excel文件
(二). 代码
1. 前台页面 gridviewtoexcelfile.aspx 代码:
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<head runat=”server”>
7 <title>untitled page</title>
8</head>
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 <headerstyle backcolor=”tan” font-bold=”true” />
17 <alternatingrowstyle backcolor=”palegoldenrod” />
18 </asp:gridview>
19 <br />
20 <asp:button id=”button1″ runat=”server” backcolor=”#c0c0ff” onclick=”button1_click”
21 text=”create excel” width=”137px” />
22 </div>
23 </form>
24</body>
25</html>
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<head runat=”server”>
7 <title>untitled page</title>
8</head>
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 <headerstyle backcolor=”tan” font-bold=”true” />
17 <alternatingrowstyle backcolor=”palegoldenrod” />
18 </asp:gridview>
19 <br />
20 <asp:button id=”button1″ runat=”server” backcolor=”#c0c0ff” onclick=”button1_click”
21 text=”create excel” width=”137px” />
22 </div>
23 </form>
24</body>
25</html>
2. 页面后台文件 gridviewtoexcelfile.aspx.cs 代码:
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”] = “pencil”;
72 drnew[“price”] = 7;
73 dt.rows.add(drnew);
74
75 drnew = dt.newrow();
76 drnew[“categoryid”] = 7;
77 drnew[“categoryname”] = “ruler”;
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 button1_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 //生成的excel文件名
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 control)
115 {
116 //base.verifyrenderinginserverform(control);
117 }
118 }
119
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”] = “pencil”;
72 drnew[“price”] = 7;
73 dt.rows.add(drnew);
74
75 drnew = dt.newrow();
76 drnew[“categoryid”] = 7;
77 drnew[“categoryname”] = “ruler”;
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 button1_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 //生成的excel文件名
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 control)
115 {
116 //base.verifyrenderinginserverform(control);
117 }
118 }
119
(三). 示例代码下载
http://www.cnblogs.com/files/chengking/gridviewtoexcel.rar