DataGrid导出Excel

 public void ToExcelsaa(System.Web.UI.WebControls.DataGrid dgExport)
  {
   dgExport.Visible = true;
   dgExport.BorderColor = Color.Black;
   dgExport.HorizontalAlign = HorizontalAlign.Center;
   dgExport.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
   dgExport.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
   dgExport.EditItemStyle.HorizontalAlign = HorizontalAlign.Center;
   HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Report.xls");
   //HttpContext.Current.Response.Charset = "UTF-8" '"gb2312" ' ;
   HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7;
   // HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default
   HttpContext.Current.Response.ContentType = "application/ms-excel";
   dgExport.Page.EnableViewState = false;
   System.IO.StringWriter tw= new System.IO.StringWriter();
   System.Web.UI.HtmlTextWriter hw= new System.Web.UI.HtmlTextWriter(tw);
   dgExport.RenderControl(hw);
   HttpContext.Current.Response.Write(tw.ToString());
   HttpContext.Current.Response.End();
  }

//以下是以前写的vb.net, 把页面内容导出到excel(为整理)

Dim oRpt As ReportDocument = New ReportDocument
Dim msg As New message.Info

        Dim diskOpts As New DiskFileDestinationOptions
        oRpt.ExportOptions.ExportFormatType = ExportFormatType.Excel
        oRpt.ExportOptions.ExportDestinationType = ExportDestinationType.DiskFile
        ''这里我们导出成为.pdf格式文件,你也能选择上面的其它类型文件
        '' 导出成为其它文件时也需要这个选项
        '' 如Microsoft Exchange, MAPI等
        diskOpts.DiskFileName = "c://cc.xls" ''如果你不指定确切的目录,那么文件就会保存到[Windows]/System32目录中去了
        oRpt.ExportOptions.DestinationOptions = diskOpts
        ''水晶报表文件不包含直接的FileName属性,因此你不能直接指定保存的文件名
        ''所以你不得不使用DiskFileDestinationOptions对象,设置它的DiskFileName属性
        ''为你想要的路径,最后将水晶报表的DestinationsOptions属性指定为上面的DiskFileDestinationOption
        oRpt.Export()

        msg.getMsg("c://" & Trim(DpDealer.SelectedItem.Text) & ".xls文件导出成功!")
        Response.ClearContent()
        Response.ClearHeaders()

        Response.WriteFile("c://cc.xls")
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7
        HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=bb.xls")
        HttpContext.Current.Response.ContentType = "application/ms-excel"

        Response.Flush()
        Response.Close()
        System.IO.File.Delete("c://cc.xls")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值