Domanda

From everything I've seen everywhere on teh Googles, this appears to be a problem. I have some code (posted below) which works fine for any smaller report, but once there are ~5K or more records returned, it refuses to export.

Anyone got any ideas? Due to corporate restrictions, we can't use any 3rd party tools or add-ins that are not standard in VS2010.

My code:

Just before I bind the datasource to the gridview when I run the report, I fill a session variable with the datasource:

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;

I do this because the user may or may not choose to export it once it's done running. If they choose to export it, it's quicker to use the session variable to re-fill the gridview.

Then, I have a separate function that is responsible for exporting the report. I have to refill the gridview, so I use the session variable to do so:

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

Like I said, this works flawlessly on smaller reports, but exports nothing but a blank sheet when you get around 5K or more records.

È stato utile?

Soluzione

Try setting <httpRuntime maxRequestLength="1048576"/> in your config file (or some other number to accommodate your needs). By default, maxRequestLength only allows for 4MB of data.

Altri suggerimenti

Check the Response buffer limit. The default is only set to 4MB.

Instruction to Edit ASP Settings (IIS 7)

You're allowed to use jQuery right? as that's included as standard in VS2010...?

There is a plugin for jQuery that will upload an unlimited size file, by splitting it into smaller chunks.

The link is https://github.com/blueimp/jQuery-File-Upload

I understand that you can't use this directly due to your limitation on the use of 3rd party code, but you can still look at the source and maybe get some ideas of how to do this yourself with your own code.

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top