【问题标题】:Export Excel to SQL - InvalidOperationException将 Excel 导出到 SQL - InvalidOperationException
【发布时间】:2015-01-06 20:37:08
【问题描述】:

我正在按照本教程在线将信息从 SQL 数据库传输到 Excel 工作表。 http://www.c-sharpcorner.com/UploadFile/cd7c2e/export-sql-data-to-excel-using-Asp-Net/

目前我已经在 SQL 数据库中有数据,所以我正在使用第 4 步。在 Graph.aspx 文件上,我创建了一个按钮

<asp:Button ID ="btnSQLToExcel" runat ="server" Text ="Download SQL To Excel" OnClick="btnLoad_Click" />

然后在Graph.aspx.cs文件中我有如下方法点击按钮:

protected void btnLoad_Click(object sender, EventArgs e)
    {
        if(GridView1.Visible)
        {
            Response.AddHeader("content-disposition", "attachment; filename = GridViewToExcel.xls");
            Response.ContentType = "application/excel";
            StringWriter sWriter = new StringWriter();
            HtmlTextWriter hTextWriter = new HtmlTextWriter(sWriter);
            GridView1.RenderControl(hTextWriter);
            Response.Write(sWriter.ToString());
            Response.End();
        }
    }

public override void VerifyRenderingInServerForm(Control control)
    {
    }

如果我取出最后一个方法,它会显示他在他的网站上描述的错误'GridView' 类型的'Control'GridView1' 必须放在带有 runat=server' 的表单标签内。但是通过添加第二种方法,这应该可以处理这个问题,但我得到了以下错误。

System.Web.dll 中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理。

补充信息:RegisterForEventValidation 只能在 Render() 期间调用;

我在这个方面已经超出了我的深度,所以如果有人可以让我知道我做错了什么,或者甚至是一种将 sql 转换为 web 表单中的 excel 的更好方法,我将不胜感激。

【问题讨论】:

    标签: c# asp.net sql-server excel webforms


    【解决方案1】:

    我仍然不知道问题是什么。但是当我通过 Visual Studio 工具创建 SQL Grid View 时,它不起作用。当我在 C# 中手动创建网格视图时,它现在可以工作了。如果有人能告诉我这有什么区别,那将很有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-18
      • 1970-01-01
      • 1970-01-01
      • 2017-11-26
      • 1970-01-01
      • 1970-01-01
      • 2014-07-30
      • 2011-10-22
      相关资源
      最近更新 更多