编程语言
首页 > 编程语言> > php框架tp6自学笔记——excel文件处理

php框架tp6自学笔记——excel文件处理

作者:互联网

读取excel中的内容

<form action="ExcelUpLoad" name="form" method="post" enctype="multipart/form-data">
    <label for="file">Filename:</label>
    <input type="file" name="file" id="file" />
    <br />
    <input type="submit" name="submit" value="Submit"/>
</form>
if(is_uploaded_file($_FILES['file']['tmp_name'])){
            //接收前台文件
            $ex = $_FILES['file'];
            $file_name = $ex['name'];//文件名
            $file_size = $ex['size'];//文件大小
            $file_tmp = $ex['tmp_name'];//文件临时存储的位置
            $file_type= $ex['type'];//文件类型
            $file_ext=substr($ex['name'], stripos($ex['name'], '.'));//文件后缀
			//这部分是文件转储保留
            $path = 'uploadfile./' . basename( $_FILES["file"]["name"]);//设置移动路径
            move_uploaded_file($ex['tmp_name'], $path);
			
			//t从这里开始对excel进行处理,先是获取一些参数
            $file =  $path;
            $objPHPExcel = PHPExcel_IOFactory::load($file); //获取sheet表格数目
            $sheetCount = $objPHPExcel->getSheetCount(); //默认选中sheet0表
            $sheetSelected = 0;
            $objPHPExcel->setActiveSheetIndex($sheetSelected);
            $rowCount = $objPHPExcel->getActiveSheet()->getHighestRow(); //获取表格行数
            $columnCount = $objPHPExcel->getActiveSheet()->getHighestColumn();//获取表格列数
            $length = strlen($columnCount);
            $dataArr = array();
            $end = substr($columnCount,-1);//获取最后一列

			//数据最后都存储到了$dataArr中
            for ($row = 2; $row <= $rowCount; $row++) {
                    //列数循环 , 列数是以A列开始
                    for ($n = ord('A'); $n <= ord($columnCount); $n++) {
                        $column = chr($n);
                        $dataArr[$row][$column] = $objPHPExcel->getActiveSheet()->getCell($column . $row)->getValue();
                    }
                }
            
            //这里是逐行插入数据库
            $name = 'xbm';
            for ($n = 2; $n <= $rowCount; $n++) {
                    $data = [$name.'_id'=>$dataArr[$n]['B'],//学号
                             $name.'_name' => $dataArr[$n]['C']
                    ];
                    $insert = Db::name($name.'_lgb')->insert($data);
            }
      }```

标签:name,row,excel,dataArr,tp6,ex,file,php,objPHPExcel
来源: https://blog.csdn.net/weixin_43227482/article/details/120208969