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

时间:2014-04-23 13:36:19

标签: c# gridview export-to-excel

从我在Googles到处看到的所有内容来看,这似乎是一个问题。我有一些代码(在下面发布)适用于任何较小的报告,但一旦返回约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,所以我使用会话变量来执行此操作:

    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或更多记录时,只输出一张白纸。

4 个答案:

答案 0 :(得分:1)

尝试在配置文件中设置<httpRuntime maxRequestLength="1048576"/>(或其他一些数字以满足您的需求)。默认情况下,maxRequestLength仅允许4MB的数据。

答案 1 :(得分:0)

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

Edit ASP Settings (IIS 7)的指示

答案 2 :(得分:0)

你被允许使用jQuery吗?正如在VS2010中作为标准包含的那样......?

有一个jQuery插件可以通过将其分成更小的块来上传无限大小的文件。

链接为https://github.com/blueimp/jQuery-File-Upload

据我所知,由于您对第三方代码的使用限制,您无法直接使用此功能,但您仍然可以查看源代码,也可以了解如何使用自己的代码自行完成此操作

答案 3 :(得分:0)

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