【问题标题】:Export DataTable to Excel File in asp.net在 asp.net 中将 DataTable 导出到 Excel 文件
【发布时间】:2011-12-12 05:31:40
【问题描述】:

我已阅读这篇将数据表导出到 excel 文件的文章。 效果很好。

Export DataTable to Excel File

代码如下:

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

但是当我使用这个代码时,韩文字符都坏了。 谁能帮我解决这个问题?

【问题讨论】:

  • 可能是 Excel 无法识别需要显示字符的 unicode。你可能需要一个标题来表明这一点。
  • 我应该指出哪个标题?
  • ContentType charset en.wikipedia.org/wiki/List_of_HTTP_header_fields#Responses 确保包含文章中所示的 UTF8
  • 当然我把 utf-8 放在了 head 里。还有什么吗??

标签: c# asp.net excel datatable


【解决方案1】:

我使用windows-1254 土耳其语字符集。我猜KS_X_1001 会为你工作。 更多套装:http://en.wikipedia.org/wiki/Character_encoding

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

【讨论】:

    猜你喜欢
    • 2012-08-01
    • 1970-01-01
    • 2018-12-11
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 2016-01-15
    • 2012-11-20
    • 1970-01-01
    相关资源
    最近更新 更多