When PHP exports large amounts of Excel data

Source: Internet
Author: User

 Public functionCeshiexcel1 () {Set_time_limit(0); $filename= ' Virus log '; Header(' Content-type:application/vnd.ms-excel '); Header(' Content-disposition:attachment;filename= '.$filename.‘. CSV "'); Header(' Cache-control:max-age=0 '); //native link MySQL//database configuration        $mysql _conf=Array(' host ' = ' 127.0.0.1:52971 ', ' db ' = ' center ', ' db_user ' = ' root ', ' db_pwd ' and ' = '); $mysql _conn= @mysql_connect($mysql _conf[' Host '],$mysql _conf[' Db_user '],$mysql _conf[' Db_pwd ']); if(!$mysql _conn) {             die("Could not connect to the database:\n".Mysql_error());//Diagnosing Connection Errors        }        $select _db=mysql_select_db($mysql _conf[' DB ']); if(!$select _db) {             die("Could not connect to the db:\n".Mysql_error()); }        $sql= M (' Logvirus ')                    ->alias (' a ')                     -Fetchsql ()->field (' A.id,a.strvirusname,c.address,c.rmaker,a.nvirustype,a.nunknowvirustype,a.strviruspath,a.virustime,  A.nunknownflag,a.terminal,d.name as Rname, (case if A.ihigh_disktype=0 then A.ilow_disktype else A.ihigh_disktype end) As Idisktypes ')//F.idisktypes,j.name as Iname-Join(' left JOIN sysinfo c on c.terminal = A.terminal ')                    -Join(' left JOIN pm_dealresult d on d.mask = A.ndealresult ')                     //->where ($where)->order ("A.virustime desc")                    ->limit (' 1,10 ')                    -Select (); $head=Array(                    ' id ' = ' + ' number ', ' strvirusname ' + ' virus name ', ' address ' = ' ip ', ' Rmaker ' = ' + ' Alias ', ' nname ' = ' virus type ', ' iname ' + ' threat source ', ' strvir Uspath ' + ' virus path ', ' rname ' = ' processing result ', ' virustime ' = ' time ' ); $stmt=mysql_query($sql); /*echo "<pre/>";                              while ($row = Mysql_fetch_array ($stmt, Mysql_assoc)) {foreach ($row as $i = = $v) {                   $row [$i] = iconv (' utf-8 ', ' gb2312 ', $v);        Var_dump ($row); }} exit;*/        //open php file handle, php://output direct output to browser         $fp=fopen(' Php://output ', ' a '); //output Excel Column name information//$head = array (' id ', ' casenum ', ' terminal ', ' strVirusMd5 ', ' nvirustype ', ' nviruscode ', ' Noldvirus '  Code ');         foreach($head  as $i=$v) {              //CSV Excel support GBK encoding, be sure to convert otherwise garbled            $head[$i] =Iconv(' Utf-8 ', ' gb2312 ',$v); }         //writes data to a file handle via FputcsvFputcsv ($fp,$head); //counter        $cnt= 0; //every $limit line, refresh the output buffer, not too big, not too small        $limit= 100000; //row-by-line data extraction without wasting memory        $data=Array();  while($row=Mysql_fetch_array($stmt,Mysql_assoc)) {              $cnt++; if($limit==$cnt) {//Refresh the output buffer to prevent problems caused by too much data                Ob_flush();//Release                Flush(); $cnt= 0; }                                 foreach($row  as $i=$v) {                  //$row [$i] = iconv (' utf-8 ', ' gb2312 ', $v);                   $data[' id '] =$cnt; $data[' strvirusname '] =Iconv(' Utf-8 ', ' gb2312 ',$row[' Strvirusname ']); $data[' address '] =Iconv(' Utf-8 ', ' gb2312 ',$row[' Address ']); $data[' rmaker '] =Iconv(' Utf-8 ', ' gb2312 ',$row[' Virustime ']); $data[' nname '] =Iconv(' Utf-8 ', ' gb2312 ',$row[' Virustime ']); $data[' iname '] =Iconv(' Utf-8 ', ' gb2312 ',$row[' Virustime ']); $data[' strviruspath '] =Iconv(' Utf-8 ', ' gb2312 ',$row[' Virustime ']); $data[' rname '] =$row[' Rname '] ; $data[' virustime '] =Iconv(' Utf-8 ', ' gb2312 ',$row[' Virustime ']); } fputcsv ($fp,$data); }    //Close the database     Mysql_close($mysql _conn); }

When PHP exports large amounts of Excel data

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.