[Vuejs]-Laravel excel export works just 1 time

0👍

You can choose the PHP file handler to export CSV instead of maatwebsite/excel.

Code snippets:

    $fileDirectory = 'path';
    $fileName   = 'filename.csv';
    $file       = $fileDirectory . '/'. $fileName;
    $columns    = [
        "column1",
        "column2",
    ];
    $fp         = fopen($file, 'w');
    fputcsv($fp, $columns);
    foreach ($array as $data) {
        $row   = [];
        $row[] = $data->column1;
        $row[] = $data->column2;
        fputcsv($fp, $row);
    }
    fclose($fp);
    header('Content-Type: text/csv');
    header('Content-Disposition: attachment; filename="' . $fileName);
    header('Content-Length: ' . filesize($file));
    readfile($file);
    unlink($file);
    exit;

Modify codes to fullfill your requirements.

Leave a comment