PHPExcel超时发生

时间:2013-04-26 15:31:26

标签: php mysql memory timeout phpexcel

请帮我解决将大数据导出为ex​​cel格式xlsx的问题。 我一次导出近45000条记录到单个文件,它超时而不保存文件。 mysql select查询执行大数据需要21秒。下面是我使用PHP Excel库将数据导出到Excel文件的代码。

 $sql2 = "SELECT * FROM Surveys";
 $result2 = mysql_query($sql2);

while($row2 = mysql_fetch_assoc($result2))
{   
$j=$rowscounter+2;
$sheet->setCellValue("A$j",$row2['brandname']);
$sheet->setCellValue("B$j",$row2['productname']);
$sheet->setCellValue("C$j",$row2['sname']);
$sheet->setCellValue("D$j",$row2['smobile']);
$sheet->setCellValue("E$j",$row2['semail']);
$sheet->setCellValue("F$j",$row2['country']);
$sheet->setCellValue("G$j",$row2['city']);
$sheet->setCellValue("H$j",$row2['sdob']);
$sheet->setCellValue("I$j",$row2['age']);
$sheet->setCellValue("J$j",$row2['comment']);
$sheet->setCellValue("K$j",$row2['outletcode']);
$sheet->setCellValue("L$j",$row2['username']);
$sheet->setCellValue("M$j",$row2['datetime']);
$sheet->setCellValue("N$j",$row2['duration']);  
$rowscounter++;
}

 // Rename worksheet
 $sheet->setTitle('Survey-Report');    
 $objPHPExcel->setActiveSheetIndex(0);
 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
 $objWriter->setPreCalculateFormulas(false);
 unlink("Survey-Report.xlsx");
 $objWriter->save('Survey-Report.xlsx');
 echo "ok";

更新:

我忘了提到我已经尝试了set_timout等,并在我的php文件中写下了代码。

set_time_limit(0);
ini_set('memory_limit','2500M');

2 个答案:

答案 0 :(得分:1)

您可以在脚本顶部添加:

set_time_limit (0);

这将禁用默认的30秒php超时。

或者您可以添加自定义秒数,请参阅set_time_limit()

答案 1 :(得分:0)

我遇到了同样的问题,我在使用PHPExcel导出记录时继续获得504网关超时。我也试过set_time_limit(0)但没有成功。我最终做的是改变apache超时。

您可以在httpd.conf文件中找到此timout

希望它有所帮助!