List item
导入:前端使用的layui框架
js:
layui.use('upload', function () {
var upload = layui.upload;
let _token = "{{csrf_token()}}";
let id=document.getElementById('company_id').value;
let upload_url = '{{ route('admin.companies.collects') }}?id='+id+'&_token=' + _token;
//执行实例
var uploadInst = upload.render({
elem: '#upload_btn' //绑定元素
, url: upload_url //上传接口
, accept: 'file'
, field: 'file'
, exts:'xls|xlsx'
, done: function (res) {
//上传完毕回调
if (res.code==2) {
layer.alert('导入失败');
} else if(res.code==0) {
layer.msg('导入成功,有失败数据');
window.location.href = "{{ route('admin.upload.export') }}?url=admin/companies";
} else if(res.code==1){
layer.alert('导入成功');
}
}
, error: function () {
//请求异常回调
}
});
});
## 控制器:
public function collects(Request $request)
{
$company_id = $request->get('id');
if ($request->hasFile('file')) {
$dataNewName = date('Y-m-d-h-i-s') . '.' . $request->file('file')->getClientOriginalExtension();
$dataPath = Storage::disk('public')->putFileAs('file', $request->file('file'), $dataNewName);
Excel::import(new EmployeeImport($company_id), $dataPath);
$error = session('error');
if (count($error) > 1) {
$result['code'] = 0;
$result['msg'] = '上传成功,有失败文件';
} else {
$result['code'] = 1;
$result['msg'] = '上传成功';
}
} else {
$result['code'] = 2;
$result['msg'] = '文件为空';
}
return $result;
}
##EmployeeImport
company_id = $company_id;
// \Log::info(__FUNCTION__ . ',' . __LINE__ . ',company:' . $this->getCompanyId() . ' - ' . $this->company_id);
}
public function collection(Collection $row)
{
session()->forget('error');
$error = [];
$title = $row->shift();
$title->push('错误行数');
array_unshift($error, $title->toArray());
$count = 0;
foreach ($row as $result) {
$count++;
$createR = [];
$sexMap = ['男' => 1, '女' => 0];
$tmpFields = ['job_no', 'name', 'sex', 'birthday', 'politics_status', 'marital_status', 'address', 'nation', 'bank', 'bank_account', 'id_card_no', 'mobile', 'phone', 'department', 'company_id'];
$result = $result->toArray();
array_walk($result, function ($value, $key) use (&$createR, $sexMap, $tmpFields) {
$field = isset($tmpFields[$key]) ? $tmpFields[$key] : null;
if (!empty($value) && $field) {
if ($key == 2) {
$value = isset($sexMap[$value]) ? $sexMap[$value] : 0;
}
if ($key == 3) {
if (is_numeric($value)) {
$n = intval(($value - 25569) * 3600 * 24);
$value = gmdate('Y-m-d H:i:s', $n);
} else {
$value = date('Y-m-d', strtotime($value));
}
}
if ($key == 14) {
$value = $this->company_id;
}
$createR[$field] = $value;
}
});
if (count($createR) != 15) {
array_push($result, $count);
array_push($error, $result);
continue;
}
try {
$model = Employee::updateOrCreate($createR);
\Log::info(__FUNCTION__ . __LINE__ . ',model:' . json_encode($model));
} catch (\Exception $e) {
array_push($result, $count);
array_push($error, $result);
continue;
}
}
session(['error' => $error]);
}
/**
* @return mixed
*/
public function getCompanyId()
{
return $this->company_id;
}
/**
* @param mixed $company_id
*/
public function setCompanyId($company_id)
{
$this->company_id = $company_id;
}
}
即可实现laravel excel的导入并插入数据表
##导出
Export
salary_id = $salary_id;
}
public function collection()
{
$id=session('salary_id');
$data=[];
$title = array('收款账号', '收款户名', '省份', '城市', '收款银行', '金额', '用途');
array_unshift($data, $title);
$salaries = Salary::where('salary_id',$this->salary_id)->get(['bank_account', 'bankAccount_name', 'province', 'city', 'opening_bank', 'salary', 'remarks'])->toArray();
foreach ($salaries as $salaries) {
array_push($data, $salaries);
}
return new Collection($data);
}
}
public function export(Request $request)
{
$salary_id = $request->get('id');
return Excel::download(new SalaryExportImport($salary_id), '工资表.xlsx');
}