当前位置:网站首页>php导出百万数据
php导出百万数据
2022-07-07 04:19:00 【php_kevlin】
提示:由于phpexcel和phpoffice导出数据会使内存溢出,所以使用php-Xlswriter# 前言
一、安装扩展
pecl install xlswriter
修改php.ini
extension = xlswriter.so
然后重启使php-fpm设置生效
二、使用步骤
1.引入库
代码如下(示例):
composer require viest/php-ext-xlswriter-ide-helper:dev-master
2.读入数据
代码如下(示例):
<?php
require __DIR__ . '/vendor/autoload.php';
$dbms='mysql'; //数据库类型
$host='127.0.0.1'; //数据库主机名
$dbName='employees'; //使用的数据库
$user='root'; //数据库连接用户名
$pass='123456'; //对应的密码
$dsn="$dbms:host=$host;dbname=$dbName";
//默认这个不是长连接,如果需要数据库长连接,需要最后加一个参数:array(PDO::ATTR_PERSISTENT => true) 变成这样:
$dbh = new \PDO($dsn, $user, $pass, array(PDO::ATTR_PERSISTENT => true));
ini_set('memory_limit','1024M');
set_time_limit(0);
$config = [
'path' => './',
];
$excel = new \Vtiful\Kernel\Excel($config);
// Init File
$fileName = 'emp.xlsx';
//固定内存模式
$excel = $excel->constMemory($fileName, 'sheet11');
$excel = $excel->header([ 'id', 'salary', 'first_name', 'last_name']);
for($i=5000;$i<=1000000;$i+=5000){
$start = $i-5000;
$limit = 5000;
$sql = "SELECT * FROM salaries order by emp_no asc limit {
$start},{
$limit}";
//$sql = 'SELECT * FROM employees order by emp_no asc limit 10';
//var_dump($sql);die();
//$sql = "SELECT * FROM salaries limit 10";
$sth = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$sth->execute();
$emps = $sth->fetchAll(PDO::FETCH_NUM);
//var_dump(count($emps));die();
//var_dump($emps);die();
$excel = $excel->data($emps);
}
// Output
$filePath = $excel->output();
结果
参考文章:http://www.884358.com/php-xlswriter
文档地址:https://xlswriter-docs.viest.me/zh-cn/an-zhuang/
边栏推荐
- misc ez_usb
- 微博发布案例
- Interviewer: what development models do you know?
- 【Liunx】进程控制和父子进程
- Differences between H5 architecture and native architecture
- Convolutional neural network -- understanding of pooling
- Dynamics CRM server deployment - restore database prompt: the database is in use
- Gslx680 touch screen driver source code analysis (gslx680. C)
- vus. Precautions for SSR requesting data in asyndata function
- Kbu1510-asemi power supply special 15A rectifier bridge kbu1510
猜你喜欢
95后CV工程师晒出工资单,狠补了这个,真香...
Technology cloud report: from robot to Cobot, human-computer integration is creating an era
测试周期被压缩?教你9个方法去应对
Idea add class annotation template and method template
Initial experience of teambiion network disk (Alibaba cloud network disk)
Leetcode-226. Invert Binary Tree
【p2p】本地抓包
[GUET-CTF2019]虚假的压缩包
How to * * labelimg
Interviewer: what development models do you know?
随机推荐
How to * * labelimg
My ideal software tester development status
智联+影音,AITO问界M7想干翻的不止理想One
四、高性能 Go 语言发行版优化与落地实践 青训营笔记
JS plot flot application - simple curve
毕设-基于SSM大学生兼职平台系统
Wx is used in wechat applet Showtoast() for interface interaction
1142_ SiCp learning notes_ Functions and processes created by functions_ Linear recursion and iteration
[2022 ACTF]web题目复现
二、并发、测试笔记 青训营笔记
I failed in the postgraduate entrance examination and couldn't get into the big factory. I feel like it's over
【数学笔记】弧度
Leetcode-226. Invert Binary Tree
Idea add class annotation template and method template
Tianqing sends instructions to bypass the secondary verification
How do I get the last part of a string- How to get the last part of a string?
Simple example of ros2 planning system plansys2
【斯坦福计网CS144项目】Lab3: TCPSender
BGP experiment (1)
2022-07-06:以下go语言代码是否会panic?A:会;B:不会。 package main import “C“ func main() { var ch chan struct