?
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
Medium Priority
?
1,183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 9

Expert Comment

by:QuinnDex
ID: 39703385
the error is in your XSL style sheet, check that for correct mapping
0
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 2000 total points
ID: 39703718
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

Implementing Azure Infrastructure Exam 70-533

This course is designed to familiarize and instruct students in the content that is covered by Microsoft Exam 70-533, Implementing Microsoft Azure Solutions. It focuses on all the November 2016 objective domain topics.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

718 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