2012-11-06 43 views
3

我的網站asp.net(C#)中導出.xls文件的功能有問題。
它將所有<tr>保存爲一個字符串。 功能很好,但它不能保存適當的波蘭字符(Ł,ó,ê - 等)。
我該如何解決這個問題?從C#導出xls文件 - 不保存特殊字母

這裏是我的代碼:

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

} 
+0

建議這似乎是一個編碼問題。你是用這種方式搜索的嗎? http://stackoverflow.com/questions/227575/encoding-problem-with-httpwebresponse可以幫助,或http://stackoverflow.com/questions/12190524/getting-response-as-a-string-c-net。 – Gnial0id

回答

2

嘗試使用byte order mark (BOM)在本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(); 

} 
+0

很好的解決方案,謝謝 – whoah

+0

不客氣! – chridam

1

的UTF-8字符集不包含拋光字符。快速搜索顯示ISO 8859-2。我會嘗試更改字符集並查看是否可以解決您的問題。