c# - 在 ASP.NET 中将 GridView 导出到 Excel 时如何设置波斯语字符编码?

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

我使用此代码将 GridView 转换为 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();

但是当我的 GridView 导出后包含波斯字符时,这些字符不正确。 我认为导出时应该设置编码。 请帮我。 谢谢。

最佳答案

试试这个

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

关于c# - 在 ASP.NET 中将 GridView 导出到 Excel 时如何设置波斯语字符编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23694005/

相关文章:

asp.net - 如何使用 SQL Server session 管理检查 ASPState 数据库中的 session 值(用户是否存在)

c# - 模板中的服务器控件如何对数据上下文敏感?

asp.net - EntityDataSource 在查询中用 % 通配符替换 *

mysql - 如果连接超时则取消插入命令操作 MySQL

asp.net - 作为 ASP.Net 报告生成器替代 Excel

c# - Spreadsheetlight 内存使用

c# - 用于简单实时圆检测的霍夫变换替代方案

c# - WPF ListViewItem 项目复选框。如何获取所有选中的项目?

c# - 比较两个自定义对象时 Nunit Assert.AreEqual() 失败

SSIS - 无法创建表 Microsoft jet 数据库引擎找不到对象