这篇文章主要讲解了“用PhpSpreadsheet实现excel导入数据”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“用PhpSpreadsheet实现excel导入数据”吧!

科尔沁右翼前网站建设公司创新互联公司,科尔沁右翼前网站设计制作,有大型网站制作公司丰富经验。已为科尔沁右翼前近千家提供企业网站建设服务。企业网站搭建\外贸营销网站建设要多少钱,请找那个售后服务好的科尔沁右翼前做网站的公司定做!
1、composer
composer require phpoffice/phpspreadsheet
2、文件传入方法
public function talentImport(string $file_name='excelFile')
{
$file = \Illuminate\Support\Facades\Request::file($file_name);if ($file) {if(!$file->isValid()) {return '文件有误';
}if (!in_array($file->getClientOriginalExtension(), ['xls', 'xlsx'])) {return '只允许上传xls或xlsx格式的文件';
}
$excel_file_path = $file->getRealPath();if (!is_uploaded_file($excel_file_path)) {return '非法上传';
}
$sheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($excel_file_path)->getActiveSheet();
$res = array();foreach ($sheet->getRowIterator(2) as $row) {
$tmp = array();foreach ($row->getCellIterator() as $cell) {
$tmp[] = $cell->getFormattedValue();
}
$res[$row->getRowIndex()] = $tmp;
}if (empty($res)) {return '无法读取文件内容或文件内容为空';
}return $res;
}return '请上传excel文件';
}3、对文件内容的数组进行操作
foreach ($res as $row) {
验证数据正确性
存入insert数组
}
insert($array)感谢各位的阅读,以上就是“用PhpSpreadsheet实现excel导入数据”的内容了,经过本文的学习后,相信大家对用PhpSpreadsheet实现excel导入数据这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!