Client.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace app\operationcenter\lib\xls;
  3. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  4. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  5. //表格处理
  6. class Client
  7. {
  8. const Letters=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
  9. protected $data=[];
  10. protected $excel=null;
  11. protected $sheet=null;
  12. protected $fileName='列表';
  13. protected $fields=array();
  14. public function __construct()
  15. {
  16. $this->excel = new Spreadsheet();
  17. $this->sheet = $this->excel->getActiveSheet();
  18. $this->sheet->setTitle('视图1');
  19. }
  20. //配置标题数据
  21. public function setTitle($title,$subTitle=''){
  22. $this->fileName=$title;
  23. $this->sheet->setCellValue('A1', $title);
  24. $this->sheet->setCellValue('A2', $subTitle);
  25. return $this;
  26. }
  27. //配置头部数据
  28. public function setHeader($data,$startLine=3){
  29. $lastLetter='';//最后的字母
  30. foreach ($data as $k=>$datum) {
  31. if(!isset(self::Letters[$k]))continue;//字段数量超出
  32. $lastLetter=self::Letters[$k];
  33. $this->sheet->setCellValue(self::Letters[$k].$startLine,$datum);
  34. }
  35. if($lastLetter){
  36. //全并列
  37. $this->sheet->mergeCells("A1:{$lastLetter}1");
  38. $this->sheet->mergeCells("A2:{$lastLetter}2");
  39. }
  40. return $this;
  41. }
  42. //设置字段
  43. public function setFields($data){
  44. $this->fields=$data;
  45. return $this;
  46. }
  47. //配置数据
  48. public function setBody($data=array(),$startLine=4){
  49. if(gettype($data)=='object'){
  50. $data=$data->toArray();
  51. }
  52. foreach ($data as $k=>$datum) {
  53. $line=$startLine+$k;
  54. switch (gettype($datum)){
  55. case 'object':
  56. case 'array':
  57. if($this->fields){
  58. foreach ($this->fields as $i=>$field) {
  59. if(!isset(self::Letters[$i]))continue;//字段数量超出
  60. if(!isset($datum[$field]))continue;//字段数量超出
  61. $this->sheet->setCellValue(self::Letters[$i].$line,$datum[$field]);
  62. }
  63. }else{
  64. $i=0;
  65. foreach ($datum as $item) {
  66. if(!isset(self::Letters[$i]))continue;//字段数量超出
  67. $this->sheet->setCellValue(self::Letters[$i].$line,$item);
  68. $i++;
  69. }
  70. }
  71. break;
  72. default:
  73. $this->sheet->setCellValue('A'.$line,$datum);
  74. }
  75. }
  76. return $this;
  77. }
  78. //配置宽度
  79. public function setWidth($data=null,$indexStr=''){
  80. if($indexStr){
  81. //设置单列
  82. $this->sheet->getColumnDimension($indexStr)->setWidth($data);
  83. }else{
  84. //设置多列
  85. foreach ($data as $k=>$datum) {
  86. if(!isset(self::Letters[$k]))continue;//字段数量超出
  87. $this->sheet->getColumnDimension(self::Letters[$k])->setWidth($datum);
  88. }
  89. }
  90. return $this;
  91. }
  92. //设置列高
  93. public function setHeight($height,$line){
  94. $this->sheet->getRowDimension($line)->setRowHeight($height);
  95. return $this;
  96. }
  97. //导出
  98. public function export(){
  99. $fileName=$this->fileName.date('Y.m.d.H.i').".xlsx";
  100. $writer = new Xlsx($this->excel);
  101. // die();
  102. // Set active sheet index to the first sheet, so Excel opens this as the first sheet
  103. // $excel->setActiveSheetIndex(0);
  104. // Redirect output to a client’s web browser (Xlsx)
  105. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  106. header('Content-Disposition: attachment;filename="' . $fileName . '"');
  107. header('Cache-Control: max-age=0');
  108. // If you're serving to IE 9, then the following may be needed
  109. header('Cache-Control: max-age=1');
  110. // If you're serving to IE over SSL, then the following may be needed
  111. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  112. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
  113. header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  114. header('Pragma: public'); // HTTP/1.0
  115. // $writer = IOFactory::createWriter($excel, 'Xlsx');
  116. $writer->save('php://output');
  117. }
  118. }