当前位置:网站首页>PHP exports millions of data
PHP exports millions of data
2022-07-07 07:47:00 【php_ kevlin】
List of articles
Tips : because phpexcel and phpoffice Exporting data will overflow memory , So use php-Xlswriter# Preface
One 、 Install the extension
pecl install xlswriter
modify php.ini
extension = xlswriter.so
Then restart to make php-fpm Settings take effect
Two 、 Use steps
1. Import and stock in
The code is as follows ( Example ):
composer require viest/php-ext-xlswriter-ide-helper:dev-master
2. Read in the data
The code is as follows ( Example ):
<?php
require __DIR__ . '/vendor/autoload.php';
$dbms='mysql'; // Database type
$host='127.0.0.1'; // Database host name
$dbName='employees'; // Database used
$user='root'; // Database connection user name
$pass='123456'; // Corresponding password
$dsn="$dbms:host=$host;dbname=$dbName";
// By default, this is not a long connection , If you need a long database connection , You need to add one last parameter :array(PDO::ATTR_PERSISTENT => true) It's like this :
$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';
// Fixed memory mode
$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();
result
Reference article :http://www.884358.com/php-xlswriter
Document address :https://xlswriter-docs.viest.me/zh-cn/an-zhuang/
边栏推荐
- Outsourcing for three years, abandoned
- 【obs】win-capture需要winrt
- Route jump in wechat applet
- [2022 CISCN]初赛 web题目复现
- L'externalisation a duré trois ans.
- URP - shaders and materials - light shader lit
- What is the difference between TCP and UDP?
- [P2P] local packet capturing
- SQL优化的魅力!从 30248s 到 0.001s
- Resource create package method
猜你喜欢
Resource create package method
idea添加类注释模板和方法模板
1142_ SiCp learning notes_ Functions and processes created by functions_ Linear recursion and iteration
SQL优化的魅力!从 30248s 到 0.001s
Simple example of ros2 planning system plansys2
URP - shaders and materials - light shader lit
Outsourcing for four years, abandoned
My ideal software tester development status
Flutter riverpod is comprehensively and deeply analyzed. Why is it officially recommended?
[guess-ctf2019] fake compressed packets
随机推荐
Common method signatures and meanings of Iterable, collection and list
nacos
探索Cassandra的去中心化分布式架构
JS get all date or time stamps between two time stamps
Few-Shot Learning && Meta Learning:小样本学习原理和Siamese网络结构(一)
外包幹了三年,廢了...
自定义类加载器加载网络Class
pytest+allure+jenkins环境--填坑完毕
pytorch 参数初始化
IO stream file
Leetcode-226. Invert Binary Tree
【斯坦福计网CS144项目】Lab3: TCPSender
解决问题:Unable to connect to Redis
今日现货白银操作建议
Jenkins remote build project timeout problem
Regular e-commerce problems part1
misc ez_ usb
The metauniverse of the platofarm farm continues to expand, with Dao governance as the core
Live broadcast platform source code, foldable menu bar
[webrtc] m98 Screen and Window Collection