当前位置:网站首页>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/
边栏推荐
- 【经验分享】如何为visio扩展云服务图标
- 【p2p】本地抓包
- 07_ Handout on the essence and practical skills of text measurement and geometric transformation
- Solve could not find or load the QT platform plugin "xcb" in "
- 【性能压测】如何做好性能压测?
- The configuration that needs to be modified when switching between high and low versions of MySQL 5-8 (take aicode as an example here)
- C language (high-level) data storage + Practice
- Rxjs - observable doesn't complete when an error occurs - rxjs - observable doesn't complete when an error occurs
- Leetcode-226. Invert Binary Tree
- JSON introduction and JS parsing JSON
猜你喜欢

Redis data migration

After 95, Alibaba P7 published the payroll: it's really fragrant to make up this

面试结束后,被面试官在朋友圈吐槽了......

Write CPU yourself -- Chapter 9 -- learning notes

一、Go知识查缺补漏+实战课程笔记 | 青训营笔记

我理想的软件测试人员发展状态

Implementing data dictionary with JSP custom tag

Jenkins远程构建项目超时的问题

IO stream file

BGP experiment (1)
随机推荐
聊聊异步编程的 7 种实现方式
Gslx680 touch screen driver source code analysis (gslx680. C)
How do I get the last part of a string- How to get the last part of a string?
pytorch 参数初始化
[webrtc] m98 Screen and Window Collection
Write CPU yourself -- Chapter 9 -- learning notes
Outlier detection technology of time series data
解决could not find or load the Qt platform plugin “xcb“in ““.
Redis data migration
Solve could not find or load the QT platform plugin "xcb" in "
2、 Concurrent and test notes youth training camp notes
面试官:你都了解哪些开发模型?
《动手学深度学习》(四) -- 卷积神经网络 CNN
Example of Pushlet using handle of Pushlet
nacos
抽絲剝繭C語言(高階)數據的儲存+練習
JS plot flot application - simple curve
测试周期被压缩?教你9个方法去应对
After the interview, the interviewer roast in the circle of friends
一、Go知识查缺补漏+实战课程笔记 | 青训营笔记