1
votes

I have read this article that exports datatable to excel file. It worked great.

Export DataTable to Excel File

and the code is below:

dt = city.GetAllCity();//your datatable 
string attachment = "attachment; filename=city.xls"; 
Response.ClearContent(); 
Response.AddHeader("content-disposition", attachment); 
Response.ContentType = "application/vnd.ms-excel"; 
string tab = ""; 
foreach (DataColumn dc in dt.Columns) 
{ 
    Response.Write(tab + dc.ColumnName); 
    tab = "\t"; 
} 
Response.Write("\n"); 
int i; 
foreach (DataRow dr in dt.Rows) 
{ 
    tab = ""; 
    for (i = 0; i < dt.Columns.Count; i++) 
    { 
        Response.Write(tab + dr[i].ToString()); 
        tab = "\t"; 
    } 
    Response.Write("\n"); 
} 
Response.End(); 

But when I use this codes, the korean characters are all broken. Can anyone help me with this problem?

1
It may be down to Excel not recognising the unicode need to display the characters. You probably need a header to indicate that.Simon Halsey
which header shall I indicate?Joshua Son
ContentType charset en.wikipedia.org/wiki/List_of_HTTP_header_fields#Responses make sure you include UTF8 as shown in the articleSimon Halsey
Of course I put utf-8 in head. Anything else??Joshua Son

1 Answers

2
votes

I use windows-1254 character set for Turkish. I guess KS_X_1001 will work for you. For more sets : http://en.wikipedia.org/wiki/Character_encoding

Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1254");
Response.Charset = "windows-1254";