0

I'm fairly new to PHPExcel and was wondering if you would be able to assist me with the code below.

Exporting with PHPExcel works perfectly, but when I am importing a xls or xlsx document, it stores the content of the sheet (which is what i want) but also inserts almost 200 blank entries into the Database, from a sheet with 4 rows of data.

I've searched the internet for quite a while now, but cannot seem to find the solution to this.

See my code below:

        $storedir = "../uploads/". $_FILES['file']['name'];
        $store = move_uploaded_file($_FILES['file']['tmp_name'], $storedir);

        $filename =  $_FILES['file']['name'];
        $srow = $_POST['srow'];

        if ($store) {   
        $objPHPExcel = PHPExcel_IOFactory::load($storedir);
        foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
        $highestRow         = $worksheet->getHighestRow();
        $highestColumn      = $worksheet->getHighestColumn();
        $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
        $nrColumns = ord($highestColumn) - 64;

        for ($row = $srow; $row <= $highestRow-2; ++ $row) {
            $val=array();

            for ($col = 1; $col < $highestColumnIndex; ++ $col) {
                $cell = $worksheet->getCellByColumnAndRow($col, $row);
                $val[] = $cell->getValue();
        }
$savetodb = mysql_query("INSERT INTO `students` (`gender`, `name`, `surname`) VALUES ('".$val[1]."','".$val[2]."','".$val[3]."')") or die (mysql_error());
} 
  }
} 

I've not added my Connection codes and includes for PHPExcel classes and IOFactories, although they are in the code.

Any assistance would be greatly appreciated.

Boeta
  • 5
  • 7
  • could you echo $highestRow please – Gotrekk Jul 01 '15 at 07:17
  • it replies an endless string of '473' across the page, although the sheet only has 4 entries – Boeta Jul 01 '15 at 15:34
  • $highestColumn = 'N' ; $highestColumnIndex = '14' – Boeta Jul 01 '15 at 15:36
  • I came to realise that the empty cells created is caused by the bordered cells in the excel file. The imported file contains empty cells with borders around them, for the purpose of adding to the list, should the need arise. but these empty cells are added as values when imported. is there a way to ignore them? – Boeta Jul 05 '15 at 14:03
  • do you have any needed value? gender name or surname? – Gotrekk Jul 06 '15 at 07:07
  • Yes, the values are in the xlsx file. I only specified those three fields for the purpose of testing. the excel file contains much more fields of data. – Boeta Jul 06 '15 at 16:41

0 Answers0