导出数据表使用阿拉伯数据到Excel

时间:2013-08-01 08:07:04

标签: asp.net c#-4.0 export-to-excel

我有一个由阿拉伯数据组成的数据表。 当我将其导出为Excel时,我无法正确获取阿拉伯数据。

目前我的代码如下,

  public void ExportExcel(DataTable table, string filename)
    {
        if (table != null && filename != "")
        {

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Buffer = true;

            HttpContext.Current.Response.AddHeader("content-disposition", "attachment;attachment;filename=" + filename + ".xls");
            HttpContext.Current.Response.Charset = "";
            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";               
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();

            System.Web.UI.HtmlTextWriter htmlWrite =
            new HtmlTextWriter(stringWrite);

            GridView GrdExcel = new GridView();
            GrdExcel.AllowPaging = false;
            GrdExcel.DataSource = table;
            GrdExcel.DataBind();
            for (int i = 0; i < GrdExcel.Rows.Count; i++)
            {
                GridViewRow row = GrdExcel.Rows[i];                   
                row.Attributes.Add("class", "text");
            }
            GrdExcel.RenderControl(htmlWrite);
            string style = @"<style> .textmode { mso-number-format:\@; } </style>";
            HttpContext.Current.Response.Write(style);
            HttpContext.Current.Response.Output.Write(stringWrite.ToString());
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();


        }

    }

编辑1:

以下是对我有用的最终代码

             ////If you want the option to open the Excel file without saving than

             ////comment out the line below

            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);

            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;attachment;filename=" + filename + ".xls");

            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";


            System.IO.StringWriter stringWrite = new System.IO.StringWriter();

            System.Web.UI.HtmlTextWriter htmlWrite =
            new HtmlTextWriter(stringWrite);
            DataGrid grdExcel = new DataGrid();
            grdExcel.AllowPaging = false;
            grdExcel.DataSource = table;
            grdExcel.DataBind();
            foreach (DataGridItem i in grdExcel.Items)
            {

                foreach (TableCell tc in i.Cells)
                    tc.Attributes.Add("class", "text");

            }
            grdExcel.RenderControl(htmlWrite);
            string style = @"<style> .text { mso-number-format:\@; } </style> ";
            HttpContext.Current.Response.Write(style);
            HttpContext.Current.Response.Write(stringWrite.ToString());
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();   

这是通过从网上引用一些文章来写的。希望它有所帮助

2 个答案:

答案 0 :(得分:0)

Encoding.UTF8.GetString中的每个字符串换行(例如:Encoding.UTF8.GetString(str)

并在web.config中:

<system.web>
    <globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="he-IL"/>
</system.web>

他是我的情况,分别写你的语言文化

答案 1 :(得分:0)

将此代码添加到您的代码中:

HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
相关问题