Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > .NET 1.0 and Visual Studio.NET > VS.NET 2002/2003
| Search | Today's Posts | Mark Forums Read
VS.NET 2002/2003 Discussions about the Visual Studio.NET programming environment, the 2002 (1.0) and 2003 (1.1). ** Please don't post code questions here ** For issues specific to a particular language in .NET, please see the other forum categories.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VS.NET 2002/2003 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
  #1 (permalink)  
Old April 11th, 2006, 06:18 AM
Authorized User
 
Join Date: Apr 2006
Location: london, , United Kingdom.
Posts: 19
Thanks: 3
Thanked 0 Times in 0 Posts
Send a message via MSN to akshay144
Default EXPORT DATAGRID TO EXCEL FORMATTING PROBLEM

Hi all

I am trying to export a Datagrid to Excel and have been successful to a certain extent.

I am using the code below to export a datagrid to excel which works fine except for one thing. I have a Template column which contains a £ sign like this...Totals (£).
When the user selects the option to open the excel spreadsheet it looks like this Totals (£)

Can anyone help?

CODE:-

private void btn_export_Click(object sender, System.EventArgs e)
        {
            //export to excel
            Response.Clear();
            Response.Buffer= true;
            Response.ContentType = "application/vnd.ms-excel";
            Response.Charset = "";
            this.EnableViewState = false;
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            this.ClearControls(this.dgrd_display_MasterChangeL istWIP);
            this.dgrd_display_MasterChangeListWIP.RenderContro l(oHtmlTextWriter);

            Response.Write(oStringWriter.ToString());

            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;
        }
  #2 (permalink)  
Old October 31st, 2006, 12:55 PM
Registered User
 
Join Date: Oct 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
Did you get an answert for this issue. Even i am stuck with this and can't rteally find an answer. Help!
  #3 (permalink)  
Old October 31st, 2006, 01:10 PM
Registered User
 
Join Date: Oct 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Including the line below solved the issue.
Response.ContentEncoding = System.Text.Encoding.Default
Thanks!



Similar Threads
Thread Thread Starter Forum Replies Last Post
export the contents of Datagrid to Excel file softsea VS.NET 2002/2003 0 October 4th, 2006 02:38 PM
Export to Excel Formatting/Hyperlink HockeyMom SQL Server DTS 0 June 5th, 2006 04:04 PM
export datagrid to excel with configured width sn00pyg4rfi3ld ASP.NET 1.x and 2.0 Application Design 0 October 25th, 2005 05:42 AM





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