Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 29th, 2005, 11:57 AM
Registered User
 
Join Date: Mar 2005
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





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 09:21 AM
exporting to excel from datagrid hotshot_21 ASP.NET 1.0 and 1.1 Basics 2 June 9th, 2006 08:53 AM
problem in exporting datagrid to excel swati_joshi ASP.NET 1.0 and 1.1 Professional 3 June 4th, 2006 11:26 PM
Exporting data to excel from datagrid, Problem. swadhinm ASP.NET 1.x and 2.0 Application Design 0 May 31st, 2005 01:44 AM
Exporting DataGrid To Excel chiefg C# 2 December 31st, 2003 12:23 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.