PHP export large amounts of data to a csv table

For doing background development of code for farming, import data from excel to the database, or are derived also from database data to excel are very common operation. As often encounter such a scenario, because there are a lot of pit encountered export data from the database to the table, we need to open up the way for this kind of scene optimization.

If a small amount of data is exported to excel spreadsheet, then, generally not a problem much efficiency and resource consumption, but when the amount of data becomes large, such as 200,000 or 500,000 will cause a bottleneck when processing PHP, either out of memory or the script runs out. PHP is of course relative to more flexible, you can change the script timeout and memory to run alone when the method is run. But this is unhelpful, about data formats supported by sixty thousand largest general xls line, so to sixty thousand a row when he cool.

Csv subsequent access to information that can support an unlimited number of rows, as long as your computer can support open data will be able to continue filling them.

The maximum number of rows in CSV file format is no ceiling in foreign scientific data download CSV file hundreds of dozens of G G, the row number how much billions of billions of lines; but the big open CSV file editing software basically no , Excel, wps, openoffice tight editing support opens the front row 1048576; the number of rows snapde software support more, one can open the CSV data editing one or two million lines, super-fast.

Code

? <PHP 
public exportWarehouseManagementToExcel function () 
    { 
        the ini_set ( 'the max_execution_time', 300); // set timeout PHP 
        ini_set ( 'memory_limit', '2048M '); // set PHP temporary memory size allows 
        $ querySql = "SELECT * FROM fsyz_cpInventory WHERE 1 = 1 "; // export all data 
        $ queryResult = $ this-> db-> Query ($ querySql) -> result_array (); 

        // path 
        $ fileName = 'Vehicle the delivery WM all traffic stagnation data '(Ymd_His').' .csv DATE. ''; 
        $ filePath = 'Excel /' $ fileName;. 
        $ index = 0; 
        $ FP = the fopen ($ filePath, 'W'); // generate temporary files 
        chmod ($ filePath, 0777); // modify executable permissions 
        // write the data file handle fputcsv 
        $ header = array ( 'License plate number ',' plate type ',' Area ',' state ',' timespan '); // Set the header 
        fputcsv ($ fp, $ header) ;
        // handle export data
        the foreach (Key $ = $ queryResult AS> Val $ &) { 
            the foreach (Val $ $ K AS => $ V) { 
                $ Val [$ K] V $ = "\ T";. 
                IF (index == $ 10000) each write {// clear the memory data 1000 
                    $ index = 0; 
                    ob_flush (); // clear the memory 
                    the flush (); 
                } 
                $ index ++; 
            } 
            fputcsv (FP $, $ Val); 
        } 
        ob_flush (); 
        fclose ( $ fp); // close the handle 
        header ( "the Cache-Control: max-Age = 0");  
        header ( "the Content-type: file application / vnd.ms-Excel; charset = UTF -8");
        header ( "the Content-the Description: File Transfer"); 
        header ( 'the Content-Disposition: Attachment; filename =' the basename (. $ fileName));
        header ( "the Content-the Type: text / CSV"); 
        header ( "the Content-Transfer-Encoding: binary"); 
        header ( 'the Content-the Length:'. filesize ($ filePath)); 
        @readfile ($ filePath); / / output file; 
        unlink ($ filePath); // delete temporary files compressed 
        echo $ filePath; 
        return; 
    }

Reprint:

 https://199508.com/post/2064

Guess you like

Origin www.cnblogs.com/yehuisir/p/11521226.html