Export Mysql database data to Excel

Asked

Viewed 861 times

0

I am running a post that send a string with all titles and fields of a database, but it only exports when I send up to 4 months. I wish I could send 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 export method:


 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);
}

1 answer

1


You don’t think it’s better to use your own class for this?
I recommend the class Phpexcel, that is easy to work and well complete. I can export thousands of records without complications.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.