/EQT_V2/EQT/EQTWebApp/App_Code/GridViewExportUtil.cs
C# | 110 lines | 82 code | 10 blank | 18 comment | 15 complexity | c69702d96a14fb08e420e0ab526e0585 MD5 | raw file
Possible License(s): LGPL-2.1
1using System; 2using System.Data; 3using System.Configuration; 4using System.IO; 5using System.Web; 6using System.Web.Security; 7using System.Web.UI; 8using System.Web.UI.WebControls; 9using System.Web.UI.WebControls.WebParts; 10using System.Web.UI.HtmlControls; 11 12/// <summary> 13/// 14/// </summary> 15public class GridViewExportUtil 16{ 17 /// <summary> 18 /// 19 /// </summary> 20 /// <param name="fileName"></param> 21 /// <param name="gv"></param> 22 public static void Export(string fileName, GridView gv) 23 { 24 HttpContext.Current.Response.Clear(); 25 HttpContext.Current.Response.AddHeader( 26 "content-disposition", string.Format("attachment; filename={0}", fileName)); 27 HttpContext.Current.Response.ContentType = "application/ms-excel"; 28 29 using (StringWriter sw = new StringWriter()) 30 { 31 using (HtmlTextWriter htw = new HtmlTextWriter(sw)) 32 { 33 // Create a form to contain the grid 34 Table table = new Table(); 35 36 table.GridLines = gv.GridLines; 37 38 // add the header row to the table 39 if (gv.HeaderRow != null) 40 { 41 GridViewExportUtil.PrepareControlForExport(gv.HeaderRow); 42 table.Rows.Add(gv.HeaderRow); 43 } 44 45 // add each of the data rows to the table 46 foreach (GridViewRow row in gv.Rows) 47 { 48 GridViewExportUtil.PrepareControlForExport(row); 49 table.Rows.Add(row); 50 } 51 52 // add the footer row to the table 53 if (gv.FooterRow != null) 54 { 55 GridViewExportUtil.PrepareControlForExport(gv.FooterRow); 56 table.Rows.Add(gv.FooterRow); 57 } 58 59 // render the table into the htmlwriter 60 table.RenderControl(htw); 61 62 // render the htmlwriter into the response 63 HttpContext.Current.Response.Write(sw.ToString()); 64 HttpContext.Current.Response.End(); 65 } 66 } 67 } 68 69 /// <summary> 70 /// Replace any of the contained controls with literals 71 /// </summary> 72 /// <param name="control"></param> 73 private static void PrepareControlForExport(Control control) 74 { 75 for (int i = 0; i < control.Controls.Count; i++) 76 { 77 Control current = control.Controls[i]; 78 if (current is LinkButton) 79 { 80 control.Controls.Remove(current); 81 control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text)); 82 } 83 else if (current is ImageButton) 84 { 85 control.Controls.Remove(current); 86 control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText)); 87 } 88 else if (current is HyperLink) 89 { 90 control.Controls.Remove(current); 91 control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text)); 92 } 93 else if (current is DropDownList) 94 { 95 control.Controls.Remove(current); 96 control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text)); 97 } 98 else if (current is CheckBox) 99 { 100 control.Controls.Remove(current); 101 control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False")); 102 } 103 104 if (current.HasControls()) 105 { 106 GridViewExportUtil.PrepareControlForExport(current); 107 } 108 } 109 } 110}