Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7129573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:15:25+00:00 2026-05-28T11:15:25+00:00

Basically I have a webcontrol that contains a gridview with an export button. When

  • 0

Basically I have a webcontrol that contains a gridview with an export button. When this export button is clicked I want to basically convert the gridview into an .xls file so I can view the data in excel.

I’ve actually managed to get everything to work using GridView.RenderControl(); The problem is that the entire usercontrol’s data seems to be saved to this excel file (including the button/images/headings etc). This isn’t want I want. I only want to render the GridView data and possibly the heading.

Is there any way I can choose what information gets rendered? It seems bizarre that calling a function on a single control causes all the controls to be rendered…

Anyway here is my export button code:

 protected void btnExport_Click(object sender, EventArgs e)
        {
            try
            {
                // Exports as excel spreadsheet
                Response.Clear();
                Response.Buffer = true;

                Response.AddHeader("content-disposition",
                "attachment;filename=registered_subscribers_" + System.DateTime.Now.ToShortDateString().Replace("/", "") + ".xls");
                Response.Charset = "";
                Response.ContentType = "application/vnd.ms-excel";
                StringWriter sw = new StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(sw);                

                for (int i = 0; i < gvInterest.Rows.Count; i++)
                {
                    GridViewRow row = gvInterest.Rows[i];

                    //Apply text style to each Row
                    row.Attributes.Add("class", "textmode");
                }

                gvInterest.RenderControl(hw);

                //style to format numbers to string
                string style = @"<style> .textmode { mso-number-format:\@; } </style>";
                Response.Write(style);
                Response.Output.Write(sw.ToString());
                Response.Flush();
                Response.End();
            }
            catch
            {
                // Handle error
            }
        }

and here’s my front end:

 <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ViewSubscribers.ascx.cs"
        Inherits="MyCode.ViewSubscribers" %>
    <div id="subscribers">
        <asp:Button ID="btnExport" runat="server" Text="Export" 
            onclick="btnExport_Click" />    
        <h2>
            Pre-registered subscribers for tickets</h2>
        <asp:GridView ID="gvInterest" runat="server" AutoGenerateColumns="false">
            <HeaderStyle CssClass="gv-header" ForeColor="#ffffff" BackColor="#333333" />
            <RowStyle BackColor="White" ForeColor="#333333" />
            <AlternatingRowStyle BackColor="#e6e6e6" ForeColor="#333333" />
            <Columns>
                <asp:BoundField DataField="Title" HeaderText="Title" />
                <asp:BoundField DataField="FirstName" HeaderText="First Name" />
                <asp:BoundField DataField="LastName" HeaderText="Last Name" />
                <asp:BoundField DataField="HouseNumber" HeaderText="House Name/Number" />
                <asp:BoundField DataField="Address1" HeaderText="Address 1" />
                <asp:BoundField DataField="Address2" HeaderText="Address 2" />
                <asp:BoundField DataField="Postcode" HeaderText="Post code" />
                <asp:BoundField DataField="Country" HeaderText="Country" />
                <asp:BoundField DataField="Phone" HeaderText="Phone" />
                <asp:BoundField DataField="Email" HeaderText="Email" />
                <asp:BoundField DataField="Comments" HeaderText="Comments" />
                <asp:BoundField DataField="DateCreated" HeaderText="Date Created" />
            </Columns>
            <EmptyDataTemplate>
                There are currently no subscribers
            </EmptyDataTemplate>
        </asp:GridView>      
    </div>

Anyone able to help?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T11:15:25+00:00Added an answer on May 28, 2026 at 11:15 am

    Check this post here http://www.c-sharpcorner.com/UploadFile/DipalChoksi/ExportASPNetDataGridToExcel11222005041447AM/ExportASPNetDataGridToExcel.aspx

    You can use clearcontrols function to remove any html tags/controls/images etc before rendering them

    Another link – http://csharpdotnetfreak.blogspot.com/2011/10/export-gridview-to-excel.html

    check changecontroltovalue function

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I basically have three tables, posts, images and postimages (this simply contains the ids
Basically I have a gridview that is loaded when a DataSet is returned from
I basically have an entity that gets bound to my gridview and has a
I basically have a form that you input chat messages into. It looks like
I basically have something like this: void Foo(Type ty) { var result = serializer.Deserialize<ty>(inputContent);
I basically have 7 select statements that I need to have the results output
basically have two questions. 1. Is there a c++ library that would do full
I basically have this piece of code. char (* text)[1][80]; text = calloc(2821522,80); The
I basically have the same problem in this questions: Flash Video still playing in
I basically have a div on my site that always has the same stuff.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.