导出到Excel错误

时间:2015-05-26 14:26:13

标签: c# excel export-to-excel

我正在尝试通过弹出窗口将数据网格导出到Excel。从应用程序中删除代码时,我能够成功导出到excel,但是,当尝试使用我的应用程序中完全相同的代码导出到Excel时,我收到以下错误:

从我的try / catch:

无法评估表达式,因为代码已优化或本机框架位于调用堆栈之上。

...并从控制台:

错误:Sys.WebForms.PageRequestManagerParserErrorException:无法解析从服务器收到的消息。

我的服务器端代码如下:

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;
        }

    }

2 个答案:

答案 0 :(得分:0)

我将以下内容添加到PageLoad()中,导出现在可以使用:

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

看起来UpdatePanelScriptManager是我忽略的元素。当我将功能与主应用程序分离时,我没有包含主应用程序中的所有HTML。由于ScriptManagerUpdatePanel不是隔离功能的一部分,因此它可以正常工作。我一定会在下次发布HTML

这是以下帖子的解决方案:

Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed

答案 1 :(得分:0)

我建议使用其中一个好的,免费的C#&#34;导出到Excel&#34;那里的图书馆。

您已经编写了代码,以便将您的DataView读入DataTable,并且一旦使用此格式,就可以使用以下免费的C#库...

ExportToExcel library

...您可以将数据导出到真实的&#34; Excel 2007文件在一行代码中....

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

好的,好的,两行代码。我添加了评论。

但你明白了!