0
0

Report.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace tools;
  3. /**
  4. * @copyright (c) 2014 aircheng
  5. * @file report.php
  6. * @brief 导出excel类库
  7. * @author dabao
  8. * @date 2014/11/28 22:09:43
  9. * @version 1.0.0
  10. * @update 4.6
  11. * @date 2016/9/15 23:30:28
  12. * @author nswe
  13. * @content 重构了写入方式和方法
  14. */
  15. class Report
  16. {
  17. //文件名
  18. private $fileName = 'user';
  19. //数据内容
  20. private $_data = "";
  21. //构造函数
  22. public function __construct($fileName = '')
  23. {
  24. $this->setFileName($fileName);
  25. }
  26. //设置要导出的文件名
  27. public function setFileName($fileName)
  28. {
  29. $this->fileName = $fileName;
  30. }
  31. /**
  32. * @brief 写入内容操作,每次存入一行
  33. * @param $data array 一维数组
  34. */
  35. public function setTitle($data = array())
  36. {
  37. array_walk($data,function(&$val,$key)
  38. {
  39. $val = "<th style='text-align:center;background-color:green;color:#fff;font-size:12px;vnd.ms-excel.numberformat:@'>".$val."</th>";
  40. });
  41. $this->_data .= "<tr>".join($data)."</tr>";
  42. }
  43. /**
  44. * @brief 写入标题操作
  45. * @param $data array 数据
  46. */
  47. public function setData($data = array())
  48. {
  49. array_walk($data,function(&$val,$key)
  50. {
  51. $val = "<td style='text-align:center;font-size:12px;vnd.ms-excel.numberformat:@'>".$val."</td>";
  52. });
  53. $this->_data .= "<tr>".join($data)."</tr>";
  54. }
  55. //开始下载
  56. public function toDownload($data = '')
  57. {
  58. // Redirect output to a client’s web browser (Excel5)
  59. header('Content-Type: application/vnd.ms-excel');
  60. header('Content-Disposition: attachment;filename='.$this->fileName.'_'.date('Y-m-d').'.xls');
  61. header('Cache-Control: max-age=0');
  62. // If you're serving to IE 9, then the following may be needed
  63. header('Cache-Control: max-age=1');
  64. // If you're serving to IE over SSL, then the following may be needed
  65. header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  66. header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
  67. header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  68. header ('Pragma: public'); // HTTP/1.0
  69. $result = $data ? $data : "<table border='1'>".$this->_data."</table>";
  70. echo <<< OEF
  71. <html>
  72. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  73. <body>
  74. {$result}
  75. </body>
  76. </html>
  77. OEF;
  78. }
  79. }