1

I ran into the error: Cannot get inner content of ExportDiv because the contents are not literal I did a search and got this very useful resource!

I know I used server controls in my aspx page. The page fetches data dynamically from the database. How can I export this data to excel given that there're server controls in my html page.

Here're some of the site.aspx code to the page

    <form id="form1" runat="server">
    <div runat="server" id="ExportDiv">
        <asp:Panel ID="ResultsPanel" runat="server">
        <table cellpadding="0" class="style1">
            <tr>
                <td class="style2">
                    <asp:DetailsView ID="DetailsView1" runat="server" CellPadding="4" 
                    </asp:DetailsView>
                </td>
                <td>
                    Poor</td>
                <td>
                    Good</td>
                <td class="style3">
                    Very Good</td>
                <td>
                    Total Responses</td>
                <td>
                    Average Score</td>
            </tr>
            <tr>
                <td class="style2" colspan="6" bgcolor="#CCCCCC">
                    1. How would you rate the food served to you?</td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp; &nbsp;a.) Overall Quality Taste and Flavour.<br />
                    &nbsp; &nbsp;b.) Variety of Food.</td>
                <td>
                    <asp:Label ID="lblResult0" runat="server" Text="Label"></asp:Label><br/>
                    <asp:Label ID="lblResult3" runat="server" Text="Label"></asp:Label>
                </td>
                <td>
                    <asp:Label ID="lblResult1" runat="server" Text="Label"></asp:Label><br/>
                    <asp:Label ID="lblResult4" runat="server" Text="Label"></asp:Label>
                </td>
                <td class="style3">
                    <asp:Label ID="lblResult2" runat="server" Text="Label"></asp:Label><br/>
                    <asp:Label ID="lblResult5" runat="server" Text="Label"></asp:Label>
                </td>
                <td>
                    <asp:Label ID="aveNum0" runat="server" Text="Label"></asp:Label>
                    <br/>
                    <asp:Label ID="aveNum1" runat="server" Text="Label"></asp:Label>
                    <br/>
                </td>
                <td>
                    <asp:Label ID="aveScore0" runat="server" Text="Label" style="font-weight: 700"></asp:Label>
                    <br />
                    <asp:Label ID="aveScore1" runat="server" Text="Label" style="font-weight: 700"></asp:Label>
                    <br/>
                </td>
            </tr>
           </table>
        </asp:Panel>


    </div>
                    <asp:GridView ID="gvSurveyResult" runat="server">
    </asp:GridView>
    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
    </form>
</body>

And here's also the site.aspx.cs code

    Response.AppendHeader("content-disposition", "attachment;filename=ExportedHtml.xls");
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = "application/vnd.ms-excel";
    this.EnableViewState = false;
    Response.Write(ExportDiv.InnerHtml);
    Response.End();

All answers would be welcomed!

Community
  • 1
  • 1
Guzzyman
  • 561
  • 6
  • 16
  • 37
  • do you use a query string to determine which record of the data you are looking at? if so you could just use HttpWebRequest and HttpWebResponse to get the HTML of the page and then parse this however you like – jgok222 Jan 06 '14 at 17:20

2 Answers2

1

It's been a while since I've done Server Controls, etc., but will this do the trick?

http://harouny.com/2012/10/15/render-asp-net-controls-user-controls-to-html-by-code/

SethMW
  • 1,062
  • 1
  • 9
  • 10
  • This worked perfectly. The control housing the Label Controls and Gridview is the Panel Control. I simply passed the panel ID as parameter and it worked. Oce again, thanks a bunch! – Guzzyman Jan 08 '14 at 13:44
1

I would suggest you to create a function that will convert your data into comma separated values and then render the same as CSV instead of excel format. Both CSV and excel format files would open the same way in excel.

Shashank Chaturvedi
  • 2,756
  • 19
  • 29
  • I want the entire page to be rendered in either csv or excel. The pafge itself is a static page that fetch dynamic content from the database. I have gridview on a seprate page with a show detail link. Once this link is clicked, the page in question fetches the data relating to the link clicked and displays it. I however want to export this page to excel. – Guzzyman Jan 07 '14 at 07:06
  • My understanding of your approach is that: First a page loads that has a link which on clicking brings up another page or loads some data to the same page. Now the newly created page is editable by the user, and on post back you you want to export all the page data (consisting of user inputs as well) to excel. Please correct if I have understood anything wrong, that will help me understand your problem better. – Shashank Chaturvedi Jan 07 '14 at 07:17