Solved

Chrome is exporting aspx file instead of xls

Posted on 2011-03-10
8
4,294 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
Technology Partners: 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!

 

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
 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
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…
This Micro Tutorial will demonstrate how to add subdomains to your content reports. This can be very importing in having a site with multiple subdomains.

730 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