dtpa98038 2015-10-16 10:49
浏览 69

如何使用带有数字格式的100K记录导出HTML表而不耗尽内存

I'm using PHP and MySQL to fetch the data to HTML Table. The data is supposed to download the HTML in Excel File. Following code is used:

$filename = "individual_list_" . date('Ymdhms') . ".xls";
    header("Content-type: application/vnd.ms-excel");
    header("Content-Disposition: attachment; filename=\"$filename\"");
    $html='<style>.num {mso-number-format:General;}.text{mso-number-format:"\@";}</style><table border=1>';
    $rowCount=1;
    foreach ($export_data as $key => $row) {
        if(!$header)
        {
            $header_field=array_keys($row);
            $html .='<thead><tr>';
            foreach ($header_field as $k => $value) {      
                $html .='<th class="text">'.$value.'</th>';
            }
            $html .='</tr></head>';
            $header=true;
        }
        $values=array_values($row);
        $rowCount++;
        $html .='<tbody><tr>';
        foreach ($values as $k => $value) {
            if (DateTime::createFromFormat('Y-m-d G:i:s', $value) !== FALSE) {
             $value = date('Y-m-d', strtotime($value));
            }
            $html .='<td class="text">'.$value.'</td>';
        }
        $html .='</tr></tbody>';
    }
    $html .='</table>';
    echo $html;

There are around 90K records to export. This code once produced Allowed Memory Exhausted Error, So I changed the memory limit. Now the error is resolved but the data is displayed as HTML Table instead of download. The code is working good for less recordset. How can the issue be resolved? The export(download) is done in popup that downloads the Excel file. How can the popup window be closed after download complete?

  • 写回答

1条回答 默认 最新

  • douzhantao2857 2015-10-16 12:12
    关注

    Output the html within the loop rather than buffering it until the end. This will reduce the amount of memory required by PHP and may speed up the download process. PHP & your web server will still do some buffering. This is OK but you could override with explicit flush(); commands if the issues persist.

    $filename = "individual_list_" . date('Ymdhms') . ".xls";
    header("Content-type: application/vnd.ms-excel");
    header("Content-Disposition: attachment; filename=\"$filename\"");
    
    echo '<style>.num {mso-number-format:General;}.text{mso-number-format:"\@";}</style><table border=1>';
    $rowCount=1;
    foreach ($export_data as $key => $row) {
        $html = '';
        if(!$header)
        {
            $header_field=array_keys($row);
            $html .='<thead><tr>';
            foreach ($header_field as $k => $value) {      
                $html .='<th class="text">'.$value.'</th>';
            }
            $html .='</tr></head>';
            $header=true;
        }
        $values=array_values($row);
        $rowCount++;
        $html .='<tbody><tr>';
        foreach ($values as $k => $value) {
            if (DateTime::createFromFormat('Y-m-d G:i:s', $value) !== FALSE) {
             $value = date('Y-m-d', strtotime($value));
            }
            $html .='<td class="text">'.$value.'</td>';
            echo  $html;
        }
        echo '</tr></tbody>';
        echo '</table>';
    }
    

    Depending on your MySQL client library, you may also be able to use unbuffered MySQL queries which allow your script to start generating HTML as soon as data is received from MySQL and uses less memory than waiting for the entire result to be loaded into a PHP buffer.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c