标签:draw uniqid 读取 time info 编码 request function 信息
/**
* 导入Excel功能
*/
public function import(){
if (!empty($_FILES)) {
$file = request()->file(‘import‘);
$info = $file->rule(‘uniqid‘)->move(ROOT_PATH . ‘public‘ . DS . ‘suffix‘);
if($info){
$file_name = ‘./public/suffix/‘.$info->getFilename();
}else{
// 上传失败获取错误信息
echo $file->getError();
}
Vendor(‘PHPExcel.PHPExcel‘);
Vendor(‘PHPExcel.PHPExcel.Worksheet.Drawing‘);
Vendor(‘PHPExcel.PHPExcel.Writer.Excel2007‘);
// 读取Excel文件内容
$PHPReader = new \PHPExcel_Reader_Excel2007();
$PHPExcel = $PHPReader->load($file_name);
$currentSheet = $PHPExcel->getSheet(0); //读取excel文件中的第一个工作表
$allColumn = $currentSheet->getHighestColumn(); //取得最大的列号
$allRow = $currentSheet->getHighestRow(); //取得一共有多少行
$arr = array(); //声明数组
/**从第二行开始输出,因为excel表中第一行为列名*/
for($currentRow = 2;$currentRow <= $allRow;$currentRow++){
/**从第A列开始输出*/
for($currentColumn= ‘A‘;$currentColumn<= $allColumn; $currentColumn++){
//数据坐标
$address = $currentColumn . $currentRow;
//读取到的数据,保存到数组$data中
$data[$currentRow][$currentColumn] = $currentSheet->getCell($address)->getValue();
/**如果输出汉字有乱码,则需将输出内容用iconv函数进行编码转换,如下将gb2312编码转为utf-8编码输出*/
//echo iconv(‘utf-8‘,‘gb2312‘, $val)."\t";
}
}
foreach ($data as $k => $v) {
$where = [‘suffix‘=>$v[‘A‘]];
$num = Db(‘suffix‘)->where($where)->value(‘id‘);
if(!$num){
$arr[‘suffix‘] = $v[‘A‘];
$arr[‘companyname‘] = $v[‘B‘];
$num = Db(‘suffix‘)->insert([‘suffix‘=>$arr[‘suffix‘],‘companyname‘=>$arr[‘companyname‘],‘creat_time‘=>date(‘y-m-d h:i:s‘)]);
if($num < 0){
return $this->error(‘导入数据库失败‘);
}
}
}
return $this->success(‘导入成功‘);
}
标签:draw uniqid 读取 time info 编码 request function 信息
原文地址:https://www.cnblogs.com/mcll/p/9275131.html