Import multisheet into mysql database

Topics: Developer Forum, User Forum
Jan 29, 2015 at 7:12 AM
We have code to import excel to mysql database, there are 10 sheets inside. It works to view on browser all sheet, but only one sheet insert into database. Could you help us, thanks.
$path = $_FILES['file']['name'];
    
    // Load PHPExcel
    $objPHPExcel = PHPExcel_IOFactory::load($path);

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

echo "<br>The worksheet ".$worksheetTitle." has ";
    echo $nrColumns . ' columns (A-' . $highestColumn . ') ';
    echo ' and ' . $highestRow . ' row.';
    echo '<br>Data: <table border="1"><tr>';
    for ($row = 1; $row <= $highestRow; ++ $row) {
        echo '<tr>';
        for ($col = 0; $col < $highestColumnIndex; ++ $col) {
            $cell = $worksheet->getCellByColumnAndRow($col, $row);
            $val = $cell->getValue();
            $dataType = PHPExcel_Cell_DataType::dataTypeForValue($val);
            echo '<td>' . $val . '<br></td>';
        }
        echo '</tr>';
    }
    echo '</table>';
    }

for ($row = 2; $row <= $highestRow; ++$row) {
        $val = array();
        for ($col = 0; $col < $highestColumnIndex; ++$col) {
            $cell = $worksheet->getCellByColumnAndRow($col, $row);
            $val[] = $cell->getValue();
        }
if ($numMatch > 0) {} else{
            // Buat query insert per-baris data ke tabel user
            $sql = "INSERT IGNORE INTO mvr (number,project_id,raised_by, approved_by) VALUES ('" .$val[1]."".$val[2]. "','".$project_id."','".$raised_by."','".$approved_by."')";
            mysql_query($sql);
        
        }