Quantcast
Channel: ClosedXML - The easy way to OpenXML
Viewing all articles
Browse latest Browse all 1877

Commented Unassigned: German Special Characters Problem when Exporting to Excel [8869]

$
0
0
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
Comments: ** Comment from web user: losmoz **

Hi musti2k3,

When you assign the content of a GridView cell to a DataTable cell, the content is Html-encoded. What you need to do is decode the content before you assign it. You can modify your code this way:

```
// 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] = __System.Net.WebUtility.HtmlDecode(__row.Cells[i].Text__)__;
}
dt.Rows.Add(dr);
}

```

Hope this helps... :)


Viewing all articles
Browse latest Browse all 1877

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>