This project has moved. For the latest updates, please go here.

German Special Characters Problem when Exporting to Excel

Jul 12, 2013 at 6:21 PM
Hello,

i`m sucessfully exporting a GridView into Excel but when using german special characters (ä, ü, ö, ß) they get replaced with "& #252;" in the excel file. Is there a way to fix that problem? Not using these characters isn't an option. ;-)

My Code looks like following:
        protected void Button1_Click(object sender, EventArgs e)
        {
            // Create the workbook
            var Workbook = new XLWorkbook();

            // Create the table
            DataTable dt = new DataTable();

            // Important because the sheet must have a name
            dt.TableName = "Information";

            // Not allowing Paging to avoid potential problems with GridView
            // Replaces gvUpLoad with your own GridView
            GridView1.AllowPaging = false;

            // Loop the GridView to copy it as DataTable
            // add the columns to the datatable            
            if (GridView1.HeaderRow != null)
            {
                for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
                {
                    dt.Columns.Add(GridView1.HeaderRow.Cells[i].Text);
                }
            }

            //  add each of the data rows to the table
            foreach (GridViewRow row in GridView1.Rows)
            {
                DataRow dr;
                dr = dt.NewRow();

                for (int i = 0; i < row.Cells.Count; i++)
                {
                    dr[i] = row.Cells[i].Text.Replace(" ", "");
                }
                dt.Rows.Add(dr);
            }

            //  add the footer row to the table
            if (GridView1.FooterRow != null)
            {
                DataRow dr;
                dr = dt.NewRow();

                for (int i = 0; i < GridView1.FooterRow.Cells.Count; i++)
                {
                    dr[i] = GridView1.FooterRow.Cells[i].Text.Replace(" ", "");
                }
                dt.Rows.Add(dr);
            }

            // Add a DataTable as a worksheet
            Workbook.Worksheets.Add(dt);

            // Create Response
            HttpResponse response = Response;

            //Prepare the response
            response.Clear();
            response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            response.AddHeader("content-disposition", "attachment;filename=HelloWorld.xlsx");

            //Flush the workbook to the Response.OutputStream
            using (MemoryStream MyMemoryStream = new MemoryStream())
            {
                Workbook.SaveAs(MyMemoryStream);
                MyMemoryStream.WriteTo(response.OutputStream);
                MyMemoryStream.Close();
            }

            response.End(); 
Thanks in Advance for any help.

Greetings

musti2k3