简体   繁体   English

将数据网格导出到 excel 时出错

[英]error while exporting datagrid to excel

i am trying to export a datagrid to excel. using the code.我正在尝试将数据网格导出到 excel。使用代码。

 Response.Clear();
                    Response.AddHeader("content-disposition", "attachment;filename=Salary_JV_Posting_For_PG_.xls");
                    Response.Charset = "";
                    Response.ContentType = "application/vnd.xls";
                    System.IO.StringWriter stringWrite = new System.IO.StringWriter();
                    stringWrite.Write("&nbsp;&nbsp;<b><font color=#336699 size = 25px><u>test</u></font></b> ");
                    stringWrite.Write(Environment.NewLine);
                    stringWrite.Write(Environment.NewLine);
                    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
                    Response.Flush();
                    dgLine.RenderControl(htmlWrite);
                    Response.Write(stringWrite.ToString());
                    Response.End();

I tried debugging but when the last line (response.end) is executed the following error appears我尝试调试但是当执行最后一行(response.end)时出现以下错误

Data = Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.

I am using AJAX.我正在使用 AJAX。

regards Kumar问候库马尔

Response.End() would absolutely stop the page-lifecycle execution. Response.End()绝对会停止页面生命周期的执行。

Maybe you can try HttpContext.Current.ApplicationInstance.CompleteRequest();也许你可以试试HttpContext.Current.ApplicationInstance.CompleteRequest();

instead of it.而不是它。

Try this buddy试试这个哥们

 Public Sub ExportGridToExcel(ByVal GridView1 As DataGrid, ByVal fileName As String)
        Response.Clear()
        Response.AddHeader("content-disposition", String.Format("attachment;filename={0}.xls", fileName))
        Response.Charset = ""
        Response.ContentType = "application/vnd.xls"
        Dim stringWrite As New StringWriter()
        Dim htmlWrite As New HtmlTextWriter(stringWrite)
        GridView1.RenderControl(htmlWrite)
        Response.Write(stringWrite.ToString())
        Response.Flush()
        Response.[End]()
    End Sub

And call this on button like here然后像这里一样在按钮上调用它

Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
    ExportGridToExcel(DataGrid1, "IT-ADEEL")
End Sub

cheers干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM