PHP用PhpOffice->PhpSpreadsheet导出excel

phpexcel由于版本陈旧性能低下官方放弃维护
转而开发PhpSpreadsheet 用了最新得psr标准因而 对php版本不向下兼容需要注意!。

PhpSpreadsheet是一个用纯PHP编写的库,提供了一组类,使您可以读取和写入不同的电子表格文件格式
PhpSpreadsheet提供了丰富的API接口,可以设置诸多单元格以及文档属性,包括样式、图片、日期、函数等等诸多应用,总之你想要什么样的Excel表格,PhpSpreadsheet都能做到

使用 PhpSpreadsheet 开发的PHP要求 7.1或更高版本
PhpSpreadsheet 支持链式操作

安装:

composer require phpoffice/phpspreadsheet

工具类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
class ExcelUtil extends Model {
      private  $cellName = [
          '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',
          'AA',
          'AB',
          'AC',
          'AD',
          'AE',
          'AF',
          'AG',
          'AH',
          'AI',
          'AJ',
          'AK',
          'AL',
          'AM',
          'AN',
          'AO',
          'AP',
          'AQ',
          'AR',
          'AS',
          'AT',
          'AU',
          'AV',
          'AW',
          'AX',
          'AY',
          'AZ'
      ];
 
      public static function export($data, $title, $filename, $fields=[]) {
 
              // Create new Spreadsheet object
              $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
              $sheet = $spreadsheet->getActiveSheet();
 
              //设置表头单元格内容
              foreach ($title as $key => $value) {
                  $column = $this->cellName[$key].'1';
                  // 单元格内容写入
                  $sheet->setCellValueByColumnAndRow($key + 1, 1, $value);
                  //设置字体粗体
                  $spreadsheet->getActiveSheet()->getStyle($column)->getFont()->setBold(true);
              }
 
            //设置表格数据,从第二行开始
              $row = 2;
              foreach ($data as $item) {
                  $column = 1;
                  if($fields) {
                      foreach ($fields as $field) {
                          // 单元格内容写入
                          $sheet->setCellValueByColumnAndRow($column, $row, $item[$field]);
                          $column++;
                      }
                      $row++;
                      continue;
                  }else{
                foreach ($item as $value) {
                            // 单元格内容写入
                            $sheet->setCellValueByColumnAndRow($column, $row, $value);
                            $column++;
                      }
                      $row++;
            }
              }
 
              // Redirect output to a client’s web browser (Xlsx)
              header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
              header('Content-Disposition:attachment;filename="' . $filename . '.xls"');
              header('Cache-Control: max-age=0');
              // If you're serving to IE 9, then the following may be needed
              header('Cache-Control: max-age=1');
 
              // If you're serving to IE over SSL, then the following may be needed
              header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
              header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
              header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
              header('Pragma: public'); // HTTP/1.0
 
              $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
              $writer->save('php://output');
              exit;
      }
}

  

调用工具类导出excel:

1
2
3
4
5
6
7
$list = [
      [1, 11.00, '2023-05-23'],
      [2, 22.00, '2023-05-23']
];
$filename = '充值明细(' . date("Y-m-d", time()) . ')';
$header = array('ID', '金额', '支付时间');
ExcelUtil::export($list, $header, $filename);

  

 

posted @   杨杨23  阅读(98)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示