1

アラビア語のデータで構成されるデータ テーブルがあります。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();   

これは、Web のいくつかの記事を参照して作成されました。

4

2 に答える 2

0

すべての文字列の折り返しEncoding.UTF8.GetString(例: Encoding.UTF8.GetString(str))

そしてweb.configで:

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

he-IL は私の場合、あなたの言語文化をそれぞれ書いてください

于 2013-08-01T11:44:05.597 に答える