导出到excel asp.net时的阿拉伯语单词

时间:2014-04-15 20:22:54

标签: c# excel

我在从c#代码后面导出阿拉伯语作品时遇到了问题: 我用的代码:

        HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        string attachment = "attachment; filename=Employee.xls";
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.Charset = "UTF-8";
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
        HttpContext.Current.Response.AddHeader("content-disposition", attachment);

        HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
        Response.Write("السبت");// this word mean Saturday in arabic
        Response.End();
        Application.UnLock();

但是在打开excel文件后,它看起来像: enter image description here

为什么这个词看起来像这样以及如何修复它? 如果你在第二行看到单词的大小不符合单元格的大小怎么办? 另一个问题:如何从后面的c#代码为单元格的背景着色以及如何组合单元格? 无论如何

2 个答案:

答案 0 :(得分:0)

我把你的例子改写成我刚才测试它并且一切正常 我想知道您是否遇到Culture问题

请记住我在我的末尾分配的文件名并将其传递给Method

public static void ExportToExcel(DataTable table, string fileName)
{
    HttpContext context = HttpContext.Current;
    context.Response.Clear();
    context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1254");
    context.Response.Charset = "windows-1254"; //ISO-8859-13 ISO-8859-9  windows-1254
    //Begin Table
    context.Response.Write("<table><tr>");

    //Write Header
    foreach (DataColumn column in table.Columns)
    {
        context.Response.Write("<th>" + column.ColumnName + "</th>");
    }
    context.Response.Write("</tr>");

    //Write Data
    foreach (DataRow row in table.Rows)
    {
        context.Response.Write("<tr>");
        for (int i = 0; i < table.Columns.Count; i++)
        {
            context.Response.Write("<td>" + row[i].ToString().Replace(",", string.Empty) + "</td>");
        }
        context.Response.Write("</tr>");
    }

    //End Table
    context.Response.Write("</table>");
    context.Response.ContentType = "application/vnd.ms-excel";
    context.Response.AppendHeader("content-disposition", string.Format("attachment;filename={0}.xls", fileName));
    context.Response.Flush();
    context.Response.End();
}

答案 1 :(得分:0)

使用1256而不是1254可以正常工作:)

HttpContext context = HttpContext.Current;
context.Response.Clear();
context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1256");
context.Response.Charset = "windows-1256"; //ISO-8859-13 ISO-8859-9  windows-1256