简体   繁体   中英

How to set Encoding for Persian Character When Export Grid View To Excel In ASP.NET?

I Use This Code For Convert Grid View To Excel:

        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";
        using (StringWriter sw = new StringWriter())
        {
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            Page.ResponseEncoding = "UTF-8";// System.Text.Encoding.UTF8;
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.HeaderEncoding = System.Text.Encoding.UTF8;
            //To Export all pages
            gvProduct.AllowPaging = false;

            gvProduct.HeaderRow.BackColor = Color.White;
            foreach (TableCell cell in gvProduct.HeaderRow.Cells)
            {
                cell.BackColor = gvProduct.HeaderStyle.BackColor;
            }
            foreach (GridViewRow row in gvProduct.Rows)
            {
                row.BackColor = Color.White;
                foreach (TableCell cell in row.Cells)
                {
                    if (row.RowIndex % 2 == 0)
                    {
                        cell.BackColor = gvProduct.AlternatingRowStyle.BackColor;
                    }
                    else
                    {
                        cell.BackColor = gvProduct.RowStyle.BackColor;
                    }
                    cell.CssClass = "textmode";

                }
            }

            gvProduct.RenderControl(hw);

            //style to format numbers to string
            string style = @"<style> .textmode { } </style>";
            Response.Write(style);
            var a = Response.Output.Encoding;
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();

But When My Grid View Contain Persian Character After Export Those Have not Correct. I Think When Export, I Should Set Encoding. Please Help Me. Thanks.

try this

    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition",
    "attachment;filename=Export.xls");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    GridView1.AllowPaging = false;
    GridView1.DataBind();

    GridView1.RenderControl(hw);
    //style to format numbers to string
    string style = @"<style> .textmode { mso-number-format:\@; } </style> <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>";
    Response.Write(style);
    Response.Output.Write(sw.ToString());
    Response.Flush();
    Response.End();`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM