当有超过5K行时,将C#报告导出到Excel

当有超过5K行时,将C#报告导出到Excel,c#,gridview,export-to-excel,C#,Gridview,Export To Excel,从我在谷歌上看到的一切来看,这似乎是个问题。我有一些代码(张贴在下面),对于任何较小的报告都可以正常工作,但一旦返回了~5K或更多记录,它就会拒绝导出 有人有什么想法吗?由于公司限制,我们不能使用VS2010中不标准的任何第三方工具或加载项 我的代码: 在运行报表时将数据源绑定到gridview之前,我将使用数据源填充一个会话变量: var adapter = new SqlDataAdapter(cmd2); var ds = new DataSet(); adapter.Fill(ds, "

从我在谷歌上看到的一切来看,这似乎是个问题。我有一些代码(张贴在下面),对于任何较小的报告都可以正常工作,但一旦返回了~5K或更多记录,它就会拒绝导出


有人有什么想法吗?由于公司限制,我们不能使用VS2010中不标准的任何第三方工具或加载项

我的代码:

在运行报表时将数据源绑定到gridview之前,我将使用数据源填充一个会话变量:

var adapter = new SqlDataAdapter(cmd2);
var ds = new DataSet();
adapter.Fill(ds, "MyTableName");

// Add this to a session variable so the datagrid won't get NULLed out on repost
Session["SSRptMenu"] = ds;
我这样做是因为用户可能会选择,也可能不会选择在运行完成后将其导出。如果他们选择导出它,使用会话变量重新填充gridview会更快

然后,我有一个单独的功能,负责导出报告。我必须重新填充gridview,因此我使用session变量:

    private void ExportGridView()
    {
        // Exports the data in the GridView to Excel
        // First, fill the datagrid with the results of the session variable
        DataSet gridDataSource = (DataSet)Session["SSRptMenu"];

        GridView_Reports.Visible = true;
        GridView_Reports.DataSource = gridDataSource;
        GridView_Reports.DataBind();

        // Exports the data in the GridView to Excel
        string attachment = "attachment; filename=RingMaster.xls";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        GridView_Reports.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
    }

正如我所说的,这在较小的报告上可以完美地工作,但是当您得到大约5K或更多记录时,只会导出一张空白表。

检查响应缓冲区限制。默认值仅设置为4MB


请尝试在配置文件中设置
(或满足您需要的其他数字)。默认情况下,只允许4MB的数据

您可以使用jQuery,对吗?作为VS2010中的标准

jQuery有一个插件,它可以通过将文件分成更小的块来上传一个大小不限的文件

链接是


我知道,由于您对使用第三方代码的限制,您不能直接使用它,但您仍然可以查看源代码,或许可以了解如何使用自己的代码实现这一点。

那么,导出后RingMaster.xls的内容是什么样子的?“由于公司限制,我们不能使用VS2010中不标准的任何第三方工具或加载项“我觉得这种态度完全令人迷惑。有多少公司正在把巨额资金投入厕所,翻新老旧的道路?对于许多.Net库的开源,这将如何处理?您是否收到404错误代码?如果是,子状态代码是什么?如果报告中有>5K条记录,则导出后RingMaster.xls为空。没有404或其他错误,只是一个空的导出文件。我假设它与会话变量的容量有关?我不知道。或多或少,如果你自己还没有解决这个问题,我打赌你不会有答案。这似乎是一个比较常见的问题,但我还没有找到一个可行的解决方案。一般来说:在会话中存储大数据集是一个好主意吗?我不这么认为。和reggarding导出,若你们面临IIS缓冲区大小的限制,你们可以将excel导出到临时文件,然后从文件中发送。你们真是个神!!赏金在21小时内不能颁发,但它是你的。很高兴我能帮上忙!我知道有多少次我为这样的小事而烦恼。
Please try the code after HttpContext.Current.Response.Write(tw.ToString());  
and place your HttpContext.Current.Response.End(); after the catch.

#region "   Summary - Excel Upload                      "
        private void fnExcelUpload()
        {
            try
            {
                dgDashboard.AllowPaging = false;
                dgDashboard.Columns[0].Visible = false;
                Response.ContentType = "application/vnd.ms-excel";
                Response.AddHeader("content-disposition", "attachment; filename = ExcelExport.xls");
                Response.Charset = "";
                Response.Buffer = true;
                this.EnableViewState = false;
                StringWriter tw = new StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(tw);
                fillDashboard();
                dgDashboard.RenderControl(hw);
                HttpContext.Current.Response.Write(tw.ToString());
                HttpContext.Current.ApplicationInstance.CompleteRequest();
                HttpContext.Current.Response.Flush();
                //HttpContext.Current.Response.End();
            }
            catch (Exception Ex)
            {
                ErrorLog obj = new ErrorLog(Session["PROGRAMCODE"].ToString(), Ex.Message, Ex.StackTrace, this.Page.ToString(), new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name, System.Net.Dns.GetHostEntry(Context.Request.ServerVariables["REMOTE_HOST"]).HostName.ToString(), Session["EMPNUMBER"].ToString(), HttpContext.Current.User.Identity.Name.ToString());
            }
            HttpContext.Current.Response.End();
        }

        protected void imgExcelExport_Click(object sender, ImageClickEventArgs e)
        {
            fnExcelUpload();
        }
        #endregion  

#region "   Summary - Excel Upload                      "
        private void fnExcelUpload()
        {
            try
            {
                dgDashboard.AllowPaging = false;
                dgDashboard.Columns[0].Visible = false;
                Response.ContentType = "application/vnd.ms-excel";
                Response.AddHeader("content-disposition", "attachment; filename = ExcelExport.xls");
                Response.Charset = "";
                Response.Buffer = true;
                this.EnableViewState = false;
                StringWriter tw = new StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(tw);
                fillDashboard();
                dgDashboard.RenderControl(hw);
                HttpContext.Current.Response.Write(tw.ToString());
                HttpContext.Current.ApplicationInstance.CompleteRequest();
                HttpContext.Current.Response.Flush();
                //HttpContext.Current.Response.End();
            }
            catch (Exception Ex)
            {
                ErrorLog obj = new ErrorLog(Session["PROGRAMCODE"].ToString(), Ex.Message, Ex.StackTrace, this.Page.ToString(), new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name, System.Net.Dns.GetHostEntry(Context.Request.ServerVariables["REMOTE_HOST"]).HostName.ToString(), Session["EMPNUMBER"].ToString(), HttpContext.Current.User.Identity.Name.ToString());
            }
            HttpContext.Current.Response.End();
        }

        protected void imgExcelExport_Click(object sender, ImageClickEventArgs e)
        {
            fnExcelUpload();
        }
        #endregion