Export database tables to excel or csv, with php and require a maximum of 50, 000 data per page

such as title:

I found a lot of information on the Internet and found that some use extension files, but do not clearly explain how to use them, and some have no paging effect

so I hope someone can help me here. Thank you

Mar.06,2021

excel uses this phpexcel

as for data, you need to write 1000 or 2000 pieces of data to excel files at a time. If there is too much data at a time, it will lead to errors caused by memory overflow. One file per 1000 bits will be fine, and 50000 files will be written at a time. This practice is not supported (unless your server is very good)


use CSV, and then use buffering to write 1m of data at a time until 5W items are filled with


$offset = 0;
$limit = 1000;
$fp = fopen('export.csv','wb');
do {
    $list = $db->offset($offset)->limit($limit)->all(); // 
    foreach($list as $row) {
        $line = sprintf('%s,%s,%s,%s\n',$row['name'],$row['phone'],$row['company'],$row['position']); // csv
        fwrite($fp,$line);
    }
    if(count($list)<$limit) { // 
        break;
    }
    $offset += $limit; // offset
}while(true);
.
Menu