[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4777
  • Last Modified:

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

0
danielivanov2
Asked:
danielivanov2
  • 4
  • 2
  • 2
1 Solution
 
quizwedgeCommented:
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())
0
 
MMeijerCommented:
try
Response.AddHeader("Content-Disposition", "attachment; filename=oferta.xls");
0
 
danielivanov2Author Commented:
not working either way (Encoding or adding .xls)
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
danielivanov2Author Commented:
the problem appears when I'm trying to export more than 1 gridview. At 1 gridview export, the output is in xls format
0
 
MMeijerCommented:
maybe you're calling Reponse.End() twice?
0
 
danielivanov2Author Commented:
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

0
 
quizwedgeCommented:
Check out the following on generating Excel files with multiple tabs. The Experts Exchange link has other links to follow that may be helpful.

http://www.codingforums.com/archive/index.php/t-154017.html
http://www.codeproject.com/KB/aspnet/Export_to_Excel.aspx
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/ASP/Q_20741732.html
0
 
danielivanov2Author Commented:
I have found the solution in first link:
it was missing "attachment;filename=" from Response Header - thanks~
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now