Export MySQL database data to Excel

0

I'm running a post that sends a string with all the titles and fields of a database, however it only exports when I send up to 4 months. I would like to be able to submit up to 1 year, how could I improve the code below to create this file?


    $filename = $_POST['filename'].".xls";
    header('Pragma: public');
    header('Expires: 0');
    header("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT");
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Content-Type: application/force-download');
    header('Content-Type: application/octet-stream');
    header('Content-Type: application/download');
    header('Content-Disposition: attachment;filename=' . $filename);
    print_r($_POST['body']);

This is the method you export:


 public function exportGlobal($post)
    {
        $query = null;

        $startDate = $post['dataInicial'];
        $endDate = $post['dataFinal'];

        $sql = "SELECT
                  'distribuidor'.'dst_nome',
                  'filial'.'fll_nome',
                  'usuario'.'usr_nome', 
                  'venda'.'vnd_valor',
            FROM
                  'venda'
            LEFT JOIN 'usuario'      ON ('venda'.'usr_id' = 'usuario'.'usr_id')
            LEFT JOIN 'cliente'      ON ('venda'.'clt_id' = 'cliente'.'clt_id')
            LEFT JOIN 'distribuidor' ON ('distribuidor'.'dst_id' = 'venda'.'dst_id')
            LEFT JOIN 'filial'       ON ('distribuidor'.'dst_id' = 'filial'.'dst_id')
                WHERE venda.vnd_data
                BETWEEN '$startDate' AND '$endDate'
                  ;";

        $query = $this->executeQuery($sql);
        if ($query != null) {

            $exportBody = array();
            $titulos = array(
                "Distribuidor",
                "Filial do Distribuidor",
                "Vendedor",
                "Valor"
            );
    $exportBody[] = implode("\t", $titulos);
    for ($i = 0; $i < count($query); $i++) {
                $data = array(
                    $query[$i]['dst_nome'],
                    $query[$i]['fll_nome'],
                    $query[$i]['usr_nome'],
                    $query[$i]['vnd_valor'],
                );

                $data = removeTagsSaltosDeLinha($data);

                $exportBody[] = implode("\t", $data);
     }
     $exportBody = implode("\n", $exportBody);
     return str_replace('"', "", $exportBody);
}

    
asked by anonymous 02.07.2015 / 21:59

1 answer

1

Do not you think it's best to use your own class for this? I recommend the PHPExcel class , which is easy to work with and quite complete. In it I can export thousands of records without complications.

    
06.07.2015 / 14:23