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