Solved

Chrome is exporting aspx file instead of xls

Posted on 2011-03-10
8
4,155 Views
Last Modified: 2012-06-27
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
Comment
Question by:danielivanov2
  • 4
  • 2
  • 2
8 Comments
 
LVL 14

Expert Comment

by:quizwedge
ID: 35105878
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
 
LVL 5

Expert Comment

by:MMeijer
ID: 35106652
try
Response.AddHeader("Content-Disposition", "attachment; filename=oferta.xls");
0
 

Author Comment

by:danielivanov2
ID: 35106764
not working either way (Encoding or adding .xls)
0
 

Author Comment

by:danielivanov2
ID: 35107060
the problem appears when I'm trying to export more than 1 gridview. At 1 gridview export, the output is in xls format
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 5

Expert Comment

by:MMeijer
ID: 35107449
maybe you're calling Reponse.End() twice?
0
 

Author Comment

by:danielivanov2
ID: 35107481
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
 
LVL 14

Accepted Solution

by:
quizwedge earned 250 total points
ID: 35107500
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
 

Author Closing Comment

by:danielivanov2
ID: 35107583
I have found the solution in first link:
it was missing "attachment;filename=" from Response Header - thanks~
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now