当前位置: 首页 > 知识库问答 >
问题:

将带有阿拉伯文数据的数据表导出到Excel

穆鸿飞
2023-03-14

我有一个由阿拉伯语数据组成的数据表。当我将其导出到 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个答案

屠华辉
2023-03-14

中的每个字符串换行Encoding.UTF8. GetString(用于ex:Encoding.UTF8. GetString(str)

在web.config:

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

he-IL 就我而言,分别写下您的语言文化

彭涵衍
2023-03-14

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

HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
 类似资料:
  • 问题内容: 我已经更改了表和列的字符集,我得到的阿拉伯文本为???? MYSQL数据库中的标记 这是桌子的设计 当我再次触发选择查询时,它显示???? 作为文字? 谁能告诉我我在哪里做错了? 问题答案: 手动将阿拉伯数据插入您的Phpmyadmin。 首先,您检查数据库,表和列名称是否设置为utf8。如果未将这些设置为utf8,则首先进行设置,然后可以将阿拉伯数据插入到db表中。 对于数据库: 对

  • 问题内容: 使用JDBC驱动程序从Java中的oracle中读取oracle阿拉伯字符时遇到问题,主要问题是我找不到正确的字符编码来获取正确的数据,但是我使用此方法手动解决了问题: 此方法可以给我正确的字符,如数据库中显示的那样,但是当我尝试更新/插入阿拉伯数据时,它将保存错误的字符。例如:我的文本在数据库中另存为“ ?????????” 而不是“مرحبا”。 这是我连接到oracle数据库的方

  • 我正面临着从Oracle数据库12c获取阿拉伯语内容的问题,我已经回答了大多数问题,但没有任何问题与我一起工作。 我的阿拉伯字符返回如下“?????” 即使在java上,当我获得数据时,它也不会返回阿拉伯值 windows 10笔记本电脑(使用windows 10管理用户登录) Oracle 12C(使用系统用户登录) Java版本“1.8.0_152” 我在这里和网上找到了很多问题,比如: 无法

  • 我正在使用以下代码将带有图像的数据导出到Excel。 密码 Excel正在正常下载。但问题是当我过滤中的数据时。中的图像位于属性调整大小。如何使图像与属性,?

  • 我有一个独立的H2服务器,正在收集数据。为了进行测试,我希望将数据从服务器中提取到CSV文件中。有什么工具吗?

  • 本文向大家介绍c# 将Datatable数据导出到Excel表格中,包括了c# 将Datatable数据导出到Excel表格中的使用技巧和注意事项,需要的朋友参考一下 话不多说,请看代码: 以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持呐喊教程!

  • 我正在对php页面进行调整,希望最终用户能够从下拉列表中选择“姓名和电子邮件”。数据将来自mySQL数据库。 我已经设法让它部分工作,但是必须有东西做一个"验证",因为当页面上的所有数据都被选中,最终用户试图提交页面时,最终用户会收到电子邮件地址是无效,而事实上它是100%有效的。 与此相关,我更改的部分是,最终用户过去必须手动键入他们的电子邮件地址,现在他们可以从下拉列表中选择,但现在它告诉我们

  • 问题内容: 我有SSIS包,可以将数据从表导出到Excel文件 这是我的步骤: 删除Excel表 创建具有我选择查询格式的Excel表格,该表格用于从数据库中检索数据 将数据从数据库插入Excel文件 我用过像查询 我从10000行中检索出3000行,并将这3000行放入我的Excel工作表中。 但是,当打开我的Excel工作表时,我看到滚动条一直延伸到第10000行并结束,因此我的Excel工作