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

Created 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 ü 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:

```
Response.Clear();

Response.AddHeader("content-disposition",
"attachment;filename=ExcelExport.xls");

Response.Charset = "";

// If you want the option to open the Excel file without saving than

// comment out the line below

// Response.Cache.SetCacheability(HttpCacheability.NoCache);

Response.ContentType = "application/vnd.xls";

System.IO.StringWriter stringWrite = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter htmlWrite =
new HtmlTextWriter(stringWrite);

GridView1.RenderControl(htmlWrite);

Response.Write(stringWrite.ToString());

Response.End();
}

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("&nbsp;", "");
}
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("&nbsp;", "");
}
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

Viewing all articles
Browse latest Browse all 1877

Trending Articles



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