PHP_XLSXWriter 导入/导出大量数据

小白 2020-08-26 thinkphp5.1


250000行 7s时间



PHP_XLSXWriter导出大数据

rowstimememory
500001.4s0MB
1000002.7s0MB
1500004.1s0MB
2000005.7s0MB
2500007.0s0MB

参考github 地址 https://github.com/mk-j/PHP_XLSXWriter 文档


博客实例如下 

https://www.cnblogs.com/AbbyXie/p/9723540.html


<?php  include_once("xlsxwriter.class.php"); ini_set('display_errors', 0); ini_set('log_errors', 1); $writer = new XLSXWriter(); //文件名$filename = "example.xlsx";//设置 header,用于浏览器下载header('Content-disposition: attachment; filename="'.XLSXWriter::sanitize_filename($filename).'"'); header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); header('Content-Transfer-Encoding: binary'); header('Cache-Control: must-revalidate'); header('Pragma: public');//导出的数据$string = array (  0 =>  array (    'payc_bill_time' => '2017-07-12 16:40:44',    'payt_received_date' => '2017-07-12',    'ci_name' => '租金',    'payt_num' => 'YRZB(2012)A0047',    'payt_scsr_name' => '李巧红',    'payt_received' => '300.00',    'paytd_type' => '现金',    'emp_name' => '郑振标',  ),  1 =>  array (    'payc_bill_time' => '2017-07-12 16:39:55',    'payt_received_date' => '2017-07-12',    'ci_name' => '租金',    'payt_num' => 'YRZB(2012)A0046',    'payt_scsr_name' => '22222',    'payt_received' => '45.00',    'paytd_type' => '现金',    'emp_name' => '郑振标',  )  );  //每列的标题头$title = array (    0 => '开单时间',    1 => '收款时间',    2 => '开票项目',    3 => '票据编号',    4 => '客户名称',    5 => '实收金额',    6 => '收款方式',    7 => '收款人', );//工作簿名称$sheet1 = 'sheet1';//对每列指定数据类型,对应单元格的数据类型foreach ($title as $key => $item){    $col_style[] = $key ==5 ? 'price': 'string'; }//设置列格式,suppress_row: 去掉会多出一行数据;widths: 指定每列宽度$writer->writeSheetHeader($sheet1, $col_style, ['suppress_row'=>true,'widths'=>[20,20,20,20,20,20,20,20]] );//写入第二行的数据,顺便指定样式$writer->writeSheetRow($sheet1, ['测试导出'], ['height'=>32,'font-size'=>16,'font-style'=>'bold','halign'=>'center','valign'=>'center']);/*设置标题头,指定样式*/$styles1 = array( 'font'=>'宋体','font-size'=>14,'font-style'=>'bold', 'fill'=>'#eee','halign'=>'center', 'border'=>'left,right,top,bottom'); $writer->writeSheetRow($sheet1, $title,$styles1);// 最后是数据,foreach写入foreach ($string as $value) {    foreach ($value as $item) {  $temp[] = $item;}    $rows[] = $temp;    unset($temp); } $styles2 = ['height'=>16];foreach($rows as $row){    $writer->writeSheetRow($sheet1, $row,$styles2); }//合并单元格,第一行的大标题需要合并单元格$writer->markMergedCell($sheet1, $start_row=0, $start_col=0, $end_row=0, $end_col=

站点信息