从 asp .Net c# 中的数据表创建 .xls 的最佳方法是什么?
What is the best way to create .xls from datatable in asp .Net c#?
我有一个数据表,我想从中导出 excel,
我的数据表包含 unicode 字符(波斯字符),我想将其正确转换为 .xls 文件。
我想知道是否可以使用 Stimulsoft 做到这一点?
这是我常用的方法
public void ExportDetails(DataTable tempDataTable, string FileName)
{
try
{
System.IO.StringWriter objStringWriter1 = new System.IO.StringWriter();
System.Web.UI.WebControls.GridView gridView1 = new System.Web.UI.WebControls.GridView();
System.Web.UI.HtmlTextWriter objHtmlTextWriter1 = new System.Web.UI.HtmlTextWriter(objStringWriter1);
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Unicode;
Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
gridView1.DataSource = tempDataTable;
gridView1.DataBind();
gridView1.HeaderStyle.Font.Bold = true;
gridView1.HeaderStyle.BackColor = System.Drawing.Color.Gray;
gridView1.RenderControl(objHtmlTextWriter1);
gridView1.Dispose();
HttpContext.Current.Response.Write(objStringWriter1.ToString());
HttpContext.Current.Response.End();
}
catch (Exception ex)
{
}
}
我有一个数据表,我想从中导出 excel, 我的数据表包含 unicode 字符(波斯字符),我想将其正确转换为 .xls 文件。 我想知道是否可以使用 Stimulsoft 做到这一点?
这是我常用的方法
public void ExportDetails(DataTable tempDataTable, string FileName)
{
try
{
System.IO.StringWriter objStringWriter1 = new System.IO.StringWriter();
System.Web.UI.WebControls.GridView gridView1 = new System.Web.UI.WebControls.GridView();
System.Web.UI.HtmlTextWriter objHtmlTextWriter1 = new System.Web.UI.HtmlTextWriter(objStringWriter1);
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Unicode;
Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
gridView1.DataSource = tempDataTable;
gridView1.DataBind();
gridView1.HeaderStyle.Font.Bold = true;
gridView1.HeaderStyle.BackColor = System.Drawing.Color.Gray;
gridView1.RenderControl(objHtmlTextWriter1);
gridView1.Dispose();
HttpContext.Current.Response.Write(objStringWriter1.ToString());
HttpContext.Current.Response.End();
}
catch (Exception ex)
{
}
}