将带有阿拉伯数据的数据表导出到Excel [英] Export Data table With Arabic Data to Excel
本文介绍了将带有阿拉伯数据的数据表导出到Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含阿拉伯数据的数据表。
当我将其导出到excel时,我无法正确获取阿拉伯数据。
I am Having a Data Table which consists of arabic data. When I am Exporting it to excel I am unable to get the Arabic data correctly.
当前我的代码如下,
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:
下面是对我有用的最终代码
Below is the final code which has worked for me
////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();
这是通过引用网络上的几篇文章撰写的。希望对您有所帮助
This was written by referring few articles from the web .Hope it helps
推荐答案
将此代码添加到您的代码中:
add this code to your code:
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
这篇关于将带有阿拉伯数据的数据表导出到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文