• Laravel 中使用 Laravel-Excel 美化


    <?php
    
    use MaatwebsiteExcelClassesLaravelExcelWorksheet;
    use MaatwebsiteExcelExceptionsLaravelExcelException;
    use MaatwebsiteExcelFacadesExcel;
    use MaatwebsiteExcelWritersCellWriter;
    use MaatwebsiteExcelWritersLaravelExcelWriter;
    use PHPExcel_Exception;
    
    trait HandlerExcelTrait
    {
        /**
         * @var array
         */
        protected $cellLetter = [
            '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'
        ];
    
        /**
         * $data = Array(
         *  [0] => Array(
         *      [0] => 序号
         *      )
         *  [1] => Array(
         *      [id] => 1
         *      )
         *  )
         * $title = ['测试']
         * @param string $fileName
         * @param array $title
         * @param array $data
         * @return mixed
         * @throws LaravelExcelException
         */
        public function exportExcel(string $fileName, array $title, array $data)
        {
                try {
                return Excel::create($fileName, function ($excel) use ($data, $title) {
                        /** @var LaravelExcelWriter $excel */
                        $excel->sheet('sheet', function ($sheet) use ($data, $title) {
                                /** @var LaravelExcelWorksheet $sheet */
                                $column = $this->cellLetter[count($data[0]) - 1];
    
                                try {
                                        $sheet->fromArray($data, null, 'A1', true, false);
                                } catch (PHPExcel_Exception $e) {
                                        throw new LaravelExcelException($e->getMessage());
                                }
    
                    /** 此为设置整体样式 */
                                $sheet->setStyle([
                                        'font' => [
                                                'name' => 'Calibri',
                                                'size' => 12,
                                                'bold' => false,
                                        ]
                                ])
                                    ->prependRow($title)
                                    ->row(1, function ($row) {
                                            /** @var CellWriter $row */
                                            $row->setFont(array(   //设置标题的样式
                                                    'family' => 'Calibri',
                                                    'size' => '16',
                                                    'bold' => true
                                            ));
                                    })
                                    ->mergeCells('A1:' . $column . '1')
                                    ->cell('A2:' . $column . '2', function ($cells) {
                                            /** @var CellWriter $cells */
                                            $cells->setBackground('#AAAAFF');
                                    })->setHeight(1, 30)
                                    ->setAutoFilter('A2:' . $column . '2');  //设置自动过滤
    
                        /** 此为针对每行的高宽进行设置 */
                            for ($i = 2; $i <= count($data[0]) + 1; $i++) {
                                    $sheet->setHeight($i, 20);
                                    $sheet->setWidth($this->cellLetter[$i - 1], 30);
                                    $sheet->row($i - 1, function ($row) {
                                            /** @var CellWriter $row */
                                            $row->setAlignment('center');
                                            $row->setValignment('center');
                                    });
                            }
                    });
                        })->export('xlsx');
                } catch (LaravelExcelException $e) {
                        throw new LaravelExcelException($e->getMessage());
                }
        }
    }

    原文:https://learnku.com/articles/7095/laravel-excel-beautification-in-laravel

  • 相关阅读:
    C语言I博客作业02
    C语言I—2019秋作业01
    C语言I作业10
    C语言I作业09
    C语言I作业08
    C语言I作业07
    C语言I|作业06
    C语言I作业05
    C语言I作业004:第八周作业
    c语言|作业003
  • 原文地址:https://www.cnblogs.com/niuben/p/11458731.html
Copyright © 2020-2023  润新知