PHPExcel Timeout occur


PHPExcel Timeout occur

请帮助我解决导出大数据为excel格式xlsx的问题。我一次导出几乎45000条记录到一个文件,它超时而不保存文件。mysql select查询需要21秒来执行大数据。下面是我的代码导出数据在Excel文件中使用PHP 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_timeout等,并在我的php文件中编写了以下代码。

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

你可以把这个添加到你的脚本的顶部:

set_time_limit (0);

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

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

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

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

希望有帮助!