Home  >  Article  >  Backend Development  >  Is there any way in PHP to export 100,000 records to Excel at one time?

Is there any way in PHP to export 100,000 records to Excel at one time?

ringa_lee
ringa_leeOriginal
2018-05-14 14:04:232644browse

Is there any way in PHP to export 100,000 records to Excel at one time?

Reply content:

Is there any way in PHP to export 100,000 records to Excel at one time?

Use MYSQLI_USE_RESULT unbuffered query, a long stream, get it from MySQL one by one, in the loop fetch_row, fputcsv($fp, $row) writes data to the file one by one, and then provides it to the user for download. The code logic is roughly as follows:

query($sql, MYSQLI_USE_RESULT); //无缓冲查询
while ($row = $result->fetch_row()) { //逐条读取(无缓冲查询)
    fputcsv($fp, $row); //逐条写入
}
fclose($fp);
header('Content-Disposition: attachment; filename="file.csv"');
readfile('file.csv');

You are normal Just download it.

PHPExcel supports line caching, just set up a file cache.
https://github.com/PHPOffice/...

Writing in batches, exporting 100,000 records at one time and writing is prone to timeout. It is recommended to test based on the performance of the server and the size of the content.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn