需要后台admin操作,新建功能一键导入。非常方便
public function upload_file()
{
t
h
i
s
−
>
u
p
l
o
a
d
f
o
r
m
(
′
文
件
名
.
x
l
s
x
′
,
f
u
n
c
t
i
o
n
(
this->upload_form('文件名.xlsx', function (
this−>uploadform(′文件名.xlsx′,function(excel_file) {
load('Common.PHPExcel');
$objPHPExcel = \PHPExcel_IOFactory::load($excel_file);
$sheet = $objPHPExcel->getSheet(0);//获取表
$row = $sheet->getHighestRow();//获取行
$column = $sheet->getHighestColumn();//获取列
$item_list = array();
$now = date('Y-d-m H:i:s',$time());//时间
for ( $i = 1; $i <= $row ; $i++) {
$item = [];
$item['subject'] = $sheet->getCell("A$i")->getValue();//
$item['answer_a'] = $sheet->getCell("B$i")->getValue();
$item['answer_b'] = $sheet->getCell("C$i")->getValue();
$item['answer_c'] = $sheet->getCell("D$i")->getValue();
$item['answer_d'] = $sheet->getCell("E$i")->getValue();
$item['key'] = $sheet->getCell("F$i")->getValue();
cdexcel_value($item);
if($item['key'] == "A"){
$item['key'] = $item['answer_a'];
}elseif ($item['key'] == "B"){
$item['key'] = $item['answer_b'];
}elseif ($item['key'] == "C"){
$item['key'] = $item['answer_c'];
}elseif ($item['key'] == "D"){
$item['key'] = $item['answer_d'];
}
$item_list[] = $item;
}
if (empty($item_list)) {
unlink($excel_file);
exit_error_code(1, '没有任何数据');
}
$m = M('t_question');
$count_insert = 0;
$insert_list = [];
foreach ($item_list as $tmp){
$count_insert++;
$insert_list[] = $tmp + array(
'create_time' => date('Y-d-m H:i:s',$time()),
'update_time' => date('Y-d-m H:i:s',$time()),
);
if(count($insert_list) >= 500){
$m->addAll($insert_list);
$insert_list = [];
}
}
if($insert_list){
$m->addAll($insert_list);
$insert_list = [];
}
unlink($excel_file);
exit_error_code(0, "导入成功,共{$count_insert}记录");
});
}