读取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>
- php处理是参考了很多文章,选择使用PHPExcel,效果挺好的,但是其中还有很多函数不知道具体作用,写出来保留一下,方便下次学习。
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);
}
}```