ExcelResolveController.class.php
4.34 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
/**
*【线下培训-后台】解析人员导入模板
* @author: houyingcai
* @email: 594609175@qq.com
* @date : 2017-08-29 15:24:15
* @version $Id$
*/
namespace Apicp\Controller\Paper;
use Com\PythonExcel;
class ExcelResolveController extends \Apicp\Controller\AbstractController
{
// 表头最小长度
const HEADER_MIN_LENGTH = 2;
// 模版数据记录数
const TPL_LEN = 3;
// 标题长度
const TITLE_LENGTH = 2;
// 最大长度
const HEADER_MAX_LENGTH = 3;
// 初始值
const LEN = 0;
// 列表名称
protected $__fieldName = [
[
'key' => 'username',
'name' => '员工姓名',
],
[
'key' => 'mobile',
'name' => '手机号码',
]
];
public function Index_post()
{
// 获取上传文件详情
$file = $_FILES['file'];
// 验证数据
if (!$this->xls_validation($_FILES)) {
return false;
}
// xls文件名称
$filename = $file['tmp_name'];
$data = PythonExcel::instance()->read($filename, 0);
// 获取列表
list($data, $title, $total, $headTotal) = $this->get_excel_list($data);
// 如果数据为空
if (empty($data)) {
E('_EMPTY_XLS_DATA');
}
//返回值
$this->_result = [
'total' => $total,
'head_total' => $headTotal,
'head' => $title,
'list' => $data,
];
return true;
}
/**
* 验证数据
*
* @param array $file POST数据
*
* @return bool
*/
protected function xls_validation($file = [])
{
// 请上传文件
if (empty($file)) {
E('_ERR_FILE_UNDEFINED');
}
// 文件上传失败
if ($file['error'] > 0) {
E('_ERR_UPLOAD_FILE');
}
// 文件大小判断(2M以内)
if ($file['size'] > 1024 * 2 * 1000) {
E('_ERR_FILE_SIZE');
}
return true;
}
/**
* 获取head数据列表以及整合过的xls中数据列表
*
* @author houyingcai
* @param array $list xls数据列表
*
* @return array
*/
public function get_excel_list($list = [])
{
// 初始化返回值
$data = [];
// 初始化
$i = 0;
// 存储标题长度
$storage = [];
// 循环数据
foreach ($list as $v) {
// 截取前三条数据
if ($i < self::TPL_LEN) {
$i++;
continue;
}
// 姓名和手机号都不能为空
if (empty($v[0]) && empty($v[1])) {
continue;
}
// 赋值
$data[] = [
'username' => $v[0],
'mobile' => (string)$v[1],
];
// 存储所有表格长度
$storage[] = $this->countHead($v);
}
// 表格题目数组
$title = [];
// 初始化表头最小长度
$min = self::HEADER_MIN_LENGTH;
// 设置最大表头长度
if (max($storage) < $min) {
$max_title = $min;
} else {
$max_title = max($storage);
}
// 重组表格标题
for ($i = 0; $i < $max_title; $i++) {
// 标题表格列表
$title[$i] = $this->__fieldName[$i];
// 如果是最大值
if ($max_title - self::LEN == $i) {
// 新增是否导入成功
$title[$i + self::LEN] = $this->__fieldName[self::HEADER_MAX_LENGTH];
}
}
$data = array_filter($data);
$title = array_filter($title);
return [$data, $title, count($data), count($title) - self::LEN];
}
/**
* 获取标题长度
*
* @author houyingcai
* @param array $list 列表数据
*
* @return int|string
*/
private function countHead($list)
{
// 实例化
$data = 0;
// 循环数据
for ($i = self::TITLE_LENGTH; $i > 0; $i--) {
// 判断非空
if (!empty($list[$i])) {
// 赋值
$data = $i + 1;
break;
}
}
return $data;
}
}