PHP_XLSXWriter 导入/导出大量数据
小白
2020-08-26
【thinkphp5.1】
250000行 7s时间
PHP_XLSXWriter导出大数据
rows | time | memory |
---|---|---|
50000 | 1.4s | 0MB |
100000 | 2.7s | 0MB |
150000 | 4.1s | 0MB |
200000 | 5.7s | 0MB |
250000 | 7.0s | 0MB |
参考github 地址 https://github.com/mk-j/PHP_XLSXWriter 文档
博客实例如下
https://www.cnblogs.com/AbbyXie/p/9723540.html
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=
最新发布
![](/template/home/images/h1.jpg)
- 守护进程-kill端口-输出日志-查看等!
- git 本地分支关联远程分支,合并!
- pytorch安装(含cuda、cudnn安装教程)!
- linux系统下ubuntu22.04安装Docker方法!
- 视频截取封面 =php-ffmpeg/php-ffmpeg!
- 常用实例2023-5-10!
- 富文本 塞入mysql 报错: General error: 1366 Incorrect string value: ‘\xF0\x9F\x98\x84‘ for column ‘content‘!
- 百度地图拖拽获取地址实例!
- php使用smtp鉴权方式发送邮件 插件PHPMailer!
- 有道翻译接口对接!
最热排行
![](/template/home/images/h1.jpg)