- 安装phpoffice/phpspreadsheet
composer require phpoffice/phpspreadsheet
- PHP代码示例
public function import($file) {
$ext = $file->getOriginalExtension();
if(!in_array( $ext, ['xls','xlsx'])){
return '请上传xls或者xlsx格式';
}
$filePath ='/file';
$fileName = time().'.'.$ext;
$savename = \think\facade\Filesystem::disk('public')->putFileAs( $filePath, $file,$fileName);
$path = public_path().'/storage/'.$savename;
if($ext=="xlsx"){
$reader = IOFactory::createReader('Xlsx');
}else{
$reader = IOFactory::createReader('Xls');
}
$excel = $reader->load($path,$encode = 'utf-8');
$sheet = $excel->getSheet(0)->toArray();
array_shift($sheet);
}