Use Laravel-Excel and the method of deriving the flow Excel

1, using the extended package derived laravel-excel

Expansion Pack versions 3.0 and 2.0 compared to doing a lot of changes, personal feeling is easier to use. Expansion Pack gives a lot of export-based query, the view of export. The following example is based on the array of export, the other can view the document.

Export categories:

use Maatwebsite\Excel\Concerns\FromCollection;
    use Maatwebsite\Excel\Concerns\Exportable;
    use Maatwebsite\Excel\Concerns\WithHeadings;
    class UsersExport implements FromCollection, WithHeadings
    {
        use Exportable;

        private $data;
        private $headings;

        //数据注入
        public function __construct($data, $headings)
        {
            $this->data = $data;
            $this->headings = $headings;
        }

        //实现FromCollection接口
        public function collection()
        {
            return collect($this->data);
        }

        //实现WithHeadings接口
        public function headings(): array
        {
            return $this->headings;
        }

    }

Export Controller

namespace App\Http\Controllers;

    use Maatwebsite\Excel\Facades\Excel;
    use App\Exports\UsersExport;
    class UsersController extends Controller
    {
        public function export()
        {
            $data = [
                [
                    'name' => 'cheng',
                    'email' => 'cheng111'
                ],
                [
                    'name' => 'cheng',
                    'email' => 'cheng111'
                ],
            ];

            $headings = [
                'name',
                'email'
            ];
            return Excel::download(new UsersExport($data, $headings), 'users.csv');

        }
    }

2, used in the form of export flow

 public  function Export ( $ params ) 
        { 
            set_time_limit (0 ); 

            $ the Columns = [ 'field name' ]; 

            $ fileName . = 'the GPS management details' '.csv' ;
             // set tells the browser to download headers excel file 
            header ( 'the Content-the Description: File Transfer' );
             header ( 'the Type-the Content: file application / vnd.ms-Excel' );
             header ( 'the Content-Disposition: Attachment; filename = "'. $ fileName . '"' ) ;
             header ( 'the Expires: 0' );
             header ( 'Cache-Control: must-revalidate');
             Header ( 'Pragma: public' ); 

            $ FP = the fopen ( 'PHP: // output', 'A'); // open output stream 
            mb_convert_variables ( 'GBK', 'UTF-. 8', $ Columns ); 
            fputcsv ( $ FP , $ Columns );      // format the data into CSV format and written to the output stream 

            $ NUM = $ the this -> getExportNum ( $ the params );
             $ PerSize = 2000; // every time queries of number 
            $ Pages    = ceil ( $ NUM / $ PerSize ); 

            for ( $ I = 1; $i <= $pages; $i++) {
                $list = $this->getUnitExportData($params, $i, $perSize);

                foreach($list as $item) {
                    $rowData[] = $this->switchDeviceMakerToDesc($item['device_maker']);
                    .
                    .
                    .
                    mb_convert_variables('GBK', 'UTF-8', $rowData);
                    fputcsv ( $ FP , $, rowData );
                     the unset ( $, rowData ); 
                } 
                the unset ( $ accesslog ); // release variables memory 

                ob_flush ();      // Flush the output buffer to the browser 
                the flush ();         // must both ob_flush () and flush () to flush the output buffer. 
            }
             Fclose ( $ FP );
             Exit (); 
        }

 

Guess you like

Origin www.cnblogs.com/niuben/p/11458450.html