简体   繁体   English

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

[英]How to import excel data into database using PHP excel library

I am trying to import excel data into database using PHP excel Library.It is working for small amount of data.For large data (30000 rows) it is not working.It is taking much time.我正在尝试使用 PHP excel 库将 excel 数据导入数据库。它适用于少量数据。对于大数据(30000 行)它不工作。
here is my code这是我的代码

$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
            
                  }

above code is reading sheet data one by one and inserting into database.上面的代码是一个一个地读取工作表数据并插入到数据库中。 How to read the sheet data by 100 (chunck)?如何按 100(块)读取工作表数据? How to reduce the loading time?I am getting page unresponsive如何减少加载时间?我的页面无响应

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

It can read excel data very quickly and with minimal memory usage (but XLSX format only).它可以非常快速地读取 excel 数据,并且使用最少的 memory (但仅限 XLSX 格式)。 There is example with reading of 100K spreadsheet in demo folder在演示文件夹中有读取 100K 电子表格的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM