FarmerImporter1.php
3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* 导入类 : 老师 导入 操作类
+-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Admin\Extensions\Importer
* @package App\Admin\Extensions\Importer
* @author Richer <yangzi1028@163.com>
* @date 2021年11月08日14:45:16
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Admin\Extensions\Importer;
use Encore\Admin\Actions\Action;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Storage;
use Maatwebsite\Excel\Excel;
/**
* Class FarmerImporter
*
* @category App\Admin\Extensions\Importer
* @package App\Admin\Extensions\Importer
* @author Richer <yangzi1028@163.com>
* @date 2021年11月08日14:45:16
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
class FarmerImporter1 extends Action
{
public $name = '导入数据';
protected $selector = '.import-post';
public function handle(Request $request)
{
// 下面的代码获取到上传的文件,然后使用`maatwebsite/excel`等包来处理上传你的文件,保存到数据库
// 1、文件获取:取特定的参数名,可以改为配置参数
$file = $request->file('file', 0);//文件名称
if (!$file) {
return $this->response()->error('操作失败,文件不存在。')->refresh();
}
// 2、判断文件格式以及各种错误
// 获取文件的扩展名
$ext = $file->getClientOriginalExtension();
if ($ext !== 'xls' && $ext !== 'xlsx') {
return $this->response()->error('操作失败,文件格式只能为 xls 或者 xlsx。')->refresh();
}
// 获取文件的绝对路径
$path = $file->getRealPath();
$file_name = date("Ymdhis") . '.' .$ext;
$origin_name = $file->getClientOriginalName();
// 3、上传文件到服务器,成功返回true, 失败返回false
$result = Storage::disk('import')->put($file_name, file_get_contents($path));
if (!$result) {
return $this->response()->error('操作失败,文件保存失败!')->refresh();
}
Excel::import(new UsersImport, 'users.xlsx');
// Excel::
// 4、 读取文件内容
$inputFileType = PHPExcel_IOFactory::identify($path);
$objReader = \PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($path);
$sheet = $objPHPExcel->getSheet(0);
$origin_data = $sheet->toArray();
if (!$origin_data) {
$this->message = '操作失败,文件内容为空。';
return false;
}
// 5、将记录保存
$record = [
'admin_id' => $this->getLoginUserId('admin'),
'path' => $file_name,
'name' => $file_name,
'origin_name' => $origin_name,
'origin_data' => $origin_data,
'type' => $type,
];
$model = $this->model->create($record);
// 6、根据类型解析数据,并进行相关的操作
$result = $this->dealData($model, $type, $origin_data);
dd($file);
$request->file('file');
// 处理相关数据
return $this->response()->success('导入完成!')->refresh();
}
public function form()
{
$this->file('file', '请选择文件')->required();
}
public function html()
{
return <<<HTML
<a class="btn btn-sm btn-danger import-post"><i class="fa fa-upload"></i> 导入数据</a>
HTML;
}
}