php-tp5.1 导入导出
作者:互联网
导入
PHPExcel 下载
require_once Env::get('root_path').'/vendor/PHPExcel/PHPExcel/IOFactory.php';
header("content-type:text/html;charset=utf-8");
//上传excel文件
$file = request()->file('excel');
//将文件保存到public/uploads目录下面
$info = $file->validate(['size'=>10485762,'ext'=>'xls,xlsx'])->move( './uploads/filexisx');
if($info){
//获取上传到后台的文件名
$fileName = $info->getSaveName();
//获取文件路径
$filePath = Env::get('root_path').'public'.DIRECTORY_SEPARATOR.'uploads/filexisx'.DIRECTORY_SEPARATOR.$fileName;
//获取文件后缀
$suffix = $info->getExtension();
//判断哪种类型
if($suffix=="xlsx"){
$reader = \PHPExcel_IOFactory::createReader('Excel2007');
}else{
$reader = PHPExcel_IOFactory::createReader('Excel5');
}
}else{
$this->error('文件过大或格式不正确导致上传失败-_-!');
}
//载入excel文件
$excel = $reader->load($filePath,$encode = 'utf-8');
//读取第一张表
$sheet = $excel->getSheet(0);
//获取总行数
$row_num = $sheet->getHighestRow();
//获取总列数
$col_num = $sheet->getHighestColumn();;
$data = []; //数组形式获取表格数据
for ($i = 2; $i <= $row_num; $i ++) {
$data[$i]['zhanghao'] = $sheet->getCell("A".$i)->getValue();
$data[$i]['name'] = $sheet->getCell("B".$i)->getValue();
$data[$i]['money'] = $sheet->getCell("C".$i)->getValue();
$data[$i]['create_time'] = time();
//将数据保存到数据库
}
$ins = $model->insertAll($data);
$ins?success():error();
导出
标签:info,tp5.1,sheet,excel,获取,导入,PHPExcel,php,data 来源: https://www.cnblogs.com/phpres/p/15407246.html