//phpexcel类库github下载地址为https://github.com/PHPOffice/PHPExcel
//把里面的Classes文件夹解压放到vendor文件夹下的phpexcel文件夹下没有则创建
public function excel_import()
{
ini_set('memory_limit', '1024M');
if ($this->request->isPost()) {
$file = request()->file('file');
//引入第3方类库 (引入方式有多种,如果这种不行 可以试试其他)注意文件名别搞错了
require "../vendor/phpexcel/PHPExcel.php";
require "../vendor/phpexcel/PHPExcel/IOFactory.php";
header("Content-type:text/html;charset=utf-8");
//实例化主文件
//接收前台传过来的execl文件
$file = $_FILES['file'];
//截取文件的后缀名,转化成小写
$extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
if ($extension == "xlsx") {
//2007(相当于是打开接收的这个excel)
$objReader = \PHPExcel_IOFactory::createReader('Excel2007');
} else {
//2003(相当于是打开接收的这个excel)
$objReader = \PHPExcel_IOFactory::createReader('Excel5');
}
$objContent = $objReader->load($file['tmp_name']);
if ($objContent) {
$sheetContent = $objContent->getSheet(0)->toArray();
//删除第一行标题
unset($sheetContent[0]);
//dump($sheetContent);die();
//切记excel表格顺序下标跟对应字段不要弄错
foreach ($sheetContent as $k => $v) {
$arr['certificate_number'] = $v[0];
$arr['real_name'] = $v[1];
$arr['sfz'] = $v[2];
$arr['create_time'] = time();
$arr['sc_id'] = $school_info['sc_id'];
$arr['type'] = $school_info['type'];
$res[] = $arr;
}
//执行写入
$re = Db::table('certificate')
->insertAll($res);
if ($re) {
$this->success('导入成功 !');
} else {
$this->error('导入失败 !');
}
} else {
$this->error('请导入表格 !');
}
}
}