首页 > 代码库 > GridView数据导出excel

GridView数据导出excel

请自行下载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