导出到excel不起作用 [英] Export to excel is not working

查看:223
本文介绍了导出到excel不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将数据列表添加到Custlist中,但每当我想将其保存到excel文件中时,都会收到错误无法评估表达式,因为代码已优化或本机框架位于调用之上堆栈..有什么要写在web.config文件中????





















List< clscustexcel> Custlist = new List< clscustexcel>();

Custlist = obj.Fetch_CustomerManagementForExcel();

if(Custlist.Count> 0)

{

string filename =Customer.xls;

System.IO.StringWriter tw = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

DataGrid dgGrid = new DataGrid();

dgGrid.DataSource = Custlist;

dgGrid.DataBind();



//获取控件的HTML。

dgGrid .RenderControl(hw);

//将HTML写回浏览器。



Response.ContentType =application / vnd.ms -excel;

Response.AppendHea der(内容 - 处置,附件; filename =+ filename +);

this.EnableViewState = false;

Response.Write(tw.ToString());

Response.End();

}

i'm having a list of data into "Custlist" but whenever i want to save it into a excel file, got an error "unable to evaluate expression because the code is optimized or a native frame is on top of the call stack".. is there anything to write in web.config file????










List<clscustexcel> Custlist = new List<clscustexcel>();
Custlist = obj.Fetch_CustomerManagementForExcel();
if (Custlist.Count > 0)
{
string filename = "Customer.xls";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid dgGrid = new DataGrid();
dgGrid.DataSource = Custlist;
dgGrid.DataBind();

//Get the HTML for the control.
dgGrid.RenderControl(hw);
//Write the HTML back to the browser.

Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
this.EnableViewState = false;
Response.Write(tw.ToString());
Response.End();
}

推荐答案

大多数情况下,当您尝试重定向到新页面,而当前页面仍在尝试完成它的过程。您可以在此处找到有关该问题的更多信息:

http://support.microsoft.com/kb/312629/EN- US /

您对
An error like this occurs most of the time when you are trying to redirect to a new page, while the current page is still trying to complete it's process. You can find more information about the issue right here:
http://support.microsoft.com/kb/312629/EN-US/
Your call of
Response.End()

的调用可能会导致threadAbortionException。

添加几行这些可能会有所帮助:

possibly causes a threadAbortionException.
Adding few lines like these may help:

HttpContext.Current.ApplicationInstance.CompleteRequest();
Response.Redirect ("myexamplepage.aspx", false);


几年前我用datatabl做了e,希望能帮到你
some years ago i did it with datatable,hope to help you
public void ToExcel(DataTable dt, string reportName)
    {
        if (dt.Rows.Count > 0)
        {
            string filename = string.Format("{0}.xls", reportName);
            System.IO.StringWriter sw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
            HtmlForm hf = new HtmlForm();

            DataGrid dg = new DataGrid();

            dg.DataSource = dt;
            dg.HeaderStyle.BackColor = Color.Tomato;
            dg.HeaderStyle.ForeColor = Color.White;
            dg.BackColor = Color.LightGoldenrodYellow;
            dg.AlternatingItemStyle.BackColor = Color.PaleGoldenrod;
            dg.Font.Name = "Tahoma";
            dg.Font.Size = 10;
            dg.DataBind();

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");

            HttpContext.Current.Response.Charset = "";
            EnableViewState = false;
            Controls.Add(hf);
            hf.Controls.Add(dg);
            hf.RenderControl(htw);
            HttpContext.Current.Response.Write(sw.ToString());
            HttpContext.Current.Response.End();
        }
    }


这篇关于导出到excel不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆