2013-04-26 52 views
0

請幫我解決將大數據導出爲ex​​cel格式xlsx的問題。 我每次導出幾乎45000條記錄到單個文件,並且超時而不保存文件。 mysql選擇查詢需要21秒來執行該大數據。以下是我使用PHP Excel庫將數據導出到Excel文件的代碼。PHPExcel超時發生

$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"; 

UPDATE:

我忘了提,我已經嘗試過set_timout等,並在我的PHP文件下面的代碼中寫道。

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

php.ini中有一個變量用於爲php腳本執行設置超時值[1]。如果你碰到那可能解決你的問題。 [1]:http://php.net/manual/en/function.set-time-limit.php – 2013-04-26 15:32:55

回答

0

你可以在你的腳本的頂部添加此:

set_time_limit (0); 

這將禁用默認30秒PHP超時。

或者你可以添加自定義秒數,看到set_time_limit()

+0

感謝您的回答。我更新了我的描述。我已經補充說,沒有運氣。 – Deeps 2013-04-26 15:42:26

+0

好吧,在這種情況下,如果你正在得到一個'timeout',那麼它可能是一個Apache超時(或者與其他web服務器相同),請閱讀http://www.php.net/manual/en/info.configuration .php#如果ini.max,執行時間 – Nelson 2013-04-26 15:48:07

0

我,我繼續使用PHPExcel導出記錄時得到一個504網關超時了同樣的問題。我也試過set_time_limit(0)沒有成功。我最終做的是改變Apache的超時。

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

希望它有幫助!