其他分享
首页 > 其他分享> > lavarel 导出excel

lavarel 导出excel

作者:互联网

 
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
                    $sheet = $spreadsheet->getActiveSheet();
                    $sheet->mergeCells('A1:C1');
                    $sheet->setCellValue('A1', $fileName . '--年统计表');

                    $sheet->setCellValue('A2', '统计区间:' . $year . '年');
                    $sheet->setCellValue('B2', '列印时间:' . date("Y-m-d H:i"));

                    $sheet->setCellValue('A3', '单位别');
                    $sheet->setCellValue('B3', $year . '年');
                    $sheet->setCellValue('C3', '合计');

                    $sheet->getColumnDimension('A')->setWidth(50);

                    $sheet->mergeCells('B2:C2');


                    $spreadsheet->getActiveSheet()->getDefaultColumnDimension()->setWidth('20'); //默认单元格宽度
                    $spreadsheet->getActiveSheet()->getDefaultRowDimension()->setRowHeight('20'); //默认行高
                    //垂直居中对齐  
                    $spreadsheet->getActiveSheet()->getStyle('A1:C1')->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);
                    //水平居中对齐  
                    $spreadsheet->getActiveSheet()->getStyle('A1:C1')->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);

                    $spreadsheet->getActiveSheet()->getStyle('B2:C2')->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT);
$writer = new Xlsx($spreadsheet);
                    $fileName = $fileName . time() . '.xlsx';
                    $pathUrl = public_path() . '\excel/';
                    $filePath = $pathUrl . $fileName;
                    //判断目录是否存在,如果不存在就新建
                    if (!is_dir($pathUrl))
                        mkdir($pathUrl, 0755, true);
                    $writer->save($filePath); //保存exce

 

标签:sheet,lavarel,excel,导出,spreadsheet,PhpSpreadsheet,PhpOffice,setCellValue,getActi
来源: https://www.cnblogs.com/limonyun/p/16116517.html