/**/ /// <summary> /// 匯出Excel /// </summary> /// <param name="ds">數據集</param> public static void InitExcel(DataSet ds) ... { DataGrid dgExport = null; StringWriter strWriter = null; HtmlTextWriter htmlWriter = null; System.Data.DataTable dt = ds.Tables[0]; for( int i=0; i < dt.Rows.Count; i++ ) ...{ for( int j=0; j < dt.Columns.Count; j++ ) ...{ string temp = dt.Rows[i][j].ToString(); bool flag = false; while(temp.IndexOf(" ") != -1) ...{ temp = temp.Replace(" "," <br> "); flag = true; } if( flag ) dt.Rows[i][j] = temp; } } if( dt != null ) ...{ HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; HttpContext.Current.Response.Charset = ""; strWriter = new StringWriter(); htmlWriter = new HtmlTextWriter(strWriter); dgExport = new DataGrid(); dgExport.DataSource = dt.DefaultView; dgExport.AllowPaging = false; dgExport.HeaderStyle.ForeColor = System.Drawing.Color.White; dgExport.HeaderStyle.BackColor = System.Drawing.Color.Gray; dgExport.DataBind(); dgExport.RenderControl(htmlWriter); HttpContext.Current.Response.Write(strWriter.ToString()); HttpContext.Current.Response.End(); } }