Skip to main content

PHP - Export MySQL data to EXCEL Example

I found an PHP function from the internet that can write data into true excel not just csv, and i adopted it into my project to put data from MySQL. Thanks to that site (i forgot the link).

1. download this file excel_function.php.

2. in your php file add this code

<?php
    
    function cleanData($str) {
        $str = preg_replace("/\t/", "\\t", $str);
        $str = preg_replace("/\r?\n/", "\\n", $str);
        if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
        return $str;
    }
   
    include('excel_function.php');

    $query = "SELECT * FROM tablename";


    if ($query){  

        include ('connect.php'); //connect to database
        

        $queryGenerate = $mysqli->query($query);
        if ($queryGenerate){                               
            $num = $queryGenerate->num_rows;
            if ($num>0){

                $current_date = date('Y-m-d');
                $filename ="{$current_date}.xls";


                 // Send Header
                header("Pragma: public");
                header("Expires: 0");
                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}");                 header("Content-Transfer-Encoding: binary ");
               
              
               
                xlsBOF();
                xlsWriteLabel(0,0,"Gatepass No.");
                xlsWriteLabel(0,1,"QTY");
                xlsWriteLabel(0,2,"UOM");
                xlsWriteLabel(0,3,"Item");
                xlsWriteLabel(0,4,"Item");
                xlsWriteLabel(0,5,"Date");


                $xlsRow = 1;
               
                while($rows=$queryGenerate->fetch_object()){
                     xlsWriteLabel($xlsRow,0,$rows->gt);
                     xlsWriteNumber($xlsRow,1,$rows->qty);
                     xlsWriteLabel($xlsRow,3,$rows->uom);
                     xlsWriteLabel($xlsRow,4,$rows->description);
                     xlsWriteLabel($xlsRow,5,$rows->d);
                     

                     $xlsRow++;
                }
               
                xlsEOF(); 


             }
         }

         $mysqli->close();
    }     

?>

Comments

Popular posts from this blog

Bundy Clock Quick Setup Manual for IWATA TIME RECORDER CW-1A, CW-2A, CW-2SA

Recently, I bought IWATA bundy clock for our office and I'm happy because there is a Quick Setup Manual included in the package which is hard for me to find/download from the internet. Then I realized that it might be useful in the future so I decided to upload the manual here. Thanks,

Bundy Clock Set Time (IWATA CW-1A)

if the time is advance    * Press "HO" button once to stop the timer    * Press "HO" button again to start the timer if the time is late    * Press "SY" to add 1 minute UPDATE: I have already a copy of Quick Setup Manual for IWATA Bundy Clock. CLICK HERE

(MYPHONE Ocean Mini) Stuck on Start Logo problem

Option 1: Hard Reset Press Volume UP + Volume Down + Power Button Select Recovery Mode Select Recovery Mode Again Wipe Data/Factory Reset Reboot the Phone If you encountered an error from the above procedure, try option #2 below. Option 2: Firmware Update Download needed files below on your computer and extract into a folder.     Mediatek MT65xx Preloader Driver     SP-Flash Tool v5.1352.01     MyPhone Agua Ocean Mini firmware  or this one  version 1.08 Connect you phone to your computer. ( Open Device Manager. Right-click "MT65XX..." and click "Update Driver Software". Select "Browse my computer for driver software". Click Next, then Finish. Open Mediatek folder the one that you extracted earlier. Open flash_tool.exe from SP Flash Tool folder Browse Download Agent File (i.e, MTK_AllInOnce_DA.bin) from SP Flash Folder Browse Scatter-Loading file (i.e, MT6572_Android_scatter_mt...