3

I have a problem with my function for exporting .xls file from website asp.net (C#).
It save all <tr> into one string. Function work nice, but it doesn't save properly polish chars (Ł, ó, ę - etc).
how can I resolve this ?

Here is my code:

string ExcelExport;
string StringExport;


 (..)

protected void lvUsers_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item is ListViewDataItem)
    {
        (..)

        StringExport = StringExport + string.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td></tr>", currentUser.name, currentUser.surname, tel, currentUser.email);
    }
}




protected void btnDownloadList_Click(object sender, EventArgs e)
{
    ExcelExport = "<table><tr><td>Name</td><td>Surname</td><td>Telephone</td><td>E-mail</td></tr><tr><td> </td><td> </td><td> </td><td> </td></tr>" + StringExport + "</table>";

    Response.AddHeader("Content-disposition", "attachment; filename=raport.xls");
    Response.Charset = "utf-8";
    Response.ContentType = "application/ms-excel";
    Response.Write(ExcelExport);
    Response.End();

}
whoah
  • 4,363
  • 10
  • 51
  • 81
  • This seems to be a encoding issue. Did you search on this way ? http://stackoverflow.com/questions/227575/encoding-problem-with-httpwebresponse could help, or http://stackoverflow.com/questions/12190524/getting-response-as-a-string-c-net. – Nate B. Nov 06 '12 at 16:00

2 Answers2

2

Try using the byte order mark (BOM) as suggested in this similar solution

protected void btnDownloadList_Click(object sender, EventArgs e)
{
    ExcelExport = "<table><tr><td>Name</td><td>Surname</td><td>Telephone</td><td>E-mail</td></tr><tr><td> </td><td> </td><td> </td><td> </td></tr>" + StringExport + "</table>";

    Response.AddHeader("Content-disposition", "attachment; filename=raport.xls");
    Response.ContentType = "application/ms-excel";
    byte[] BOM = { 0xEF, 0xBB, 0xBF }; // The BOM for UTF-8 encoding.
    Response.BinaryWrite(BOM);
    Response.Write(ExcelExport);
    Response.End();

}
chridam
  • 100,957
  • 23
  • 236
  • 235
1

The utf-8 character set does not contain polish characters. A quick search reveals that the ISO 8859-2 does. I'd try changing the Charset and seeing if that fixes your issue.

rie819
  • 1,249
  • 12
  • 19