在asp .Net c#中从datatable创建.xls的最佳方法是什么?

时间:2015-08-05 10:09:20

标签: asp.net excel export stimulsoft

我有一个数据表,我想从中导出excel, 我的数据表包含unicode字符(波斯字符),我想正确地将它转换为.xls文件。 我很奇怪是否有可能用Stimulsoft做到这一点?

1 个答案:

答案 0 :(得分:2)

这是我使用的常用方法

       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)
                {


                }
}