将GridView导出到Excel

 1 None.gif public   static   void  ExportGridView(GridView gridView,  string  filename)
 2 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
 3InBlock.gif        string attachment = "attachment; filename=" + filename + ".xls";
 4InBlock.gif        HttpResponse Response = HttpContext.Current.Response;
 5InBlock.gif        Response.ClearContent();
 6InBlock.gif        Response.ContentEncoding = Encoding.GetEncoding("GB2312");
 7InBlock.gif        Response.AddHeader("content-disposition", attachment);
 8InBlock.gif        Response.ContentType = "application/ms-excel";
 9InBlock.gif        StringWriter sw = new StringWriter();
10InBlock.gif        HtmlTextWriter htw = new HtmlTextWriter(sw);
11InBlock.gif        // Create a form to contain the grid
12InBlock.gif        HtmlForm frm = new HtmlForm();
13InBlock.gif        gridView.Parent.Controls.Add(frm);
14InBlock.gif        frm.Attributes["runat"= "server";
15InBlock.gif        frm.Controls.Add(gridView);
16InBlock.gif        frm.RenderControl(htw);
17InBlock.gif        //GridView1.RenderControl(htw);
18InBlock.gif        Response.Write(sw.ToString());
19InBlock.gif        Response.End();
20ExpandedBlockEnd.gif    }

转载于:https://www.cnblogs.com/eric-huang/archive/2008/05/08/1188123.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值