当前位置:网站首页>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/
边栏推荐
- 基于Flask搭建个人网站
- Few-Shot Learning && Meta Learning:小样本学习原理和Siamese网络结构(一)
- resource 创建包方式
- Determining the full type of a variable
- 外包干了三年,废了...
- @component(““)
- [GUET-CTF2019]虚假的压缩包
- Mutual conversion between InputStream, int, shot, long and byte arrays
- [2022 actf] Web Topic recurrence
- Operation suggestions for today's spot Silver
猜你喜欢
URP - shaders and materials - light shader lit
Interviewer: what development models do you know?
A concurrent rule verification implementation
@component(““)
Resource create package method
Common method signatures and meanings of Iterable, collection and list
[ANSYS] learning experience of APDL finite element analysis
misc ez_usb
Dynamics CRM server deployment - restore database prompt: the database is in use
Few-Shot Learning && Meta Learning:小样本学习原理和Siamese网络结构(一)
随机推荐
UWB learning 1
自定义类加载器加载网络Class
JS plot flot application - simple curve
How can a 35 year old programmer build a technological moat?
Deep learning Flower Book + machine learning watermelon book electronic version I found
numpy中dot函数使用与解析
探索Cassandra的去中心化分布式架构
misc ez_usb
misc ez_ usb
四、高性能 Go 语言发行版优化与落地实践 青训营笔记
vus. Precautions for SSR requesting data in asyndata function
电商常规问题part1
leetcode:105. Constructing binary trees from preorder and inorder traversal sequences
What are the positions of communication equipment manufacturers?
vus.SSR在asynData函数中请求数据的注意事项
解决:Could NOT find KF5 (missing: CoreAddons DBusAddons DocTools XmlGui)
Operation suggestions for today's spot Silver
解决could not find or load the Qt platform plugin “xcb“in ““.
一、Go知识查缺补漏+实战课程笔记 | 青训营笔记
微信小程序中使用wx.showToast()进行界面交互