繁体   English   中英

加载csv文件时出错:内存中的巨大文本节点

[英]Error loading csv file: Huge text node out of memory

当使用PHPExcel加载38MB .csv文件时,我得到了一个huge text node: out of memory不足错误,即使我将php memory_limit设置为1024M。

知道如何加载大型.csv文件吗?

错误

Error loading file "data.csv": Warning: DOMDocument::loadHTMLFile(): xmlSAX2Characters: huge text node: out of memory in /var/www/site/data.csv, line: 264094 in /var/www/site/vendor/CodePlex/PHPExcel/PHPExcel/Reader/HTML.php line 458

PHP代码

ini_set('memory_limit', '1024M');

$inputFileName = '/var/www/site/data.csv';
$phpExcel = new PHPExcel;

//  Read your Excel workbook
try 
{
    $inputFileType = PHPExcel_IOFactory::identify($inputFileName); 
    $objReader = PHPExcel_IOFactory::createReader($inputFileType);
    $objPHPExcel = $objReader->load($inputFileName);
} 
catch(Exception $e) 
{
     die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}

php.ini

; Maximum amount of memory a script may consume (128MB)
; http://php.net/memory-limit
memory_limit = 128M

您可以使用fgetcsv并逐行加载csv。

$file = '/path/filename.csv';

$f = fopen($file, 'rb');

while( $cols = fgetcsv($f) ) 
{
  // $cols[0]; // first column of the current row
}

fclose($f);

使用PHPExcel读取大量Excel文件的可能解决方案

如何使用PHPExcel从大型Excel文件(27MB +)中读取大型工作表?

答案之一是建议读取大文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM