mgallegos / laravel-jqgrid

@Laravel jqGrid package allows you to easily integrate the popular @jQuery Grid Plugin (jqGrid) into your Laravel application.
mariogallegos.com
Other
69 stars 44 forks source link

extendeble export #52

Closed KrasilnikovKB closed 7 years ago

KrasilnikovKB commented 7 years ago

Now we can create a method export_to_xls (or other ['exportFormat']) in our implementation RepositoryInterface/EloquentRepositoryAbstract and describe it to export data process

All data were obtained in an array ['rows', 'postedData']

KrasilnikovKB commented 7 years ago

For example, i instal PhpOffice : # composer require phpoffice/phpword

and wrote method:

    public function export_to_xls($data){

        $human_readable_filename = 'Stat_for_telphin.docx';
        $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('../resources/tpl/PhoneStatForTelphin_tpl.docx');
        $templateProcessor->setValue('date_time', date('d.m.Y H:i:s'));

        $templateProcessor->cloneRow('name', count($data['rows']));
        for($i = 0; $i < count($data['rows']); $i++){
            $templateProcessor->setValue('name#'.($i+1), $data['rows'][$i]['name']);
            $templateProcessor->setValue('summa#'.($i+1), $data['rows'][$i]['summa']);
            $templateProcessor->setValue('nds#'.($i+1), $data['rows'][$i]['nds']);
        }

        $newDoc = '/tmp/PhoneStatForTelphin_tpl'.(Str::random(10)).'.docx';
        $templateProcessor->saveAs($newDoc);

        header('Pragma: no-cache');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
        header('Content-Disposition: attachment; filename='.$human_readable_filename.';');
        header('Content-Transfer-Encoding: binary');
        header('Content-Length: '.filesize($newDoc));
        readfile($newDoc);
        session_write_close();

        unlink($newDoc);
        exit;

    }

now i can download report in docx-format