Link to home
Start Free TrialLog in
Avatar of mannevenu
mannevenu

asked on

to export gridview which is inside update panel to MSExcel

Hello, i have a problem in exporting gridview data to Ecel which is inside update panel. I have used the following code  inside updatepanel  instead am getting an error which can be seen in below screen shot. Plzzz help me out for this problem.

  one more thing the below code is working perfectly in IE 7.0 and mozilla but the problem is when am running it in IE 6.0 what may be the cause for the error.Hope u reply back soon
<Triggers>
<asp:PostBackTrigger ControlID="btnExcelReport"></asp:PostBackTrigger>
</Triggers>

Open in new window

error.bmp
Avatar of Alpha Au
Alpha Au
Flag of Hong Kong image

How you export the data from Girdview to Excel?

I use the following method and with fine in IE6/7 (with update panel and button with
PostBackTrigger)


 
    public static void Export(string fileName, GridView gv)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
        HttpContext.Current.Response.Charset = "";
        HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
        HttpContext.Current.Response.ContentType = "application/ms-excel";
 
        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                //  create a sheet to hold the grid
                Table table = new Table();
                table.GridLines = GridLines.Both;
 
                //  add the header row to the table
                if (gv.HeaderRow != null)
                {
                    table.Rows.Add(gv.HeaderRow);
                }
 
                //  add each of the data rows to the table
                foreach (GridViewRow row in gv.Rows)
                {
                    table.Rows.Add(row);
                }
 
                //  add the footer row to the table
                if (gv.FooterRow != null)
                {
                    table.Rows.Add(gv.FooterRow);
                }
 
                // render the table into the htmlwriter
                table.RenderControl(htw);
 
                // render the htmlwriter into the response
                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.End();
            }
        }
    }

Open in new window

Avatar of mannevenu
mannevenu

ASKER

hi very gud mrng i used the same code what you have given.but it is showing same error what i posted above ?
can you show me how you call the function? have you specify the filename?
ya sure plz go through code which i am attaching

 
public void ExportGridToExcel(GridView GV, string fileName)
    {
        Response.Clear();
        Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", fileName));
        Response.Charset = "";
        Response.ContentType = "application/vnd.xls";
        StringWriter stringWrite = new StringWriter();
        HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        GV.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.Flush();
        Response.End();
 
 
        
    }
    public static void Convert(DataSet ds, HttpResponse Response)
    {
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment; filename=mp.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
        System.Web.UI.WebControls.DataGrid dg = new System.Web.UI.WebControls.DataGrid();
        dg.DataSource = ds.Tables[0];
        dg.DataBind();
        dg.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();
 
 
 
    } 
 
..........................................................................
 
protected void btnExcelReport_Click1(object sender, ImageClickEventArgs e)
    {
 
        {
            if (ddlJoboffer.SelectedItem.Text.Equals("All Jobs"))
            {
                jobdetails();
                Convert(ds, Response);
            }
            else if (ddlJoboffer.SelectedItem.Text.Equals("Particular Job"))
            {
                if (ddlJoboffertitle.SelectedItem.Text.Equals("-Select-"))
                {
                    ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "ClientScript", "{debugger;alert('Please select Job Title');}", true);
                    ScriptManager sm = ScriptManager.GetCurrent(this);
                    sm.SetFocus(ddlJoboffertitle);
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "focus", "$get('" + ddlJoboffertitle.ClientID + "').focus();", true);
                }
                else
                {
                    partcjobdetails();
                    Convert(ds, Response);
                }
            }
            else if (ddlJoboffer.SelectedItem.Text.Equals("By Date"))
            {
                if (txtofferfromdate.Text != "")
                {
                    if (txtoffertodate.Text != "")
                    {
                        DateTime dt1 = DateTime.Parse(txtofferfromdate.Text);
                DateTime dt2 = DateTime.Parse(txtoffertodate.Text);
                if (dt2 > dt1)
                {
                    //imgbtnprint.Visible = true;
                    //btnExcelReport.Visible = true;
                    jobdetailsbydate();
                    gdvofferjob.Visible = true;
                }
                    else if(dt1==dt2)
                {
                    ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "ClientScript", "{debugger; alert('From date & To date cannot be same.No records found between these dates'); }", true);
                    }
                else
                {
                    gdvofferjob.Visible = false;
                    ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "ClientScript", "{debugger; alert('To date cannot be less than From date'); }", true);
                    txtofferfromdate.Text = "";
                    txtoffertodate.Text = "";
                    ScriptManager.GetCurrent(this).SetFocus(txtofferfromdate);
                }
                        jobdetailsbydate();
                        Convert(ds, Response);
                    }
                    else
                    {
                        ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "ClientScript", "{debugger; alert('Please select To Date'); }", true);
                        ScriptManager sm = ScriptManager.GetCurrent(this);
                        sm.SetFocus(txtoffertodate);
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "focus", "$get('" + txtoffertodate.ClientID + "').focus();", true);
                    }
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "ClientScript", "{debugger; alert('Please select From Date'); }", true);
                    ScriptManager sm = ScriptManager.GetCurrent(this);
                    sm.SetFocus(txtofferfromdate);
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "focus", "$get('" + txtofferfromdate.ClientID + "').focus();", true);
 
                }
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "ClientScript", "{debugger; alert('Please select option from Report on Jobs Offered'); }", true);
                ScriptManager sm = ScriptManager.GetCurrent(this);
                sm.SetFocus(ddlJoboffer);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "focus", "$get('" + ddlJoboffer.ClientID + "').focus();", true);
 
            }
        }
 
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Alpha Au
Alpha Au
Flag of Hong Kong image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thank u i got solution