Solved

How to avoid an error (XML page cannot be displayed) when exporting GridView records to an Excel file using ASP.Net 4.0 with VS 2010 in C#?

Posted on 2013-12-07
2
1,096 Views
Last Modified: 2013-12-07
I am creating my first ASP.NET web application.
I try to export a GridView to Excel using the following code but I get the following error once I click on the command button:
------------------------------------------------
The XML page cannot be displayed
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.
--------------------------------------------------------------------------------
The operation completed successfully. Error processing resource 'http://localhost:4380/default.aspx'. Line 6, Position 95

   <td>5455</td><td>10/3/2013</td><td>10/3/2013</td><td>New Rejects from current d...
------------------------------------------------------------
The C# code for my command button to Export the GridView records to Excel is as follows:

 protected void Button1_Click1(object sender, EventArgs e)
        {
            Response.ClearContent();
            Response.AppendHeader("contenet-disposition", "attachment; filename-Clients.xls");
            Response.ContentType = "application/excel";

            StringWriter stringWriter = new StringWriter();
            HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
            GridView1.RenderControl(htmlTextWriter);
            Response.Write(stringWriter.ToString());
            Response.End();
        }

        public override void VerifyRenderingInServerForm(Control control)
        {

        }
0
Comment
Question by:zimmer9
2 Comments
 
LVL 9

Expert Comment

by:QuinnDex
Comment Utility
the error is in your XSL style sheet, check that for correct mapping
0
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 500 total points
Comment Utility
Are you using the code in this link as sample code?

If so, I can suggest two changes to your code (lines 2 and 3 of your button handler): (It fixes the header mispelling and add correct excel type)
Response.AppendHeader("content-disposition", "attachment; filename=Clients.xls");
Response.ContentType = "application/vnd.ms-excel";

Open in new window

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

10 Experts available now in Live!

Get 1:1 Help Now