tp6导入excel文件使用phpoffice
使用composer安装office:composer require phpoffice/phpspreadsheet
安装成功后项目的composer.json可以看到
导入excel文件:
public function importData()
{
// 获取表单上传文件
$file[] = request()->file('file');
$savename = \think\facade\Filesystem::putFile('file', $file[0]);
$fileExtendName = substr(strrchr($savename, '.'), 1);
// 有Xls和Xlsx格式两种
if ($fileExtendName == 'xlsx') {
$objReader = IOFactory::createReader('Xlsx');
} else {
$objReader = IOFactory::createReader('Xls');
}
$objReader->setReadDataOnly(TRUE);
// 读取文件,tp6默认上传的文件,在runtime的相应目录下,可根据实际情况自己更改
$objPHPExcel = $objReader->load(root_path() . '/runtime/