gridview导出excel乱码

网上找了一个方法:

 

 

解决导出execl出现乱码的问题

  1   public   void  OutPutExcel()
 
2    {
 
3      // 定义文档类型、字符编码
  4     Response.Clear();
 
5            Response.Buffer =   true ;
 
6            Response.Charset = " GB2312 " ;
 
7      string  Name = ReprotName(Request.QueryString[ " id " ],HotelID);
 
8  
 
9      // 下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开
10             // filename=FileReport.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc    .xls    .txt   .htm
11      // System.Text.Encoding.GetEncoding("utf-8").GetEncoder
12   // 解决了导出文件名乱码的问题
13            Response.AppendHeader( " Content-Disposition " , " attachment;filename= " + System.Web.HttpUtility.UrlEncode(Name,System.Text.Encoding.UTF8) + " .xls " );
14            Response.ContentEncoding = System.Text.Encoding.GetEncoding( " utf-8 " );
15             // Response.ContentType指定文件类型可以为application/ms-excel、application/ms-word、application/ms-txt、 application/ms-html 或其他浏览器可直接支持文档
16            Response.ContentType  =   " application/ms-excel " ;
17            this .plContainer.EnableViewState  =   false ;
18      //  定义一个输入流
19  
20      System.IO.StringWriter oStringWriter  =   new  System.IO.StringWriter();
21      System.Web.UI.HtmlTextWriter oHtmlTextWriter  =   new  System.Web.UI.HtmlTextWriter(oStringWriter);
22     
23     plContainer.RenderControl(oHtmlTextWriter);
24     
25             // this 表示输出本页,你也可以绑定datagrid,或其他支持obj.RenderControl()属性的控件
26            Response.Write(oStringWriter.ToString());
27            Response.End();
28    }

 

引用方法        

       // 导出数据
                ExportToExcel(this.Page, GridViewExport);

 

        /// <summary>
        /// 内容摘要:将数据导出到Excel
        /// </summary>
        /// <param name="page"></param>
        /// <param name="dgExcel"></param>
        private void ExportToExcel(System.Web.UI.Page page, GridView dgExcel)
        {
            string fileName = "OutData.xls";

            try
            {
                foreach (GridViewRow row in dgExcel.Rows)
                {
                    TableCell cell = null;
                    if (row.Cells.Count >= 2)
                    {
                        for (int i = 0; i < 2; i++)
                        {
                            cell = row.Cells[i];
                            cell.Style.Add("vnd.ms-excel.numberformat", "@");
                        } // end for
                    } // end if
                } // end foreach

                page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);

                Response.Charset = "GB2312";
                Response.ContentType = "application/ms-excel";

                //解决出现乱码关键问题
                Response.Write("<meta http-equiv=Content-Type content=text/html;charset=GB2312>");

                dgExcel.Page.EnableViewState = false;
                dgExcel.Visible = true;
                dgExcel.HeaderStyle.Reset();
                dgExcel.AlternatingRowStyle.Reset();

                System.IO.StringWriter tw = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(tw);
                dgExcel.RenderControl(hw);

                //5.输出DataGrid内容
                Response.Write(tw.ToString());
                Response.End();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }// end try catch

 

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值