PHPExcel 导出较大文件失败
我正在使用 PHPExcel 库基于 mysql 数据库生成 Excel 数据。 MySql 查询结果为 1,34,000 行。 Excel 在一张工作表上支持 65,536 行。因此,逻辑就像
foreach($result as $value) { $val = array_values($value); if($rowscounter < 65000) { $objPHPExcel->addRow($val,$rowscounter); } else { $active_sheet++; $objPHPExcel->createSheet(); $objPHPExcel->setActiveSheetIndex($active_sheet); $rowscounter = 1; } $rowscounter++; } // deliver header header("Content-Type: $mtype; charset=" . $objPHPExcel->sEncoding); header("Content-Type:application/octet-stream"); header("Content-Disposition: inline; filename=\"" . $filename . ".$ext\""); // Save it as an excel 2003 file $objWriter = IOFactory::createWriter($objPHPExcel,$objPHPExcel->sFileFormat); //echo "Peak memory usage: " . (memory_get_peak_usage(true) / 1024 / 1024) . " MB";exit; $objWriter->save('php://output');
在一张工作表达到 65000 条记录后创建新工作表一样。
但这不起作用;不给出任何输出或错误。最初我认为这是因为内存限制。但是当回显时,它显示峰值内存为 1400.5 MB,并且我已使用 ini_set('memory_limit', '3500M');
将内存限制设置为 3500MB
;您能否提出建议或任何替代方案?
I am using PHPExcel library to generate excel data based on mysql datbase. MySql query results in the 1,34,000 rows. And Excel Supports 65,536 Rows on one worksheet. So made logic like
foreach($result as $value) { $val = array_values($value); if($rowscounter < 65000) { $objPHPExcel->addRow($val,$rowscounter); } else { $active_sheet++; $objPHPExcel->createSheet(); $objPHPExcel->setActiveSheetIndex($active_sheet); $rowscounter = 1; } $rowscounter++; } // deliver header header("Content-Type: $mtype; charset=" . $objPHPExcel->sEncoding); header("Content-Type:application/octet-stream"); header("Content-Disposition: inline; filename=\"" . $filename . ".$ext\""); // Save it as an excel 2003 file $objWriter = IOFactory::createWriter($objPHPExcel,$objPHPExcel->sFileFormat); //echo "Peak memory usage: " . (memory_get_peak_usage(true) / 1024 / 1024) . " MB";exit; $objWriter->save('php://output');
to create new worksheet after 65000 records are reached for one woeksheet.
But it doesn't work; not giving any output or error. Initially I thought its because of memory limit. But when echoed it shows peak memory to 1400.5 MB and I have set my memory limit up to 3500MB using ini_set('memory_limit', '3500M');
Could you please suggest something or any alternative?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能超出了 65,000 的限制,您将 $rowscounter 初始值设置为多少? 1 或 0(零)?我之所以要求数组结果从索引 0(零)开始,您要添加行,然后在添加后递增计数器。因此,如果您从 0(零)开始计数器,那么您的行数可能会多于您计算的行数。另外,您在 else 语句中缺少一行,您循环遍历一个值,但不将其添加到工作表中,
尝试这
是我试图解释的一个简单示例,如果 $i 设置为 0(零)否则不满足条件。因此,您的结果中将会多出一行。如果 $i 设置为 1,则满足 else 条件
You might be exceeding the 65,000 limit, what do you set the $rowscounter initial value at? 1 or 0 (zero)? The reason I ask it that the array results starts at index 0 (zero), you're adding the row and then incrementing the counter after the add. So if you start the counter at 0 (zero) than you might have more rows that what you have counted. Also you are missing a row in the else statement, you loop through a value but don't add it to the sheet
try this
a simple example of what I'm trying to explain, if $i is set to 0 (zero) the else condition is not met. So you will have an extra row in your results. If $i is set to 1 the else condition is met