当前位置:网站首页>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 three years, abandoned
- After the interview, the interviewer roast in the circle of friends
- 面试结束后,被面试官在朋友圈吐槽了......
- Implementing data dictionary with JSP custom tag
- Tianqing sends instructions to bypass the secondary verification
- 毕设-基于SSM大学生兼职平台系统
- 电商常规问题part1
- Mysql高低版本切换需要修改的配置5-8(此处以aicode为例)
- [UTCTF2020]file header
- 普通测试年薪15w,测试开发年薪30w+,二者差距在哪?
猜你喜欢
随机推荐
微信小程序中的路由跳转
I failed in the postgraduate entrance examination and couldn't get into the big factory. I feel like it's over
按键精灵脚本学习-关于天猫抢红包
Leetcode-543. Diameter of Binary Tree
JS get all date or time stamps between two time stamps
IO流 file
buuctf misc USB
leetcode:105. 从前序与中序遍历序列构造二叉树
[cloud native] how to give full play to memory advantage of memory database
L'externalisation a duré trois ans.
4、 High performance go language release optimization and landing practice youth training camp notes
外包干了三年,废了...
The configuration that needs to be modified when switching between high and low versions of MySQL 5-8 (take aicode as an example here)
知识点滴 - 关于苹果认证MFI
BGP experiment (1)
IPv4 exercises
Implementing data dictionary with JSP custom tag
leetcode:105. Constructing binary trees from preorder and inorder traversal sequences
Deep learning Flower Book + machine learning watermelon book electronic version I found
Six methods of flattening arrays with JS