当前位置:网站首页>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/
边栏推荐
- 知识点滴 - 关于苹果认证MFI
- After 95, the CV engineer posted the payroll and made up this. It's really fragrant
- [P2P] local packet capturing
- L'externalisation a duré trois ans.
- Leetcode sword finger offer brush questions - day 20
- 4、 High performance go language release optimization and landing practice youth training camp notes
- Mutual conversion between InputStream, int, shot, long and byte arrays
- Make a bat file for cleaning system garbage
- buuctf misc USB
- 图解GPT3的工作原理
猜你喜欢

1142_ SiCp learning notes_ Functions and processes created by functions_ Linear recursion and iteration

Leetcode-226. Invert Binary Tree
![[Linux] process control and parent-child processes](/img/4c/89f87ee97f0f8e9033b9f0ef46a80d.png)
[Linux] process control and parent-child processes

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

毕设-基于SSM大学生兼职平台系统

07_ Handout on the essence and practical skills of text measurement and geometric transformation

About some details of final, I have something to say - learn about final CSDN creation clock out from the memory model

idea添加类注释模板和方法模板

1140_ SiCp learning notes_ Use Newton's method to solve the square root

Wechat applet full stack development practice Chapter 3 Introduction and use of APIs commonly used in wechat applet development -- 3.9 introduction to network interface (IX) extending the request3 met
随机推荐
Wechat applet full stack development practice Chapter 3 Introduction and use of APIs commonly used in wechat applet development -- 3.9 introduction to network interface (IX) extending the request3 met
idea添加类注释模板和方法模板
【webrtc】m98 screen和window采集
URP - shaders and materials - light shader lit
【p2p】本地抓包
1142_ SiCp learning notes_ Functions and processes created by functions_ Linear recursion and iteration
[2022 CISCN]初赛 web题目复现
IO stream file
2、 Concurrent and test notes youth training camp notes
English translation is too difficult? I wrote two translation scripts with crawler in a rage
Jenkins remote build project timeout problem
[ANSYS] learning experience of APDL finite element analysis
07_ Handout on the essence and practical skills of text measurement and geometric transformation
The metauniverse of the platofarm farm continues to expand, with Dao governance as the core
Jenkins远程构建项目超时的问题
Mutual conversion between InputStream, int, shot, long and byte arrays
[2022 ACTF]web题目复现
[Linux] process control and parent-child processes
【斯坦福计网CS144项目】Lab3: TCPSender
Tianqing sends instructions to bypass the secondary verification