Home > Backend Development > PHP Tutorial > 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
Release: 2023-03-01 07:24:01
Original
2684 people have browsed it

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');
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template