使用php和spout更快地将大规模excel(xlx和xlsx)文件上传到mysql
希望大家都好。我在上传一个大的excel文件时遇到了一个问题。该文件包含150+k行。当我运行php脚本时,一段时间后它停止执行,无法上传数据。我使用下面的代码上传使用php和spout更快地将大规模excel(xlx和xlsx)文件上传到mysql,php,mysql,excel,Php,Mysql,Excel,希望大家都好。我在上传一个大的excel文件时遇到了一个问题。该文件包含150+k行。当我运行php脚本时,一段时间后它停止执行,无法上传数据。我使用下面的代码上传 require_once('vendor/excel_reader2.php'); require_once('vendor/SpreadsheetReader.php'); if (isset($_POST["import"])) { ini_set('max_execution_time',0); $a
require_once('vendor/excel_reader2.php');
require_once('vendor/SpreadsheetReader.php');
if (isset($_POST["import"]))
{
ini_set('max_execution_time',0);
$allowedFileType = ['application/vnd.ms-excel','text/xls','text/xlsx','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];
if(in_array($_FILES["file"]["type"],$allowedFileType)){
$targetPath = 'uploads/'.$_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'], $targetPath);
$Reader = new SpreadsheetReader($targetPath);
$sheetCount = count($Reader->sheets());
for($i=0;$i<$sheetCount;$i++)
{
$Reader->ChangeSheet($i);
foreach ($Reader as $Row)
{
$data[]=$Row;
//insert from here
}
}
//echo implode(',',$data);
echo $data[1][0];
echo "<pre>"; print_r($data); echo "</pre>";
}
else
{
$type = "error";
$message = "Invalid File Type. Upload Excel File.";
}
}
require_once('vendor/excel_reader2.php');
一次需要_('vendor/SpreadsheetReader.php');
如果(isset($_POST[“导入”]))
{
ini_集('max_execution_time',0);
$allowedFileType=['application/vnd.ms excel'、'text/xls'、'text/xlsx'、'application/vnd.openxmlformats of icedocument.spreadsheetml.sheet'];
if(在数组中($文件[“文件”][“类型”],$allowedFileType)){
$targetPath='uploads/'。$_文件['file']['name'];
移动上传的文件($文件['file']['tmp\u名称],$targetPath);
$Reader=新的电子表格阅读器($targetPath);
$sheetCount=count($Reader->sheets());
对于($i=0;$iChangeSheet($i);
foreach($Reader作为$Row)
{
$data[]=$Row;
//从这里插入
}
}
//回波内爆(“,”,$data);
echo$数据[1][0];
echo“”;print_r($data);echo“”;
}
其他的
{
$type=“error”;
$message=“文件类型无效。请上载Excel文件。”;
}
}
一次插入一行的速度很慢。将它们保存在数组中,循环后,遍历数组,比如说一次插入100行,然后用一个insert语句插入这些行。因此SQL。显示表结构。显示mysql配置。对于大文件,可能会上载它们,并将它们的处理放在后台作业中因为您的web服务器将有最长时间。可能会在上载过程中输出一些进度输出。