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,135 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
ID: 39703385
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
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

820 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