gridview导出Excel解决方案整理(解决乱码问题)

普通项目导出Excel:

protected void btnToExcel_Click(object sender, EventArgs e)
    {

        this.GridView1.BorderWidth = Unit.Pixel(1);

 

        GridView1.DataSource = 数据源;
        GridView1.DataBind();

        

        if (GridView1.Rows.Count > 0)
        {
            if (GridView1.HeaderRow == null)
            {
                return;
            }

 

            GridView1.HeaderRow.BackColor = System.Drawing.Color.Silver;

            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "GB2312";


            string fileName = HttpUtility.UrlEncode("导出信息") + System.DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss");
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
            //Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            GridView1.AllowPaging = true;
            GridView1.RenderControl(oHtmlTextWriter);


            Response.Write("<meta http-equiv=Content-Type content=text/html;charset=GB2312>");
            Response.Output.Write(oStringWriter.ToString());
            Session.Remove("gvReportResult");
            Response.Flush();
            Response.End();
        }
        else
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "display", "<script language='javascript'>alert('信息为空,不能导出!');</script>");

}

 

 

linq项目导出Excel:

protected void btnToExcel_Click(object sender, EventArgs e)
    {
        this.GridView1.BorderWidth = Unit.Pixel(1);

 

        GridView1.DataSource = 数据源;
        GridView1.DataBind();

 

        if (GridView1.Rows.Count > 0)
        {

                ExportToExcel(GridView1, "导出信息");

        }

         else

         {

                 Page.ClientScript.RegisterStartupScript(Page.GetType(), "display", "<script language='javascript'>alert('信息为空,不能导出!');</script>");

         }

}

 

private static void ExportToExcel(GridView gvw, string title)
    {
        string fileName;
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.ClearHeaders();


        //HttpContext.Current.Response.Charset = "utf-8";
        //HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
        HttpContext.Current.Response.Charset = "GB2312"; //设置了类型为中文防止乱码的出现

 

        fileName = HttpUtility.UrlEncode(title) + string.Format("{0:yyyy-MM-dd-HH-mm}.xls", DateTime.Now);
        HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文

        //HttpContext.Current.Response.AppendHeader("Content-Type", "text/html; charset=gb2312");
        HttpContext.Current.Response.ContentType = "application/ms-excel"; //设置输出文件类型为excel文件。


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


        if (!string.IsNullOrEmpty(title))
        {
            HttpContext.Current.Response.Write("<b><center><font size=3 face=Verdana color=#000000>" + title + "</font></center></b>");
        }


        HttpContext.Current.Response.Write(tw.ToString());
        HttpContext.Current.Response.Flush();
        HttpContext.Current.Response.Close();
        HttpContext.Current.Response.End();

 

        gvw.Dispose();
        tw.Dispose();
        hw.Dispose();

        gvw = null;
        tw = null;
        hw = null;
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值