日期:2014-05-18 浏览次数:20532 次
/// <summary> /// EmportDataSetToExcel2 傳入dt 導出規定格式excel /// </summary> /// <param name="dt"></param> public static void EmportDataTableToExcel2(DataTable dt, String[] arrText, String[] arrFil) { HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Charset = ""; HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=utf-8>"); HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; StringWriter stringWrite = new StringWriter(); HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); DataGrid dg = new DataGrid(); dg.AutoGenerateColumns = false;///////自动产生列设置为False; //dg.GridLines = System.Web.UI.WebControls.GridLines.Both;//横线与竖线 dg.HeaderStyle.BackColor = System.Drawing.Color.Silver; dg.HeaderStyle.ForeColor = System.Drawing.Color.Black; //dg.HeaderStyle.BorderWidth = 3; //dg.HeaderStyle.Height = 10; for (int i = 0; i < arrText.Length;i++ ) { BoundColumn BC = new BoundColumn(); BC.HeaderText = arrText[i];//这个就是字段汉字名称 BC.DataField = arrFil[i];//这个是字段的实际名称 dg.Columns.Add(BC); } dg.DataSource = dt; dg.DataMember = "dt"; //dg.DataKeyField = "EmpID"; dg.DataBind(); dg.RenderControl(htmlWrite); HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=result.xls"); HttpContext.Current.Response.Write(stringWrite.ToString()); HttpContext.Current.Response.End(); } }