大的 xls 文件,在服务器操作,一般小的 VPS 难抗住,因此一般会在强大的机器上转为数组文件,再做下一步的数据处理,会省很多内存, xls 文件的转换确实很非内存,不信你可以试试。
现在就基于 phpspreadsheet 进行,据说还有更省资源的 package ,这里我试了,能满足我的需求,就先不折腾了。文章源自陈学虎-https://chenxuehu.com/article/2019/08/7479.html
首先安装文章源自陈学虎-https://chenxuehu.com/article/2019/08/7479.html
composer require phpoffice/phpspreadsheet
文章源自陈学虎-https://chenxuehu.com/article/2019/08/7479.html
然后就导入文章源自陈学虎-https://chenxuehu.com/article/2019/08/7479.html
use PhpOffice\PhpSpreadsheet\IOFactory;
文章源自陈学虎-https://chenxuehu.com/article/2019/08/7479.html
嗯,我简单粗暴的转换,全部转换,简单省事:文章源自陈学虎-https://chenxuehu.com/article/2019/08/7479.html
private function ExceltoArray($path,$outfile) { $starttime = microtime("true"); $inputFileType = 'Xls'; $outFilename = $outfile; $reader = IOFactory::createReader($inputFileType); $reader->setReadDataOnly(true); $spreadsheet = $reader->load($path); $ActiveSheet=$spreadsheet->getActiveSheet(); $sheetData = $ActiveSheet->toArray(null, true, true, false); array_shift($sheetData); if(file_exists($outFilename)) unlink($outFilename); $writeContent = "<?php return " . var_export($sheetData,true) . ";"; $fileSize = file_put_contents($outFilename,$writeContent); $fileSize = $fileSize ? $fileSize : 0; unlink($path); return ['filesize'=>round($fileSize/1024/1024,2),'time'=>round(microtime("true") - $starttime,2)]; }
文章源自陈学虎-https://chenxuehu.com/article/2019/08/7479.html
文章源自陈学虎-https://chenxuehu.com/article/2019/08/7479.html
文章源自陈学虎-https://chenxuehu.com/article/2019/08/7479.html 文章源自陈学虎-https://chenxuehu.com/article/2019/08/7479.html
评论