drl6054 2018-07-19 06:50 采纳率: 0%
浏览 215

使用php和mysql导出生成的excel文件大小非常大

I am using php and mysql to generate excel file. Every thing works fine. I am using html table code and exporting data to excel file. But the downloaded file is having bigger size for 20k records it is showing nearly 35 MB. Below is my code .

<?php 
$generated_date =  date("Y-m-d-H-i-s"); 
$filename = 'order_detail_report'.$generated_date;
header("Content-Type: application/xls");    
header("Content-Disposition: attachment; filename=$filename.xls");  
header("Pragma: no-cache"); 
header("Expires: 0");
?>

Html table code

<table width="100%" border="0" cellspacing="0" cellpadding="0" >
  <tr>
    <th class="excel_style">Order Number</th>
    <th class="excel_style">EAN</th>
    <th class="excel_style">Article #</th>
    <th class="excel_style">Amazon Price</th>
    <th class="excel_style">ERP Price</th>
    <th class="excel_style">Requested Quantity</th>
    <th class="excel_style">Dispatch Quantity</th>
    <th class="excel_style">Rejected Quantity</th>
    <th class="excel_style">Cancelled Quantity</th>
  </tr>

While loop

<?php 
$query = "my select query";
$res = mysqli_query($mysqliConn,$query);
while($result= mysqli_fetch_assoc($res)){
?>
<tr>
<td class="tdstyle"><?php echo $result['order_id'];?></td>
<td class="tdstyle"><?php echo $result['ean'];?></td>
<td class="tdstyle"><?php echo $result['article_no'];?></td>
<td class="tdstyle"><?php echo $result['net_price'];?></td>
<td class="tdstyle"><?php echo $result['erp_price'];?></td>
<td class="tdstyle"><?php echo $result['requested_quantity'];?></td>
<td class="tdstyle"><?php echo $result['dispatch_quantity']?></td>
<td class="tdstyle"><?php echo $result['rejected_quanity'];?></td>
<td class="tdstyle"><?php echo $result['cancelled_quantity']?></td>
</tr>
<?php } ?>
</table>

How to solve this size of the file problem? Any help would be greatly appreciated. If I use phpexcel library I am getting some out of memory exception. So client asked me don't use phpexcel library.

  • 写回答

1条回答 默认 最新

  • duanji1902 2018-07-19 08:56
    关注

    It is not clear what is exact problem - the size of result generated file or the memory getting exhausting?

    But, lets assume that the problem is memory getting exhausting. Do you really need to generate excel(.xslx) files or CSV format is acceptable? You can generate CSV file with a really low memory usage like this

    <?php 
    $fp = fopen('file.csv', 'w');
    $query = "my select query";
    $res = mysqli_query($mysqliConn,$query);
    while($result= mysqli_fetch_assoc($res)) {
        $fields = [
            $result['order_id'],
            $result['ean'],
            $result['article_no'],
            $result['net_price'],
            $result['erp_price'],
            $result['requested_quantity'],
            $result['dispatch_quantity'],
            $result['rejected_quantity'],
            $result['cancelled_quantity']
        ]
        fputcsv($fp, $fields);
    }
    fclose($fp);
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料