当前位置: 代码迷 >> ASP.NET >> 关于GridView导出Excel的有关问题
  详细解决方案

关于GridView导出Excel的有关问题

热度:4127   发布时间:2013-02-25 00:00:00.0
关于GridView导出Excel的问题
gv导出Excel的时候,通过Try..catch捕获到异常:由于代码已经过优化或者本机框架位于调用堆栈之上 无法计算表达式的值
C# code
public static void ToExcelOrWord(Control gvName,string contentType,string fileName){   HttpContext.Current.Response.Charset="GB2312";   HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+HttpContext.Current.Server.UrlEncode(fileName));   HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");   HttpContext.Current.Response.ContentType=contentType;   gvName.Page.EnableViewState=false;   System.Globalization.CultureInfo myCItrad=new System.Globalization.CultureInfo("ZH-CN",true);   StringWriter stringWriter=new StringWriter(myCItrad);   HtmlTextWriter htmlWriter=new HtmlTextWriter(stringWriter);   gvName.RenderControl(htmlWriter);   HttpContext.Current.Response.Write(stringWriter.ToString());   HttpContext.Current.Response.End();//运行到这里就报错,导致在360浏览器下,导出异常}


------解决方案--------------------------------------------------------
Response.End(); 读取速度快,但会产生异常
  

Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
------解决方案--------------------------------------------------------
用这个看看
C# code
        griV.AllowPaging = false; //清除分页        griV.AllowSorting = false; //清除排序           getView(); //你绑定gridview1数据源的那个函数。        HttpContext.Current.Response.Clear();        HttpContext.Current.Response.Buffer = true;        HttpContext.Current.Response.Charset = "GB2312";        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");        HttpContext.Current.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);        //HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls"); //.xls的文件名可修改        HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode("Excel", System.Text.Encoding.UTF8) + ".xls\""); //.xls的文件名可修改        griV.RenderControl(oHtmlTextWriter);                HttpContext.Current.Response.Output.Write(oStringWriter.ToString());        HttpContext.Current.Response.Flush();        HttpContext.Current.Response.End();        griV.AllowSorting = true; //恢复分页        griV.AllowPaging = true; //恢复排序        getView();//重新绑定
------解决方案--------------------------------------------------------
C# code
        Response.Clear();        Response.Buffer = true;        Response.Charset = "GB2312";        Response.AppendHeader("Content-Disposition", "attachment;filename=Customers.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.gvzh.RenderControl(oHtmlTextWriter);        Response.Output.Write(oStringWriter.ToString());        Response.Flush();        Response.End();
------解决方案--------------------------------------------------------
看看这个
http://www.cnblogs.com/dreamof/archive/2008/06/13/1219437
  相关解决方案