Subject: EXPORT DATAGRID TO EXCEL FORMATTING PROBLEM
Posted By: akshay144 Post Date: 4/11/2006 6:18:25 AM
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_MasterChangeListWIP);
            this.dgrd_display_MasterChangeListWIP.RenderControl(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("SelectedItem").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").GetValue(control,null);
                    control.Parent.Controls.Remove(control);
                }
            }
            return;
        }
Reply By: vellingiri Reply Date: 10/31/2006 11:55:00 AM
Hi,
Did you get an answert for this issue. Even i am stuck with this and can't rteally find an answer. Help!
Reply By: vellingiri Reply Date: 10/31/2006 12:10:16 PM
Including the line below solved the issue.
Response.ContentEncoding = System.Text.Encoding.Default
Thanks!


Go to topic 51639

Return to index page 134
Return to index page 133
Return to index page 132
Return to index page 131
Return to index page 130
Return to index page 129
Return to index page 128
Return to index page 127
Return to index page 126
Return to index page 125