Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

BikashKatwal's avatar

Export data to excel .csv without using maatwebsite.

How can I export data to excel .csv file without using maatwebsite.

I have to add export features to the existing project created in Laravel 4. I did it using maatwebsite, but the problem is when I deploy it to prod, it is not accepted. Shows an error with maatwebsite and when I remove it and deploy everything works. I don't what is the issue but I am also thinking of other ways to export the data. If there is any other way to export data. Is there any other way to work with it.

0 likes
4 replies
fylzero's avatar

@bikashkatwal Did you try an earlier version of the plugin? Though, Laravel 4 is probably not even supported altogether.

BikashKatwal's avatar
public function exportExcel()
    {
        $data = json_decode(json_encode(DB::select('CALL get_users_unpaid_invoices')), True);
        function cleanData(&$str)
        {
            if ($str == 't') $str = 'TRUE';
            if ($str == 'f') $str = 'FALSE';
            if (preg_match("/^0/", $str) || preg_match("/^\+?\d{8,}$/", $str) || preg_match("/^\d{4}.\d{1,2}.\d{1,2}/", $str) || preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$str)) {
                $str = " $str";
            }
            if (strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
        }

        // filename for download
        $filename = "users_unpaid_invoices_" . date('Ymd') . ".csv";

        header("Content-Disposition: attachment; filename=\"$filename\"");
        header("Content-Type: text/csv");

        $out = fopen("php://output", 'w');

        $flag = false;
        foreach ($data as $row) {
            if (!$flag) {
                // display field/column names as first row
                fputcsv($out, array_keys($row), ',', '"');
                $flag = true;
            }
            array_walk($row, __NAMESPACE__ . '\cleanData');
            fputcsv($out, array_values($row), ',', '"');
        }

        fclose($out);
    }

Please or to participate in this conversation.