php数据导出插件phpspreadsheet内存优化

news/2024/7/21 5:59:38 标签: excel, thinkphp, php, 后端, 缓存

实际应用项目:http://github.crmeb.net/u/long

设置缓存

节省内存: https://phpspreadsheet.readthedocs.io/en/latest/topics/memory_saving/#memory-saving

这里我们使用


use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Psr16Cache;
... 
$adapter = new FilesystemAdapter('excel', 60*60*10, storage_path('framework/cache/data'));
$cache = new Psr16Cache($adapter);
\PhpOffice\PhpSpreadsheet\Settings::setCache($cache);

指定读取工作表

因为默认的情况下我们只需要读取某个工作表,如果您有多个工作表,但不需要全部加载,则可以使用setLoadSheetsOnly()方法来限制Reader将加载的工作表。加载单个命名工作表

$inputFileType = 'Excel5'; 
$inputFileName = './sampleData/example1.xls';
$sheetname = 'Data Sheet #2'; 
/**  Create a new Reader of the type defined in $inputFileType  **/
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
/**  Advise the Reader of which WorkSheets we want to load  **/ 
$objReader->setLoadSheetsOnly($sheetname); 
/**  Load $inputFileName to a PHPExcel Object  **/
$objPHPExcel = $objReader->load($inputFileName);

或者可以通过传递名称数组来一次调用setLoadSheetsOnly()来指定多个工作表


$inputFileType = 'Excel5'; 
$inputFileName = './sampleData/example1.xls';
$sheetnames = array('Data Sheet #1','Data Sheet #3'); 
/** Create a new Reader of the type defined in $inputFileType **/ 
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
/** Advise the Reader of which WorkSheets we want to load **/ 
$objReader->setLoadSheetsOnly($sheetnames); 
/**  Load $inputFileName to a PHPExcel Object  **/
$objPHPExcel = $objReader->load($inputFileName);

循环读取excel的时候,断开sheet的链接

$m = memory_get_usage();
dump("初始化内存: " . $m);

$f = '/www/wwwroot/trmk_service/storage/app/excel/app_75535bab9a72a/2021-04-07-feedback.xls';
$f2 = '/www/wwwroot/trmk_service/storage/app/excel/app_75535bab9a72a/2021-04-08-feedback.xls';
$f3 = '/www/wwwroot/trmk_service/storage/app/excel/app_75535bab9a72a/2021-04-09-feedback.xls';

$s = \PhpOffice\PhpSpreadsheet\IOFactory::load($f);
$s1 = memory_get_usage();
$s->disconnectWorksheets();
unset($s);
dump("内存使用率 s1: " . $s1);

$s = \PhpOffice\PhpSpreadsheet\IOFactory::load($f2);
$s2 = memory_get_usage();
$s->disconnectWorksheets();
unset($s);
dump("内存使用率 s2: " . $s2);

$s = \PhpOffice\PhpSpreadsheet\IOFactory::load($f);
$s3 = memory_get_usage();
$s->disconnectWorksheets();
unset($s);
dump("内存使用率 s3: " . $s3);


$s = \PhpOffice\PhpSpreadsheet\IOFactory::load($f3);
$s4 = memory_get_usage();
$s->disconnectWorksheets();
unset($s);
dump("内存使用率 s4: " . $s4);

# 输出
> "初始化内存: 20429136"
> "内存使用率 s1: 25156120"
> "内存使用率 s2: 25264104"
> "内存使用率 s3: 25213496"
> "内存使用率 s4: 25234112"
> "exit"

参考

https://stackoverflow.com/questions/4817651/phpexcel-runs-out-of-256-512-and-also-1024mb-of-ram


http://www.niftyadmin.cn/n/908758.html

相关文章

字符编码的过滤器Filter(即输入的汉字,能在页面上正常显示,不会出现乱码)...

自定义抽象的 HttpFilter类, 实现自 Filter 接口 package com.lanqiao.javaweb;import java.io.IOException;import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servl…

数据库编程之mariadb/mysql建立主从

这里给大家推荐一款免费迭代 二开便捷的商城项目:源码直通车>>> 前提 本方案是两节点主从方案,只要建立好主从,及时数据库挂掉又拉起主从模式不会失效。 保证时间同步保证都安装了mysql/mariadb 建立主从的过程 这里介绍的是两节…

WebApp:如何让安卓的webview缓存webapp的html、js和图片等资源

一、开发环境客户端:安卓webview(vuejs)服务器端:tomcat 8.0二、问题使用安卓原生web(基于webpackvuejs)的方式开发了一个安卓应用,由于web的js文件较大,大概有400k左右,…

php如何自动刷新页面

实际应用项目&#xff1a;http://github.crmeb.net/u/long 1、首先&#xff0c;打开php编辑器&#xff0c;新建php文件&#xff0c;例如&#xff1a;index.php。 2、在index.php中&#xff0c;输入代码&#xff1a;echo ("<script>setTimeout(window.location.reloa…

JS 语言的Function 解析

1.最基本的作为一个本本分分的函数声明使用。 复制代码代码如下:function func(){} 或 var funcfunction(){}; 2.作为一个类构造器使用&#xff1a; 复制代码代码如下:function class(){} class.prototype{}; var itemnew class(); 3.作为闭包使用&#xff1a; 复制代码代码…

PHP实现微信小程序支付

实际应用项目&#xff1a;http://github.crmeb.net/u/long 一、前端代码: 1.先写一个可以点击支付的按钮 <view class"chongzhi_button" bindtap"pay">支付</view> 2.开始js代码 pay: function(){//下订单并唤起微信支付var price 0.01; …

1-5-43:质因数分解

总时间限制: 1000ms内存限制: 65536kB描述已知正整数 n 是两个不同的质数的乘积&#xff0c;试求出较大的那个质数。 输入输入只有一行&#xff0c;包含一个正整数 n。对于60%的数据&#xff0c;6 ≤ n ≤ 1000。对于100%的数据&#xff0c;6 ≤ n ≤ 2*10^9。输出输出只有一行…

微信小程序生成带参数的二维码以及小程序码

实际应用项目&#xff1a;http://github.crmeb.net/u/long 微信小程序生成带参数的二维码 官方共给了三个接口调用&#xff0c;大家可以根据自己的实际情况来使用&#xff0c;我这里使用的是接口B和接口C。官方文档地址 业务需求&#xff1a; 扫描二维码进入指定商品页面&…