tables export into to csv files

Published 1 week ago by mstdmstd

Hello, I want some my tables export into to csv files and I get this http://www.maatwebsite.nl/laravel-excel/docs/export#export library

and I see :

Excel::create('Filename', function($excel) {

})->export('xls');

But what I missed is how to set list of headers and data array itself? Has it some useful options?

Thanks!

Best Answer (As Selected By mstdmstd)
RamjithAp

Use this

  public static function writeArrayToCsvFile(array $dataArray) : int
    {
        $path = public_path('uploads/csv');
        $filename= 'res.csv';
    
        Excel::create($filename, function($excel) use($dataArray) {

            $excel->sheet('file', function($sheet) {
//                global  $dataArray;
                $sheet->fromArray( $dataArray );
            });

        })->store('xls', $path)->export('xls');
        return 1;
    }

tykus
tykus
1 week ago (373,510 XP)

By default it will use your array keys or model properties as first row.

The docs cover all options

mstdmstd

Thank you for the feedback. I made a wrapper function like:

    public static function writeArrayToCsvFile(array $dataArray) : int
    {
        $filename= '/_wwwroot/lar/csvp/public/uploads/csv/res.csv';
        $_SESSION['dataArray']= $dataArray;
        Excel::create($filename, function($excel) {

            $excel->sheet('file', function($sheet) {
//                global  $dataArray;
                $sheet->fromArray( $_SESSION['dataArray'] );
            });

        })->export('csv');
        return 1;
    }

it works, but

  1. How to set $dataArray parameter inside of
            $excel->sheet(

block, as inside this block $dataArray parameter is not accessible and global does not work in php7 ? I used session variable, but I do not think that is the best way.

  1. I set to $filename variable full path I expected the resulting file would be saved, but the file was downloaded as /home/serge/Downloads/-_wwwroot-lar-csvp-public-uploads-csv-res.csv.csv I did not find right options...
RamjithAp

To know more about save & export read this http://www.maatwebsite.nl/laravel-excel/docs/export

mstdmstd

Thank you, I tried to make your way but got error https://imgur.com/a/jqmwe How to fix it ?

RamjithAp

Use this

  public static function writeArrayToCsvFile(array $dataArray) : int
    {
        $path = public_path('uploads/csv');
        $filename= 'res.csv';
    
        Excel::create($filename, function($excel) use($dataArray) {

            $excel->sheet('file', function($sheet) {
//                global  $dataArray;
                $sheet->fromArray( $dataArray );
            });

        })->store('xls', $path)->export('xls');
        return 1;
    }

mstdmstd

Thanks for you help! But one more(I hope last) question: I upload 1 file ok, but as I need to upload several tables any to the reletive file I run the exporting function in a circle and only the first file is uploaded. Is it restriction of the browser(I tried in chromium, firefox) or if is the way to upload all files?

mstdmstd

With methods mentions above generated csv file is uploaded through browsers. If there is a way just to write this csv file to disk without uploading?

mstdmstd

I still search for decision to upload several csv files to my disk.

  1. If there is some way to make kind of buffering ( like ob_start ) of output data and write them to the files manually?

  2. If there is some other toll that could do this?

Please sign in or create an account to participate in this conversation.