導入:簡單版
public function import(Request $request)
{
$filePath = "public" . $request->get("path");
try {
Excel::load($filePath, function ($reader) {
//獲取excel的第幾張表
$reader = $reader->getSheet(1);
//獲取表中的數據
$data = $reader->toArray();
for ($row = 3; $row < count($data); $row++) {
$food = Food::where("name", $data[$row]['1'])->first();
if (!empty($food)) continue;
$list = new Food();
$list->type = $data[$row]['2'];
$list->code = $data[$row]['0'];
$list->name = $data[$row]['1'];
$list->norm = $data[$row]['6'];
$list->save();
}
});
return response()->json(["code" => CodeUtil::SUCCESS, 'msg' => "成功"]);
} catch (\Exception $e) {
return response()->json(["code" => CodeUtil::ERROR, 'msg' => "失敗"]);
}
}
複雜版
注:下面的換了一個包,laravel官網的包導入時丟失小數(坑),轉用tp框架的包,還蠻好用的
use PhpOffice\PhpSpreadsheet\IOFactory;
public function import(Request $request)
{
$now = Carbon::now()->toDateString();
$s = Sale::where("time", $now)->first();
if (!empty($s)) return response()->json(["code" => CodeUtil::ERROR, 'msg' => "今日數據已導過"]);
$filePath = public_path($request->get("path"));
$objReader = IOFactory::createReader('Xlsx');
$spreadsheet = $objReader->load($filePath); //載入excel表格
$reader = $spreadsheet->getSheet(0)->toArray();
$reader1 = $spreadsheet->getSheet(0);
$row_num = $reader1->getHighestRow(); // 總行數
$m = 0;//獲取最後一列數
foreach ($reader[3] as $v) {
$m++;
}
try {
$list = new Sale();
$list->bill_num = $reader1->getCellByColumnAndRow($m, 5)->getValue();
$list->people_num = $reader1->getCellByColumnAndRow($m, 6)->getValue();
$list->run_price = $reader1->getCellByColumnAndRow($m, 7)->getValue();
$list->discount_price = $reader1->getCellByColumnAndRow($m, 9)->getValue();
$list->tip = $reader1->getCellByColumnAndRow($m, 10)->getValue();
$list->time = Carbon::now()->toDateString();//今天;
$list->save();
$n = 0;
for ($i = 11; $i <= $row_num; $i++) {
$name = $reader1->getCellByColumnAndRow(1, $i)->getValue();
if ($name == "優惠組成") {
$n = $i;
break;
}
}
for ($i = 11; $i < $n; $i++) {
$sale_income = new SaleIncome();
$sale_income->sale_id = $list->id;
$sale_income->name = $reader1->getCellByColumnAndRow(2, $i)->getValue();
$sale_income->sum = $reader1->getCellByColumnAndRow($m, $i)->getValue();
$sale_income->save();
$ch = Channel::where("is_del", 1)->get()->toArray();
foreach ($ch as $v) {
$name = $v['name'];
if ($name == $sale_income->name) {
$sale_tip = new SaleTip();
$sale_tip->sale_id = $list->id;
$sale_tip->name = $name;
$sale_tip->sum = $sale_income->sum * $v['rate'] / 100;
$sale_tip->save();
}
}
}
for ($i = $n; $i <= $row_num; $i++) {
$sale_dis = new SaleDiscount();
$sale_dis->sale_id = $list->id;
$sale_dis->name = $reader1->getCellByColumnAndRow(2, $i)->getValue();
$sale_dis->sum = $reader1->getCellByColumnAndRow($m, $i)->getValue();
$sale_dis->save();
}
$sale_tip_sum = SaleTip::where("sale_id", $list->id)->sum("sum");
$sale = Sale::where("id", $list->id)->first();
$sale->deduction = $sale_tip_sum;
$sale->true_price = $list->run_price - $list->discount_price - $sale->deduction;
$sale->save();
return response()->json(["code" => CodeUtil::SUCCESS, 'msg' => "成功"]);
} catch (\Exception $e) {
\Log::info("sale_import" + $e);
return response()->json(["code" => CodeUtil::ERROR, 'msg' => "失敗"]);
}
}
導出 :簡單版
public function export(Request $request)
{
$code = $request->get("code");
$name = $request->get("name");
$type = $request->get("type");
$start = $request->get("start");
$end = $request->get("end");
$obj = $this->select($code, $name, $type, $start, $end);
$list = $obj->select("code", "name", "type", "norm", "num", "time")->get()->toArray();
$file_name = date("Ymd_His-") . "菜品銷售管理表";
$title = ["菜品編碼", "菜品名稱", "菜品類別", "規格", "流水數量", "點菜時間"];
$cellData = array_merge([$title], $list);
Excel::create($file_name, function ($excel) use ($cellData) {
$excel->sheet('score', function ($sheet) use ($cellData) {
$sheet->rows($cellData)->setWidth(array(//調整導出表格單元格寬度
'A' => '15%',
'F' => '25%',
));
});
})->export('xlsx');
}
複雜版1:針對於多維表頭
(需求圖)
public function export(Request $request)
{
$code = $request->get("code");
$name = $request->get("name");
$obj = $this->select($code, $name);
$list = $obj->select("code", "name", "type", "norm", 'total_price', "burden")->get()->toArray();
foreach ($list as $k => $v) {
$bur_num[] = count($v['burden']);
}
$bur_max = max($bur_num);
$title1 = ["菜品編碼", "菜品名稱", "菜品類別", "規格", "合計"];
$title2 = [];
//處理多維表頭
for ($i = 1; $i < $bur_max + 1; $i++) {
if ($i == 1) $title2[] = ["", "", "", "", "", "用料", "規格", "用量", "成本價"];
else $title2[] = ["用料", "規格", "用量", "成本價"];
$title3[] = ["菜品配料" . $i, "", "", ""];
}
$sum_cell = ($i - 1) * 4 + 5;//列數
// return $sum_cell;
$title2 = array_collapse($title2);
$title3 = array_collapse($title3);
$title = array_collapse([$title1, $title3]);//表頭
$val = [];
$data = [];//處理數據
foreach ($list as $k => $v) {
if (!empty($v['burden'])) {
foreach ($v['burden'] as $vv) {
$val[] = [
$vv['name'],
$vv['norm'],
$vv['num'],
$vv['price']
];
}
}
unset($v['burden']);
$val = array_collapse($val);
$data[] = array_collapse([$v, $val]);
}
$file_name = date("Ymd_His-") . "菜品管理表";
$cellData = array_merge([$title, $title2], $data);
Excel::create($file_name, function ($excel) use ($cellData, $sum_cell, $bur_max) {
$excel->sheet('score', function ($sheet) use ($cellData, $sum_cell, $bur_max) {
$sheet->rows($cellData)->setWidth(array(//調整導出表格單元格寬度
"B" => '25%',
));
// for ($a = 0; $a < $sum_cell + 1; $a++) {
// $sheet->setWidth(array(//調整導出表格單元格寬度
// $this->intToChr($a) => '20%',
// ));
// }
for ($n = 0; $n < $sum_cell; $n++) {
if ($n < 5) {
//前5個表格單元格格式,合併每一列的前兩行
$sheet->mergeCells($this->intToChr($n) . '1:' . $this->intToChr($n) . '2');
} elseif ($n == 5) {
//對數組 規格 數據進行處理
for ($m = 0; $m < $bur_max; $m++) {
$sheet->mergeCells($this->intToChr($n + $m * 4) . '1:' . $this->intToChr($n + +3 + ($m * 4)) . '1');
}
}
}
});
})->export('xlsx');
// $code = $request->get("code");
// $name = $request->get("name");
// $obj = $this->select($code, $name);
// $list = $obj->select("code", "name", "type", "norm", 'total_price', "burden")->get()->toArray();
// foreach ($list as $k => $v) {
// $bur_num[] = count($v['burden']);
// }
// $bur_max = max($bur_num);
// $title1 = ["菜品編碼", "菜品名稱", "菜品類別", "規格", "合計"];
// $title2 = [];
// for ($i = 0; $i < $bur_max; $i++) {
// $title2[] = ["用料", "規格", "用量", "成本價"];
// }
// $title2 = array_collapse($title2);
// $title = array_collapse([$title1, $title2]);//拿到表頭
//
// $val = [];
// $data = [];
// foreach ($list as $k => $v) {
// if (!empty($v['burden'])) {
// foreach ($v['burden'] as $vv) {
// $val[] = [
// $vv['name'],
// $vv['norm'],
// $vv['num'],
// $vv['price']
// ];
// }
// }
// unset($v['burden']);
// $val = array_collapse($val);
// $data[] = array_collapse([$v, $val]);
// }
//
// $file_name = date("Ymd_His-") . "菜品管理表";
// $cellData = array_merge([$title], $data);
// Excel::create($file_name, function ($excel) use ($cellData) {
// $excel->sheet('score', function ($sheet) use ($cellData) {
// $sheet->rows($cellData)->setWidth(array(//調整導出表格單元格寬度
// 'A' => '15%',
// 'B' => '25%',
// ));
// });
// })->export('xlsx');
}
(效果圖)
複雜版2
(需求圖)
public function export(Request $request)
{
$time = $request->get("time");
$obj = Sale::select("id", "bill_num", "people_num", "run_price", "discount_price", "true_price", "tip", "deduction");
if (!empty($time)) {
$data = $obj->where('time', $time)->get();
$list = $data->toArray();
} else return response()->json(["code" => CodeUtil::ERROR, 'msg' => "請選擇導出數據的時間"]);
$file_name = date("Ymd_His-") . "店員收銀表";
$head = ["列表1", "列表2", "合計"];
$bill_num = ["賬單數量", ""];
$people_num = ["客流數量", ""];
$run_price = ["流水金額", ""];
$discount_price = ["優惠金額", ""];
$true_price = ["實收金額", ""];
$tip = ["小費", ""];
$deduction = ["扣點", ""];
$sum = $data = [];
//插入4個表中,一個主表,3組的附表
foreach ($list as $v) {
$sum["bill_num"] = [$v["bill_num"]];
$sum["people_num"] = [$v["people_num"]];
$sum["run_price"] = [$v["run_price"]];
$sum["discount_price"] = [$v["discount_price"]];
$sum["true_price"] = [$v["true_price"]];
$sum["tip"] = [$v["tip"]];
$sum["deduction"] = [$v["deduction"]];
$bill_num = array_merge($bill_num, $sum["bill_num"]);
$people_num = array_merge($people_num, $sum["people_num"]);
$run_price = array_merge($run_price, $sum["run_price"]);
$discount_price = array_merge($discount_price, $sum["discount_price"]);
$true_price = array_merge($true_price, $sum["true_price"]);
$tip = array_merge($tip, $sum["tip"]);
$deduction = array_merge($deduction, $sum["deduction"]);
$data = [$bill_num, $people_num, $run_price, $discount_price, $true_price, $tip, $deduction];//數據
$v["income_info"] = SaleIncome::where("sale_id", $v['id'])->get();
$v["tip_info"] = SaleTip::where("sale_id", $v['id'])->get();
$v["discount_info"] = SaleDiscount::where("sale_id", $v['id'])->get();
$i = $m = $n = 0;
if (count($v["income_info"]) > 0) {
foreach ($v["income_info"] as $val) {
// 處理 空表格
$i++;
if ($i == 1) $data[] = ["實收組成", $val['name'], $val['sum']];
else $data[] = ["", $val['name'], $val['sum']];
}
}
if (count($v["tip_info"]) > 0) {
foreach ($v["tip_info"] as $val) {
$n++;
if ($n == 1) $data[] = ["渠道扣點", $val['name'], $val['sum']];
else $data[] = ["", $val['name'], $val['sum']];
}
}
if (count($v["discount_info"]) > 0) {
foreach ($v["discount_info"] as $val) {
$m++;
if ($m == 1) $data[] = ["優惠組成", $val['name'], $val['sum']];
else $data[] = ["", $val['name'], $val['sum']];
}
}
}
$cellData = array_merge([$head], $data);
Excel::create($file_name, function ($excel) use ($cellData) {
$excel->sheet('score', function ($sheet) use ($cellData) {
$sheet->rows($cellData)->setWidth(array(//調整導出表格單元格寬度
'A' => '20%',
'B' => '20%',
'C' => '20%',
));
});
})->export('xlsx');
}
(效果圖)