如何使用PHP excel库将excel数据导入数据库



我正在尝试使用PHP excel库导入excel数据到数据库中。它是为少量数据工作。对于大数据(30000行)它不工作。它需要很多时间。
这是我的代码

$fileName = $_FILES["production"]["tmp_name"];
$objPHPExcel = PHPExcel_IOFactory::load($fileName);
$phpspreadsheet = $objPHPExcel->getSheet(0);

$total_data = $phpspreadsheet->getHighestRow();
//echo $chunkSize;
// exit;
$highest_column = $phpspreadsheet->getHighestColumn();
for ($row = 2 ; $row <= $total_data; $row++) {
$productionDataAll = $phpspreadsheet-> rangeToArray ('A' . $row . ':' . $highest_column . $row, NULL, TRUE, FALSE);

$line=htmlentities($productionDataAll[0][0]);
$workedorder=htmlentities($productionDataAll[0][1]);
$modelnumber=htmlentities($productionDataAll[0][2]);
$revision=htmlentities($productionDataAll[0][3]);
if(!empty($productionDataAll[0][4])){
$serialnumber=htmlentities($productionDataAll[0][4]);
}
else{
$serialnumber='';
}
$lpn=htmlentities($productionDataAll[0][5]);
$shift=htmlentities($productionDataAll[0][6]);
$datecreated=htmlentities($productionDataAll[0][7]);
$datecompleted=htmlentities($productionDataAll[0][8]);
$repairflag=htmlentities($productionDataAll[0][9]);
$date=date("Y-m-d", strtotime( '-1 days' ) );
$skeydate=date("Ymd", strtotime( '-1 days' ) );
$skey=$skeydate.$line;
$importproduction=importproduction($mysql_connectionLinkLocal,$line,$workedorder,$modelnumber,$revision,$serialnumber,$lpn,$shift,$repairflag,$date,$skey,$datecreated,$datecompleted); // insert function

}
上面的代码是逐个读取表数据并插入到数据库中。如何按100(组)读取表格数据?如何减少加载时间?我得到页面无响应

试试https://github.com/aVadim483/fast-excel-reader

它可以读取excel数据非常快和最小的内存使用(但只有XLSX格式)。在演示文件夹

中有一个读取100K电子表格的例子

最新更新