/EQT_V2/EQT/EQTWebApp/App_Code/GridViewExportUtil.cs

http://sgsoft-las.googlecode.com/ · C# · 110 lines · 82 code · 10 blank · 18 comment · 15 complexity · c69702d96a14fb08e420e0ab526e0585 MD5 · raw file

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