ReportPage.cs
上传用户:li2971742
上传日期:2021-11-18
资源大小:39096k
文件大小:2k
源码类别:

OA系统

开发平台:

C#

  1. using System;
  2. namespace OThinker.H3.Portal
  3. {
  4. /// <summary>
  5. /// Summary description for ReportPage.
  6. /// </summary>
  7. public class ReportPage : PortalPage
  8. {
  9. public ReportPage()
  10. {
  11. }
  12.         protected virtual string ReportTableSessionName
  13. {
  14. get
  15. {
  16. return null;
  17. }
  18. }
  19. protected System.Data.DataTable ReportTable
  20. {
  21. get
  22. {
  23. return (System.Data.DataTable)this.Session[this.ReportTableSessionName];
  24. }
  25. set
  26. {
  27. this.Session[this.ReportTableSessionName] = value;
  28. }
  29. }
  30. #region 导出
  31. protected void Export()
  32. {
  33. if(this.ReportTable != null)
  34. {
  35. byte[] content = this.Export(this.ReportTable);
  36. this.Response.ContentType = "application/vnd.ms-excel";
  37.                 // 显示内容
  38.                 this.Response.BinaryWrite(content);
  39.                 this.Response.End();
  40. }
  41. }
  42.         public byte[] Export(System.Data.DataTable Table)
  43.         {
  44.             if (Table == null)
  45.             {
  46.                 return null;
  47.             }
  48.             System.IO.MemoryStream stream = new System.IO.MemoryStream();
  49.             System.IO.StreamWriter writer = new System.IO.StreamWriter(stream, System.Text.Encoding.Unicode);
  50.             // 设置标题
  51.             for (int columnCount = 0; columnCount < Table.Columns.Count; columnCount++)
  52.             {
  53.                 if (columnCount > 0)
  54.                 {
  55.                     writer.Write("t");
  56.                 }
  57.                 writer.Write(Table.Columns[columnCount].ColumnName);
  58.             }
  59.             writer.WriteLine();
  60.             // 设置内容
  61.             for (int rowCount = 0; rowCount < Table.Rows.Count; rowCount++)
  62.             {
  63.                 for (int columnCount = 0; columnCount < Table.Columns.Count; columnCount++)
  64.                 {
  65.                     if (columnCount > 0)
  66.                     {
  67.                         writer.Write("t");
  68.                     }
  69.                     writer.Write(Table.Rows[rowCount][columnCount].ToString());
  70.                 }
  71.                 writer.WriteLine();
  72.             }
  73.             writer.Write("");
  74.             writer.Flush();
  75.             writer.Close();
  76.             byte[] content = stream.ToArray();
  77.             stream.Close();
  78.             return content;
  79.         }
  80. #endregion
  81. }
  82. }