PHP用PhpOffice->PhpSpreadsheet导出excel

发布时间 2023-05-23 15:43:44作者: 杨杨23

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

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

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

安装:

composer require phpoffice/phpspreadsheet

工具类:

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:

$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);