简体   繁体   English

导出到Excel错误

[英]Export to Excel Error

I am attempting to export a datagrid to excel via a popup. 我试图通过弹出窗口将datagrid导出为ex​​cel。 I am able to successfully export to excel when removing the code from my application, however, when attempting to export to excel using the exact same code within my application, I receive the following errors: 从应用程序中删除代码后,我能够成功导出到excel,但是,当尝试使用应用程序中完全相同的代码导出到excel时,出现以下错误:

From my try/catch: 从我的尝试/捕获:

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack. 由于代码已优化或本机框架位于调用堆栈的顶部,因此无法评估表达式。

...and from the console: ...并从控制台:

Error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. 错误:Sys.WebForms.PageRequestManagerParserErrorException:无法解析从服务器收到的消息。

My server-side code is as follows: 我的服务器端代码如下:

protected void btnExportToExcel_Click(object sender, EventArgs e)
    {
        ExportDataSetToExcel();
    }

private void ExportDataSetToExcel()
    {

        try
        {
            DataTable dt = new DataTable("GridView_Data");
            foreach (TableCell cell in gvPatientRoster.HeaderRow.Cells)
            {
                dt.Columns.Add(cell.Text);
            }
            foreach (GridViewRow row in gvPatientRoster.Rows)
            {
                dt.Rows.Add();
                for (int i = 0; i < row.Cells.Count; i++)
                {
                    dt.Rows[dt.Rows.Count - 1][i] = row.Cells[i].Text;
                }
            }
            if (dt != null && dt.Rows.Count > 0)
            {
                Response.ContentType = "application/vnd.ms-excel";
                Response.AppendHeader("Content-Disposition",
                    string.Format("attachment; filename=PatientRoster.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 = dt;

                //Report Header
                hw.WriteLine("<b><u><font size='5'>" +
                             "Patient Roster</font></u></b>");
                hw.WriteLine("<br><br>");
                //hw.Write(BuildCriteriaString());
                hw.WriteLine("<br>");
                // Get the HTML for the control.
                dgGrid.HeaderStyle.Font.Bold = true;
                dgGrid.DataBind();
                dgGrid.RenderControl(hw);

                // Write the HTML back to the browser.

                this.EnableViewState = false;
                Response.Write(tw.ToString());
                Response.End();
            }
        }
        catch (Exception ex)
        {
            lblErrorMessage.Text = ex.Message;
        }

    }

I added the following to the PageLoad() and the export now works: 我将以下内容添加到PageLoad()中,导出现在可以使用:

ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
scriptManager.RegisterPostBackControl(this.btnExportToExcel);

It looks like the UpdatePanel and ScriptManager were elements that I overlooked. 看起来UpdatePanelScriptManager是我忽略的元素。 When I separated the functionality from the main application, I did not include all HTML that was in the main application. 当我从主应用程序中分离功能时,我没有包括主应用程序中的所有HTML。 Since the ScriptManager and UpdatePanel were not part of the isolated functionality, it worked properly. 由于ScriptManagerUpdatePanel不是隔离功能的一部分,因此它可以正常工作。 I will be sure to post the HTML next time 我一定会在下次发布HTML

This was a solution from the following post: 这是来自以下帖子的解决方案:

Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed Sys.WebForms.PageRequestManagerParserErrorException:无法解析从服务器收到的消息

I would suggest using one of the many good, free C# "Export to Excel" libraries out there. 我建议您使用其中的许多优质免费C#“导出到Excel”库之一。

You've already written the code to read your DataView into a DataTable , and once it's in this format, using the following free C# library... 您已经编写了将DataView读入DataTable的代码,一旦采用这种格式,就可以使用下面的免费C#库...

ExportToExcel library ExportToExcel库

...you'd be able to export the data into a "real" Excel 2007 file in one line of code.... ...您将可以在一行代码中将数据导出到“真实的” Excel 2007文件中。

//  Export the DataTable into an Excel file, and write it to the web Response
CreateExcelFile.CreateExcelDocument(dt, "SomeFilename.xlsx", Response);

Okay, okay, two lines of code. 好的,好的,两行代码。 I added a comment. 我添加了一条评论。

But you get the point ! 但是你明白了!

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

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