GridViewExport.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System.Data;
  2. using System.Web;
  3. using System.Web.UI;
  4. using System.Web.UI.WebControls;
  5. using System.IO;
  6. using System.Text;
  7. namespace Ant.Service.Utilities
  8. {
  9. /// <summary>
  10. /// Summary description for GridViewExport
  11. /// </summary>
  12. public class GridViewExport
  13. {
  14. public GridViewExport()
  15. {
  16. //
  17. // TODO: Add constructor logic here
  18. //
  19. }
  20. public static void Export(string fileName, GridView gv)
  21. {
  22. HttpContext.Current.Response.Clear();
  23. HttpContext.Current.Response.AddHeader(
  24. "content-disposition", string.Format("attachment; filename={0}", fileName));
  25. HttpContext.Current.Response.ContentType = "application/ms-excel";
  26. //HttpContext.Current.Response.Charset = "utf-8";
  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 = GridLines.Both; //单元格之间添加实线
  34. // add the header row to the table
  35. if (gv.HeaderRow != null)
  36. {
  37. 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. PrepareControlForExport(row);
  44. table.Rows.Add(row);
  45. }
  46. // add the footer row to the table
  47. if (gv.FooterRow != null)
  48. {
  49. 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(System.Web.UI.Control control)
  65. {
  66. for (int i = 0; i < control.Controls.Count; i++)
  67. {
  68. System.Web.UI.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. PrepareControlForExport(current);
  97. }
  98. }
  99. }
  100. /// <summary>
  101. /// 导出Grid的数据(全部)到Excel
  102. /// 字段全部为BoundField类型时可用
  103. /// 要是字段为TemplateField模板型时就取不到数据
  104. /// </summary>
  105. /// <param name="grid">grid的ID</param>
  106. /// <param name="dt">数据源</param>
  107. /// <param name="excelFileName">要导出Excel的文件名</param>
  108. public static void OutputExcel(GridView grid, DataTable dt, string excelFileName)
  109. {
  110. Page page = (Page)HttpContext.Current.Handler;
  111. page.Response.Clear();
  112. string fileName = System.Web.HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(excelFileName));
  113. page.Response.AddHeader("Content-Disposition", "attachment:filename=" + fileName + ".xls");
  114. page.Response.ContentType = "application/vnd.ms-excel";
  115. page.Response.Charset = "utf-8";
  116. StringBuilder s = new StringBuilder();
  117. s.Append("<HTML><HEAD><TITLE>" + fileName + "</TITLE><META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><body>");
  118. int count = grid.Columns.Count;
  119. s.Append("<table border=1>");
  120. s.AppendLine("<tr>");
  121. for (int i = 0; i < count; i++)
  122. {
  123. if (grid.Columns[i].GetType() == typeof(BoundField))
  124. s.Append("<td>" + grid.Columns[i].HeaderText + "</td>");
  125. //s.Append("<td>" + grid.Columns[i].HeaderText + "</td>");
  126. }
  127. s.Append("</tr>");
  128. foreach (DataRow dr in dt.Rows)
  129. {
  130. s.AppendLine("<tr>");
  131. for (int n = 0; n < count; n++)
  132. {
  133. if (grid.Columns[n].Visible && grid.Columns[n].GetType() == typeof(BoundField))
  134. s.Append("<td>" + dr[((BoundField)grid.Columns[n]).DataField].ToString() + "</td>");
  135. }
  136. s.AppendLine("</tr>");
  137. }
  138. s.Append("</table>");
  139. s.Append("</body></html>");
  140. page.Response.BinaryWrite(System.Text.Encoding.GetEncoding("utf-8").GetBytes(s.ToString()));
  141. page.Response.End();
  142. }
  143. }
  144. }