使用PhpSpreadsheet 导出Excel

官方文档地址

安装
composer require phpoffice/phpspreadsheet


  $spreadsheet =new Spreadsheet();
        $sheet =$spreadsheet->getActiveSheet();
        $sheet->setCellValue('A1', '记录ID');
        $sheet->setCellValue('B1', '房间');
        $sheet->setCellValue('C1', '开门人员');
        $sheet->setCellValue('D1', '开门方式');
        $sheet->setCellValue('E1', '开门时间');
        $sheet->setCellValue('F1', '是否管理员');
        $i =1;
        foreach ($list as &$vo) {
            $i++;

            $sheet->setCellValueByColumnAndRow('1',$i,$vo['record_id']);
            $sheet->setCellValueByColumnAndRow('2',$i,$vo['region_name'].$vo['region_name'].$vo['layer_name'].$vo['room_name']);
            $sheet->setCellValueByColumnAndRow('3',$i,$vo['name']);
            $sheet->setCellValueByColumnAndRow('4',$i,$vo['type']);
            $sheet->setCellValueByColumnAndRow('5',$i,date("Y-m-d H:i:s",$vo['created_at']));
            $sheet->setCellValueByColumnAndRow('6',$i,$vo['is_admin']?"是":"否");

        }
        $writer = new Xlsx($spreadsheet);
        $url ="dc/开门记录".rand(99,10).'.xlsx';
        $writer->save(public_path().$url);

循环
请先登录后发表评论
  • latest comments
  • 总共0条评论