当前位置:编程学习 > C#/ASP.NET >>

求助:用Gridview导出到Excel按钮事件中出现的问题


    //用Gridview导出到Excel按钮事件   
    protected void btnExport_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls"); //这里的FileName.xls可以用变量动态替换
        // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!! 
        Response.ContentEncoding = System.Text.Encoding.UTF7;
        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);
        this.Gridview1.RenderControl(oHtmlTextWriter);
        Response.Output.Write(oStringWriter.ToString());
        Response.Flush();
        Response.End();
    }

    public override void VerifyRenderingInServerForm(Control control)
    {
        //这里什么也不用写
    }



请问以下错误要怎么解决?谢谢!

Microsoft JScript 运行时错误: Sys.WebForms.PageRequestManagerParserErrorException: 无法分析从服务器收到的消息。之所以出现此错误,常见的原因是: 在通过调用 Response.Write() 修改响应时,将启用响应筛选器、HttpModule 或服务器跟踪。
详细信息: 分析附近的“+ADw-div+AD4-
+ADw”时出错。 --------------------编程问答--------------------

//导出方法
        public void Export()
        {
            try
            {
                HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=EnterPrise.xls");
                HttpContext.Current.Response.Charset = "utf-8";
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
                HttpContext.Current.Response.ContentType = "application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword  
                System.IO.StringWriter tw = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
                dgData.RenderControl(hw);
                HttpContext.Current.Response.Write(tw.ToString());
                HttpContext.Current.Response.End();           //会报异常,但能正常导出
                //HttpContext.Current.ApplicationInstance.CompleteRequest();    //垃圾代码,加了把控件都导出来了
            }
            catch (Exception ex) { }
        }

        //重写方法  不重写的话,偶尔会抛异常:datagrid必须放在有ruant=server标记中
        public override void VerifyRenderingInServerForm(Control control)
        {

        }


这个是我用的方法,列表控件导出excel --------------------编程问答-------------------- 这样导出的并不是真正的excel,建议用NPOI
http://www.cnblogs.com/tonyqus/archive/2009/03/16/1409966.html --------------------编程问答-------------------- 采用了1楼的方法,但是还是出现同样的错误,为什么呢?
--------------------编程问答-------------------- 我咋从来没报过这错误啊 --------------------编程问答-------------------- 那请问这个错误该怎么解决啊? --------------------编程问答-------------------- 我也不知道是啥错误,试下NPOI吧
--------------------编程问答-------------------- 试试这个:
http://www.cnblogs.com/insus/articles/1400266.html
相关的:http://www.cnblogs.com/insus/articles/2002489.html --------------------编程问答-------------------- 我也碰到了这问题,后来查了下,原因是用来打印的gridview没有绑定到数据,希望对你有帮助
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,