BasePage.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Web.UI.WebControls;
  3. using System.IO;
  4. using System.Web.UI;
  5. namespace Ant.Service.Utilities
  6. {
  7. public class BasePage :System.Web.UI.Page
  8. {
  9. public BasePage()
  10. {
  11. //
  12. //TODO: 在此处添加构造函数逻辑
  13. //
  14. }
  15. public static string Title = "标题";
  16. public static string keywords = "关键字";
  17. public static string description = "网站描述";
  18. protected override void OnInit(EventArgs e)
  19. {
  20. if (Session["admin"] == null || Session["admin"].ToString().Trim() == "")
  21. {
  22. Response.Redirect("login.aspx");
  23. }
  24. base.OnInit(e);
  25. }
  26. protected void ExportData(string strContent, string FileName)
  27. {
  28. FileName = FileName + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
  29. Response.Clear();
  30. Response.Charset = "gb2312";
  31. Response.ContentType = "application/ms-excel";
  32. Response.ContentEncoding = System.Text.Encoding.UTF8;
  33. //this.Page.EnableViewState = false;
  34. // 添加头信息,为"文件下载/另存为"对话框指定默认文件名
  35. Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ".xls");
  36. // 把文件流发送到客户端
  37. Response.Write("<html><head><meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">");
  38. Response.Write(strContent);
  39. Response.Write("</body></html>");
  40. // 停止页面的执行
  41. //Response.End();
  42. }
  43. /// <summary>
  44. /// 导出Excel
  45. /// </summary>
  46. /// <param name="obj"></param>
  47. public void ExportData(GridView obj)
  48. {
  49. try
  50. {
  51. string style = "";
  52. if (obj.Rows.Count > 0)
  53. {
  54. style = @"<style> .text { mso-number-format:\@; } </script> ";
  55. }
  56. else
  57. {
  58. style = "no data.";
  59. }
  60. Response.ClearContent();
  61. DateTime dt = DateTime.Now;
  62. string filename = dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Second.ToString();
  63. Response.AddHeader("content-disposition", "attachment; filename=ExportData" + filename + ".xls");
  64. Response.ContentType = "application/ms-excel";
  65. Response.Charset = "GB2312";
  66. Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
  67. StringWriter sw = new StringWriter();
  68. HtmlTextWriter htw = new HtmlTextWriter(sw);
  69. obj.RenderControl(htw);
  70. Response.Write(style);
  71. Response.Write(sw.ToString());
  72. Response.End();
  73. }
  74. catch
  75. {
  76. }
  77. }
  78. }
  79. }