Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Classic ASP Professional For advanced coder questions in ASP 3. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Professional section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old March 29th, 2005, 11:57 AM
Registered User
 
Join Date: Mar 2005
Location: Orlando, FL, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default problem exporting datagrid to excel

I am trying to send the contents of a datagrid to excel using C#. The code is as follows:

private void exportButton_Click(object sender, System.EventArgs e)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/vnd.ms-excel";
            Response.Charset = "";
            this.EnableViewState = false;
            StringWriter tw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            hw.RenderBeginTag(System.Web.UI.HtmlTextWriterTag. Html);
            this.ClearControls(reportGrid);
            reportGrid.RenderControl(hw);
            hw.RenderEndTag();
            Response.Write(tw);
            Response.End();
         }

        private void ClearControls(Control control)
        {
            for (int i=control.Controls.Count -1; i>=0; i--)
            {
                ClearControls(control.Controls[i]);
            }

            if (!(control is TableCell))
            {
                if (control.GetType().GetProperty("SelectedItem") != null)
                {
                    LiteralControl literal = new LiteralControl();
                    control.Parent.Controls.Add(literal);
                    try
                    {
                        literal.Text = (string)control.GetType().GetProperty("SelectedIte m").GetValue(control,null);
                    }
                    catch
                    {
                    }
                    control.Parent.Controls.Remove(control);
                }
                else
                    if (control.GetType().GetProperty("Text") != null)
                {
                    LiteralControl literal = new LiteralControl();
                    control.Parent.Controls.Add(literal);
                    literal.Text = (string)control.GetType().GetProperty("Text").GetV alue(control,null);
                    control.Parent.Controls.Remove(control);
                }
            }
            return;
        }

When running the code only the first column appears in the spreadsheet. What do I need to do to fix this problem?

Thanks,

KSCDAVE
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in exporting data from datagrid to Excel deb_kareng ASP.NET 2.0 Professional 4 August 4th, 2007 10:21 AM
exporting to excel from datagrid hotshot_21 ASP.NET 1.0 and 1.1 Basics 2 June 9th, 2006 09:53 AM
problem in exporting datagrid to excel swati_joshi ASP.NET 1.0 and 1.1 Professional 3 June 5th, 2006 12:26 AM
Exporting data to excel from datagrid, Problem. swadhinm ASP.NET 1.x and 2.0 Application Design 0 May 31st, 2005 02:44 AM
Exporting DataGrid To Excel chiefg C# 2 December 31st, 2003 12:23 PM



All times are GMT -4. The time now is 02:07 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.