laravel Excel 导入导出

本文详细介绍了如何在Laravel框架中使用Excel库进行多sheet数据的导入和导出操作,包括设置、读取及处理多个工作表的数据流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

"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');

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值