当前位置:网站首页>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/
边栏推荐
- 通信设备商,到底有哪些岗位?
- Outsourcing for four years, abandoned
- 242. Bipartite graph determination
- misc ez_usb
- 毕设-基于SSM大学生兼职平台系统
- 2022-07-06:以下go语言代码是否会panic?A:会;B:不会。 package main import “C“ func main() { var ch chan struct
- leetcode:105. 从前序与中序遍历序列构造二叉树
- 在线直播系统源码,使用ValueAnimator实现view放大缩小动画效果
- After the interview, the interviewer roast in the circle of friends
- 【性能压测】如何做好性能压测?
猜你喜欢
[webrtc] M98 screen and window acquisition
Tencent's one-day life
nacos
Calculus key and difficult points record part integral + trigonometric function integral
【斯坦福计网CS144项目】Lab3: TCPSender
Outsourcing for four years, abandoned
Leetcode-226. Invert Binary Tree
[2022 ACTF]web题目复现
Dynamics CRM server deployment - restore database prompt: the database is in use
idea添加类注释模板和方法模板
随机推荐
毕设-基于SSM大学生兼职平台系统
URP - shaders and materials - light shader lit
深度学习花书+机器学习西瓜书电子版我找到了
科技云报道:从Robot到Cobot,人机共融正在开创一个时代
How do I get the last part of a string- How to get the last part of a string?
Invalid table alias or column reference`xxx`
About some details of final, I have something to say - learn about final CSDN creation clock out from the memory model
Tianqing sends instructions to bypass the secondary verification
【Liunx】进程控制和父子进程
Deep learning Flower Book + machine learning watermelon book electronic version I found
按键精灵脚本学习-关于天猫抢红包
3、 High quality programming and performance tuning practical youth training camp notes
What is the difference between TCP and UDP?
外包干了三年,废了...
抽絲剝繭C語言(高階)數據的儲存+練習
C language (high-level) data storage + Practice
Iterable、Collection、List 的常见方法签名以及含义
直播平台源码,可折叠式菜单栏
leanote私有云笔记搭建
Tongda injection 0day