laravel Excel 導入導出

"maatwebsite/excel": "~2.1.0",

版本2.x.x 使用方法,注意3.x.x 不適用

use Excel;

導出:

$cellData[] = [ '商品編碼','調撥數量'];  //表頭

// fnsku 轉成sku
foreach ($products as $pro) {
    $cellData[] = [
         $sku_arr[$pro['product_sn']],
         $pro['exp_piece_qty'],
    ];
}
Excel::create('excel名稱',function($excel) use ($cellData){
    $excel->sheet('sheet名稱', function($sheet) use ($cellData){
        $sheet->rows($cellData);
    });

})->export('xls');
 

 

導入:

Excel::load($productsExcel, function($reader) use (&$inbound){
            $data = $reader->all();
            $d = $data->toArray();

            if (!@$d[0]['sku']) {  //if more than one sheet,just select the  first sheet
                $d = $d[0];
            }
//                dd($d);return;
            foreach ($d as $v) {
                if ( !$v['sku']) {  //判斷是否有空行
                    continue;
                }
                $PkList[] =  $v;

            }
            foreach ($PkList as $v) {
                $products[] = [
                    'product_sn' => $v['sku'],
                    'exp_carton_qty' => $v['ctn'],
                    'exp_piece_qty' => $v['ctn'] * $v['qty_ctn'],
                    'container_max_qty' => $v['qty_ctn']
                ];
            }
            $inbound['products'] = $products;
        });

多個sheet

\Excel::create('第三方庫存數據',function($excel) use ($cellDataFba, $cellDataTwusa){
    $excel->sheet('Fba', function($sheet) use ($cellDataFba){
        $sheet->rows($cellDataFba);
    });
    $excel->sheet('twusa', function($sheet) use ($cellDataTwusa){
        $sheet->rows($cellDataTwusa);
    });

})->export('xls');

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章