当前位置:网站首页>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/
边栏推荐
- 按键精灵采集学习-矿药采集及跑图
- URP - shaders and materials - simple lit
- Is the test cycle compressed? Teach you 9 ways to deal with it
- [webrtc] m98 Screen and Window Collection
- Jenkins远程构建项目超时的问题
- 4、 High performance go language release optimization and landing practice youth training camp notes
- Rxjs - observable doesn't complete when an error occurs - rxjs - observable doesn't complete when an error occurs
- 三、高质量编程与性能调优实战 青训营笔记
- Tongda injection 0day
- Stockage et pratique des données en langage C (haut niveau)
猜你喜欢

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

misc ez_usb

Leetcode-206. Reverse Linked List

1、 Go knowledge check and remedy + practical course notes youth training camp notes

外包干了三年,废了...

2、 Concurrent and test notes youth training camp notes

3、 High quality programming and performance tuning practical youth training camp notes

Calculus key and difficult points record part integral + trigonometric function integral

通信设备商,到底有哪些岗位?

四、高性能 Go 语言发行版优化与落地实践 青训营笔记
随机推荐
Kbu1510-asemi power supply special 15A rectifier bridge kbu1510
Calculus key and difficult points record part integral + trigonometric function integral
微信小程序中的路由跳转
After the interview, the interviewer roast in the circle of friends
三、高质量编程与性能调优实战 青训营笔记
Outsourcing for three years, abandoned
The configuration that needs to be modified when switching between high and low versions of MySQL 5-8 (take aicode as an example here)
科技云报道:从Robot到Cobot,人机共融正在开创一个时代
vus. Precautions for SSR requesting data in asyndata function
【obs】win-capture需要winrt
JSON introduction and JS parsing JSON
【webrtc】m98 screen和window采集
IO流 file
[ANSYS] learning experience of APDL finite element analysis
Outlier detection technology of time series data
Detailed explanation of uboot image generation process of Hisilicon chip (hi3516dv300)
buuctf misc USB
测试周期被压缩?教你9个方法去应对
Wechat applet full stack development practice Chapter 3 Introduction and use of APIs commonly used in wechat applet development -- 3.10 tabbar component (I) how to open and use the default tabbar comp
Tencent's one-day life