关于gridview导入到excel的过程

来源:互联网 发布:淘宝售前客服不会做 编辑:程序博客网 时间:2024/05/17 08:00

1、    protected void Button1_Click(object sender, EventArgs e)//导成excel的过程,可解决乱码问题
    {
       Response.ClearContent();
       Response.Clear();
      
        Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");

        Response.ContentType = "application/excel";
        Response.Charset = "GB2312";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
     
        HttpContext.Current.Response.Write("<meta   http-equiv=Content-Type   content=text/html;charset=GB2312>");
        string fileName = HttpUtility.UrlEncode( "myexcelfile.xls", Encoding.GetEncoding("GB2312"));
        HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + fileName);


        StringWriter sw = new StringWriter();

        HtmlTextWriter htw = new HtmlTextWriter(sw);

        GridView1.RenderControl(htw);

        Response.Write(sw.ToString());

        Response.End();

 

    }

 


2、    public override void VerifyRenderingInServerForm(Control control) //放在页面,防止 runt=server错误问题的。

    {
        //base.VerifyRenderingInServerForm(control);
    }

 

3、页面要加  EnableEventValidation="false"  

    如<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false"   CodeFile="zj.aspx.cs" Inherits="zj" %>

 

 

原创粉丝点击