Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting xls file from C# - doesn't save special letters

Tags:

c#

asp.net

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();

}
like image 804
whoah Avatar asked Nov 30 '25 18:11

whoah


1 Answers

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();

}
like image 60
chridam Avatar answered Dec 03 '25 08:12

chridam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!