Bonjour,

Je voulais savoir comment faire un export CSV d'une table avec CakePHP

Merci d'avance

2 réponses


charlie404
Auteur
Réponse acceptée

J'ai trouvé.

public function admin_export() {
        $this->layout = 'csv';

        $contentCsv = '';
        $fileName = 'export-_'.date('d-m-Y').'.csv';
        $results = $this->Company->find('all',array('order' => 'created DESC','fields' => array('name','first-name','society','phone','email','function','address','zip','city')));
        $i = 0;
        foreach ($results as $k => $v) { $v = current($v); 
            $i++;
            if($i == 1)
            {
                foreach ($v as $kk => $vv) {
                    $contentCsv .= trim($kk).';';
                }
                $contentCsv = rtrim($contentCsv, ';');
                $contentCsv .= "\n";
            }

            foreach($v as $kk => $vv) {
                $contentCsv .= trim($vv).';';
            }
            $contentCsv = rtrim($contentCsv, ';');
            $contentCsv .= "\n";
        }   
        header("Content-disposition: attachment; filename=".$fileName);
        header("Content-Type: application/force-download");
        header("Content-Transfer-Encoding: application/vnd.ms-excel\n");
        header("Pragma: no-cache");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0, public");
        header("Expires: 0");
        echo $contentCsv;
        exit();     
    }

Tu génère un fichier avec des , :D