php導出excel(xls或xlsx)


$titles                = array('訂單號','商品結算碼','合同號','供應商名稱','專櫃','商品名稱','商品貨號','商品單價','商品總價','供應商結算金額','商品數量','商品促銷優惠','平台優惠抵扣','品牌訂單優惠抵扣');

//導出准備
ob_get_clean();
ob_start();
echo implode("\t", $titles),"\n";

$currencyModel      = app::get('ectools')->model('currency');
foreach ($lists as $key=>$value) {
    $row                             = array();
    $row['order_id']                = html_entity_decode("".$value['order_id']);
    $row['supplier_num']            = $value['supplier_num'];
    $row['agreement_code']            = $value['agreement_code'];
    $row['supplier_name']            = $value['supplier_name'];
    $row['shoppe_name']                = $value['shoppe_name'];
    $row['name']                    = $value['name'];
    $row['bn']                        = $value['bn'];
    $row['price']                   = $value['price'];
    $row['nums']                    = $value['nums'];
    $row['g_price']                 = $value['g_price'];
    $row['settlement_amount']       = $value['settlement_amount'];
    $row['goods_amount_off']         = $value['goods_amount_off'];
    $row['amount_off']                 = $value['amount_off'];
    $row['brand_amount_off']         = $value['brand_amount_off'];

    echo implode("\t", $row),"\n";
}

header('Content-Disposition: attachment; filename='.$filename);
header('Accept-Ranges:bytes');
header('Content-Length:' . ob_get_length());
header('Content-Type:application/vnd.ms-excel');
ob_end_flush();

2)格式選擇

需要導出xls的話,用

header('Content-Type:application/vnd.ms-excel');

需要導出xlsx的話,用

header('Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

xls和xlsx的區別的話,是儲存數據量大小的問題,xls只可以保存大概5、6w數據,xlsx可以100w左右

3)長數字完整顯示解決辦法

$row['order_id']                = html_entity_decode("".$value['order_id']);

 

 

轉:  https://www.cnblogs.com/tujia/p/5999806.html

 

download.php下載

<?php
//獲取要下載的文件名
$filename = $_GET['filename'];
//設置頭信息
header('Content-Disposition:attachment;filename=' . basename($filename));
header('Content-Length:' . filesize($filename));
//讀取文件並寫入到輸出緩沖
readfile($filename);

 

https://www.cnblogs.com/xiaopiyuanzi/p/7243051.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM