locked
Excel download failed file incomplete in chrome RRS feed

  • Question

  • User1310055179 posted

    Hi,

    I run into a problem when I try to export a gridview to an excel file.

    I get an error message in chrome browser: "download failed file incomplete".

    It only happens in chrome.

    I think my problem is with the following code line: Response.AddHeader("Content-Length", (fileContent.Length * 50).ToString());

    When I don't add *50 to the length I only get the table headers, without the records. When I add it I have a problem with the length (maybe there is some kind of limitation in chrome), but it does work on IE.

    This is the code I am using:

    fileName = "ExportToExcel_" + DateTime.Now.ToString() + ".xls";
                            contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"//2003: "application/vnd.ms-excel";
     
                            //ExportDataSetToExcel(gridDataSet);
                            //call 1st export method with fileName and contentType
                            ExportFile(fileName, contentType);
    private void ExportFile(string fileName, string contentType)
            {
                //disable paging to export all data and make sure to bind griddata before begin
                GridView1.AllowPaging = false;
                GridView1.AllowSorting = false;
                GridView1.DataBind();
     
                Response.ClearContent();
                Response.Buffer = true;
                Response.AddHeader("content-disposition"string.Format("attachment; filename={0}",
                fileName));
                Response.ContentType = contentType;
                Response.ContentEncoding = System.Text.Encoding.Unicode;
                Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
     
                StringWriter objSW = new StringWriter();
                HtmlTextWriter objTW = new HtmlTextWriter(objSW);
     
                // Read Style file (css) here and add to response 
                FileInfo fi = new FileInfo(Server.MapPath("~/TableExport.css"));
                StringBuilder sb = new StringBuilder();
                StreamReader sr = fi.OpenText();
                while (sr.Peek() >= 0)
                {
                    sb.Append(sr.ReadLine());
                }
                sr.Close();
     
                GridView1.RenderControl(objTW);
                //Response.Write(objSW);
                string fileContent = "<html><head><style type='text/css'>" + sb.ToString() + "</style></head><body>" + objSW.ToString() + "</body></html>";
                Response.AddHeader("Content-Length", (fileContent.Length * 50).ToString());
                Response.Write(fileContent);
                Response.Flush();
                Response.Close();
                Response.End();
            }

    Monday, December 23, 2019 7:03 AM

All replies

  • User288213138 posted

    Hi qsoft_developer,

    When I don't add *50 to the length I only get the table headers, without the records. When I add it I have a problem with the length (maybe there is some kind of limitation in chrome), but it does work on IE

    GridView1.DataBind();

    Can you tell me why do you re-bind values to the Gridview?

    I delete the GridView1.DataBind (); and then tested the code, It works fine and no need to add * 50 to the length.

    The code:

    <asp:GridView ID="GridView1" runat="server"></asp:GridView>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    
    
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Age") });
                    dt.Rows.Add(1, "name1", "age1");
                    dt.Rows.Add(2, "name2", "age2");
                    dt.Rows.Add(3, "name3", "age3");
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
            protected void Button1_Click(object sender, EventArgs e)
            {
               string fileName = "ExportToExcel_" + DateTime.Now.ToString() + ".xls";
               string  contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; //2003: "application/vnd.ms-excel";
    
                //ExportDataSetToExcel(gridDataSet);
                //call 1st export method with fileName and contentType
                ExportFile(fileName, contentType);
                // ExportGridToExcel();
            }
            private void ExportFile(string fileName, string contentType)
            {
                //disable paging to export all data and make sure to bind griddata before begin
                GridView1.AllowPaging = false;
                GridView1.AllowSorting = false;
                //GridView1.DataBind();
                Response.ClearContent();
                Response.Buffer = true;
                Response.AddHeader("content-disposition", string.Format("attachment; filename={0}",
                fileName));
                Response.ContentType = contentType;
                Response.ContentEncoding = System.Text.Encoding.Unicode;
                Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
    
                StringWriter objSW = new StringWriter();
                HtmlTextWriter objTW = new HtmlTextWriter(objSW);
    
                // Read Style file (css) here and add to response 
                FileInfo fi = new FileInfo(Server.MapPath("~/TableExport.css"));
                StringBuilder sb = new StringBuilder();
                StreamReader sr = fi.OpenText();
                while (sr.Peek() >= 0)
                {
                    sb.Append(sr.ReadLine());
                }
                sr.Close();
    
                GridView1.RenderControl(objTW);
                //Response.Write(objSW);
                string fileContent = "<html><head><style type='text/css'>" + sb.ToString() + "</style></head><body>" + objSW.ToString() + "</body></html>";
                Response.AddHeader("Content-Length", fileContent.ToString());
                Response.Write(fileContent);
                Response.Flush();
                Response.Close();
                Response.End();
            }
    
            public override void VerifyRenderingInServerForm(Control control)
            {
            }

    The result:

    Best regards,

    Sam

    Monday, December 23, 2019 10:31 AM