php exported to excel file to avoid running out of memory

 

10W easy to solve PHPExcel export line timeouts and memory overflow problem

 

Use a lightweight PHP library -PHP_XLSXWriter operation of Excel

  • 10w row excel data export requires only 5.26 seconds, no longer have to worry excel export more than 1w row overtime (max php.ini in the Execution Time)

  • Memory stack overflows, used PHPExcel students should be clear, not to line the exported file 1w line on the error, because a single PHP process more than memory_limit php.ini configuration

  • Solved style lost when csv export, long numbers in scientific notation becomes a problem, the problem of garbled

<?php
# Use lightweight excel manipulation library PHP_XLSXWriter
TimeStart $ = microtime, and ( to true ) ; require_once __DIR__ . "/../PHP_XLSXWriter/vendor/autoload.php" ; $ Writer = new new XLSXWriter ( ) ; $ sheetHeader = [ 'commodity ID' => 'String' , 'stocks ' => ' String ' , ' Price ' => ' String ' , ' name ' => ' String ' , ' seller Tel ' => ' String ' // never have to worry about long numbers in scientific notation would become a ] ; $ Writer - >writeSheetHeader('Sheet1', $sheetHeader);//optional for($i=0; $i<100000; $i++) { $s1 = $i+1; $s2 = mt_rand(0,1000); $s3 = mt_rand(100,999)/10; $s4 = "商品".$s1; $s5 = "13713147601";//随便输入的 $writer->writeSheetRow('Sheet1' , Array (S1 $ , $ S2 , S3 $ , $ S4 , S5 $ ) ) ; } $ Writer - > the writeToFile ( 'goods_info.xlsx' ) ; echo Floor ( ( memory_get_peak_usage ( ) ) / 1024 / 1024 ) . "MB" ; echo "" ; echo microtime ( to true ) - $ TimeStart ; 

Source address: https: //gitee.com/nodestudy/The-PHP-Best-Practice

This article from Sharehttps://cloud.tencent.com/developer/article/1480133

Guess you like

Origin www.cnblogs.com/shemmor/p/12532890.html