码迷,mamicode.com
首页 > 其他好文 > 详细

GridView数据导出excel

时间:2014-12-23 12:14:59      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:

请自行下载org.in2bits.MyXls.dll库

 1 public void GridViewExportExcel(GridView gv) 
 2         {
 3             DataTable dt = new DataTable();
 4             //增加标题行
 5             for (int i = 0; i < gv.Columns.Count; i++)
 6             {
 7                 dt.Columns.Add(gv.Columns[i].HeaderText);
 8             }
 9             DataRow dr = dt.NewRow();
10             //增加数据行
11             for (int i = 0; i < gv.Rows.Count; i++)
12             {
13                 for (int j = 0; j < gv.Rows[i].Cells.Count; j++)
14                 {
15                     dr[j] = gv.Rows[i].Cells[j].Text;
16                 }
17                 dt.Rows.Add(dr);
18             }
19             DataSet toExcelds = new DataSet();
20             toExcelds.Tables.Add(dt);
21             org.in2bits.MyXls.XlsDocument xls = new org.in2bits.MyXls.XlsDocument(toExcelds);
22             this.Page.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}.xls", "Export"));
23             this.Page.Response.Charset = "UTF-8";
24             this.Page.Response.ContentEncoding = System.Text.Encoding.Default;
25             this.Page.Response.ContentType = "application/ms-excel";
26             this.Page.Response.BinaryWrite(xls.Bytes.ByteArray);
27         }

 

GridView数据导出excel

标签:

原文地址:http://www.cnblogs.com/yanJP/p/4179668.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!