0

Hi I am trying to export selected data using CheckBoxes from GridView, when I export it only shows .textmode { mso-number-format:@; } in the excel document. Did I miss something or is the C# wrong ? Any help would be amazing.

I have also attached an image of the excel document so you can understand what I'm receiving on the export better than my explanation. Excel Export

Here is the C# for the export button

protected void btnExportExcel_Click(object sender, EventArgs e)

    {

        Response.Clear();

        Response.Buffer = true;



        Response.AddHeader("content-disposition",

         "attachment;filename=ExportExcel.xls");

        Response.Charset = "";

        Response.ContentType = "application/vnd.ms-excel";

        StringWriter sw = new StringWriter();

        HtmlTextWriter hw = new HtmlTextWriter(sw);



        GridView1.AllowPaging = false;

        GridView1.DataBind();



        GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");

        GridView1.HeaderRow.Cells[0].Visible = false;

        GridView1.HeaderRow.Cells[1].Style.Add("background-color", "green");

        GridView1.HeaderRow.Cells[2].Style.Add("background-color", "green");

        GridView1.HeaderRow.Cells[3].Style.Add("background-color", "green");

        GridView1.HeaderRow.Cells[4].Style.Add("background-color", "green");

        if (ViewState["CheckBoxArray"] != null)

        {

            ArrayList CheckBoxArray = (ArrayList)ViewState["CheckBoxArray"];

            string checkAllIndex = "chkAll-" + GridView1.PageIndex;

            int rowIdx = 0;

            for (int i = 0; i < GridView1.Rows.Count; i++)

            {

                GridViewRow row = GridView1.Rows[i];

                row.Visible = false;



                if (row.RowType == DataControlRowType.DataRow)

                {

                    if (CheckBoxArray.IndexOf(i + 1) != -1)

                    {

                        row.Visible = true;

                        row.BackColor = System.Drawing.Color.White;

                        row.Cells[0].Visible = false;

                        row.Attributes.Add("class", "textmode");

                        if (rowIdx % 2 != 0)

                        {



                            row.Cells[1].Style.Add("background-color", "#C2D69B");

                            row.Cells[2].Style.Add("background-color", "#C2D69B");

                            row.Cells[3].Style.Add("background-color", "#C2D69B");

                            row.Cells[4].Style.Add("background-color", "#C2D69B");

                        }

                        rowIdx++;

                    }

                }

            }

        }

        GridView1.RenderControl(hw);

        string style = @"<style> .textmode { mso-number-format:\@; } </style>";

        Response.Write(style);

        Response.Output.Write(sw.ToString());

        Response.End();

    }
  • Start using a specialized library for creating Excel files, like [EPPlus](https://github.com/JanKallman/EPPlus). [Example here](https://www.vanderwaal.eu/mini-projecten/epplus-export-list-t-to-excel), [here](https://stackoverflow.com/a/47293207/5836671) and [here](https://stackoverflow.com/questions/52002573/send-excel-email-attachment-c-sharp). All you are doing now is creating a HTML page with an .xls extension. – VDWWD May 17 '22 at 19:46

0 Answers0