PHP real-time generation and download of ultra-large data volumes of Excel files

Source: Internet
Author: User
Tags openid

//in addition, since Excel data is read from the database and then written to the output stream, it is necessary to set the execution time of PHP a bit longer//(default 30 seconds) Set_time_limit (0) does not limit PHP execution time.     Set_time_limit(0); $columns= [       ' Article ID ', ' article title ', ...//' OpenID '    ]; //processing data that needs to be exported    $timeStart=Strtotime(' 2018-08-08 00:00:00 '); $timeEnd=Strtotime(' 2018-08-08 23:59:59 '); $arr= db::table (' t_wechat_user_wx4ed9e1f4e0f3eeb0 ')->select (Db::raw (' distinct OpenID '))->where (' Subscribe_time ', ' >= ',$timeStart)->where (' Subscribe_time ', ' <= ',$timeEnd); $csvFileName= ' August 8 added fan openid.xlsx '; //set up to tell the browser to download the Excel file headers    Header(' Content-description:file Transfer '); Header(' Content-type:application/vnd.ms-excel '); Header(' Content-disposition:attachment; Filename= '.$csvFileName.‘"‘); Header(' expires:0 '); Header(' Cache-control:must-revalidate '); Header(' Pragma:public '); $fp=fopen(' Php://output ', ' a ');//Open output StreamMb_convert_variables (' GBK ', ' UTF-8 ',$columns); Fputcsv ($fp,$columns);//format the data in CSV format and write to the output stream    $accessNum=$arr-Count();//get the total from the database, assuming it's 1 million    $perSize= 1000;//number of bars per query    $pages=Ceil($accessNum/$perSize); $lastId= 0;  for($i= 1;$i<=$pages;$i++) {        //data that needs to be exported        $accessLog=$arr->offset ($lastId)->limit ($perSize)->get ([' OpenID '])ToArray (); foreach($accessLog  as $access) {            $rowData= [                ......//each row of data $access->openid            ]; Mb_convert_variables (' GBK ', ' UTF-8 ',$rowData); Fputcsv ($fp,$rowData); $lastId++; }        unset($accessLog);//release the memory of the variable//refresh output buffer to the browser        Ob_flush(); Flush();//You must use both the Ob_flush () and flush () functions to flush the output buffers.     }    fclose($fp); Exit();

Related real-time output:

① real-time output by setting

Ob_end_flush ();//Close cache
Ob_implicit_flush (TRUE);//True to turn on absolute swipe each time the cache is output equivalent to call flush () after each output
Header (' X-accel-buffering:no '); Turn off output caching

② real-time output via PHP syntax

$buffer = Ini_get (' output_buffering ');
Echo str_repeat (", $buffer + 1); Prevent browser caching
Ob_end_flush (); Turn off caching
for ($i =1; $i <=10; $i + +) {
Echo ' first ' $i. ' Secondary output. ' <br/>\n ";
Flush (); Refresh cache (send directly to browser)
Sleep (1);
}
echo ' output complete ';

PHP real-time generation and download of ultra-large data volumes of Excel files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.