PHPExcel在PHP中批量导入csv

时间:2017-12-21 13:47:10

标签: php phpexcel

我在存储/导入超过1000行的巨大CSV文件时遇到问题。 数据库中只保存了几行,我想到的是逐批存储/导入行,但我不知道如何将它放在我的代码中:

我的代码:

require_once 'PHPexcel/PHPExcel.php';
    $objPHPExcel = PHPExcel_IOFactory::load($file);

    foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
        $worksheetTitle     = $worksheet->getTitle();
        $highestRow         = $worksheet->getHighestRow();
        $highestColumn      = $worksheet->getHighestColumn();
        $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);

        for ($row = 2; $row <= $highestRow; ++ $row) {
            $val=array();
            for ($col = 0; $col < $highestColumnIndex; ++ $col) {
                $cell = $worksheet->getCellByColumnAndRow($col, $row);
                $val[] = $cell->getValue();

            }
        }
    }

我的想法:

$counter = 0;
$total = 100;//get the total rows
while(true){
    $items = array();//get the first 20 rows from excel
    foreach($items as $item){
        //save in database

        if($counter == $total){
            break;
        } 
        $counter++;
    }

}

0 个答案:

没有答案