使用PHPExcel时,首先要下载PHPExcel并放在ThinkPHP/Library/Vendor下
1.创建html文件(文件上传按钮)
2.后台处理上传的文件
public function xxxx(){
$file = $_FILES;
if($file){
$upload = new \Think\Upload();
if (!$info = $upload->upload()) {
$this->error($upload->getError());
}
$file_name=$upload->rootPath.$info['import']['savepath'].$info['import']['savename'];
$extension = $info['import']['ext'];
//引入excel操作类
vendor("PHPExcel.PHPExcel");
if($extension == "xlsx"){
$objReader = \PHPExcel_IOFactory::createReader('Excel2007');
}else{
$objReader = \PHPExcel_IOFactory::createReader('Excel5');
}
$objPHPExcel = $objReader->load($file_name,$encode='utf-8');
$sheet = $objPHPExcel->getSheet(0);
//excel中每个单元格中的数据
$excelData = $sheet->toArray();
}else{
$this->display();
}
}