Solved

Chrome is exporting aspx file instead of xls

Posted on 2011-03-10
8
4,121 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
DLL in ASP.NET 20 45
Editing XSL files, 2 21
Fixed div within Bootstrap carousel item 11 30
Hidden Field Value 10 37
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
Shows how to create a shortcut to site-search Experts Exchange using Google in the Chrome browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch the Search Engine Menu: In chrome, via you…

747 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

11 Experts available now in Live!

Get 1:1 Help Now