Link to home
Start Free TrialLog in
Avatar of danielivanov2
danielivanov2Flag for Romania

asked on

Chrome is exporting aspx file instead of xls

I use the code attached to export data in xls. It works fine in any browser, except Chrome, where the file is exported as aspx instead xls. It is something wrong with my code or it's a browser setting that shoud be modified? Thanks
Response.Clear();
                    Response.AddHeader("content-disposition", "oferta");
                    Response.ContentType = "application/vnd.ms-excel";
                    Response.Charset = "";
                    StringWriter stringWrite = new System.IO.StringWriter();
                    HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); 
if (gv.Rows.Count>0)
                    {
                        htmlWrite.Write("<b>Client selectat:</b>");
                        gv.RenderControl(htmlWrite);
                        htmlWrite.WriteBreak();
                    }
string html = stringWrite.ToString();
                    string pattern=@"(\p{Sc})?";
                    System.Text.RegularExpressions.Regex rgx=new System.Text.RegularExpressions.Regex(pattern);
                    html = rgx.Replace(html, "");
Response.Write(html);
HttpContext.Current.Response.End();

Open in new window

Avatar of quizwedge
quizwedge
Flag of United States of America image

Haven't tested the code, but http://my.opera.com/lamhoanghiep/blog/2010/08/05/export-gridview-to-excel might work for you. Looks like they're adding

HttpContext.Current.Response.ContentEncoding = Encoding.Unicode
HttpContext.Current.Response.BinaryWrite(Encoding.Unicode.GetPreamble())
Avatar of MMeijer
MMeijer

try
Response.AddHeader("Content-Disposition", "attachment; filename=oferta.xls");
Avatar of danielivanov2

ASKER

not working either way (Encoding or adding .xls)
the problem appears when I'm trying to export more than 1 gridview. At 1 gridview export, the output is in xls format
maybe you're calling Reponse.End() twice?
nope
i have attached a sample for at least 2 gridviews
Response.Clear();
                    Response.AddHeader("content-disposition", "oferta.xls");
                    Response.ContentType = "application/vnd.ms-excel";
                    Response.Charset = "";

StringWriter stringWrite = new System.IO.StringWriter();
                    HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); 
GridView gv = (GridView)Master.FindControl("SelectionGridView");
                    if (gv.Rows.Count>0)
                    {
                        htmlWrite.Write("<b>Client selectat:</b>");
                        gv.RenderControl(htmlWrite);
                        htmlWrite.WriteBreak();
                    }
                    FormViewCustomerDataMV.RenderControl(htmlWrite);
                    htmlWrite.WriteBreak();
                    if (GridViewStandardOffersVF.Rows.Count > 0)
                    {
                        htmlWrite.Write("<b>Oferta standard voce mobila:</b>");
                        GridViewStandardOffersVF.RenderControl(htmlWrite);
                        htmlWrite.WriteBreak();
                    }
                    if (GridViewStandardOffersVFSMS4All.Rows.Count > 0)
                    {
                        htmlWrite.Write("<b>Oferta standard SMS4All:</b>");
                        GridViewStandardOffersVFSMS4All.RenderControl(htmlWrite);
                        htmlWrite.WriteBreak();
                    }
                    if (GridViewStandardOffersCompet.Rows.Count > 0)
                    {
                        htmlWrite.Write("<b>Oferta competitie:</b>");
                        GridViewStandardOffersCompet.RenderControl(htmlWrite);
                        htmlWrite.WriteBreak();
                    }
                    if (GridViewCustomOffers.Rows.Count > 0)
                    {
                        htmlWrite.Write("<b>Oferta customizata voce mobila:</b>");
                        GridViewCustomOffers.RenderControl(htmlWrite);
                        htmlWrite.WriteBreak();
                    }
                    string html = stringWrite.ToString();
                    string pattern=@"(\p{Sc})?";
                    System.Text.RegularExpressions.Regex rgx=new System.Text.RegularExpressions.Regex(pattern);
                    html = rgx.Replace(html, "");
                    //StringReader reader = new StringReader(html);
                    //Write xls document
                    Response.Write(html);                    
                    //end current Response
                    HttpContext.Current.Response.End();

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of quizwedge
quizwedge
Flag of United States of America 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
I have found the solution in first link:
it was missing "attachment;filename=" from Response Header - thanks~