I'm using PHPExcel to update an eshop's items prices and stock, passing the data to a mysql query and updating the DB. I'm having a problem with some of the cells containing numbers with the format of 123,45 (using comma as a decimal separator). Although all cells contain the same format, only some are being read correctly. Please have a look for yourselves and let me know what I'm missing.
- Excel sample data: http://dl.dropbox.com/u/25322282/data.xlsx
- Log from looping through the data and printing out what it's reading where you can see that prices are read only past row 66. http://dl.dropbox.com/u/25322282/log.htm
And finally, my code:
$objPHPExcel = PHPExcel_IOFactory::load($path);
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
$nrColumns = ord($highestColumn) - 64;
$rowsadded=0;
$begin_row=2; // 1st line of data in excel file
for ($row = $begin_row; $row <= $highestRow; ++ $row) {
$val=array();
for ($col=0; $col < $highestColumnIndex; $col++) {
$cell = $objWorksheet->getCellByColumnAndRow($col, $row);
$val[] = $cell->getCalculatedValue();
}
if (($val[0]<>'') && ($val[6]>0)) { //check that row contains data before inserting
$rowsadded++;
$sql = sprintf("update productsizes set price=%s, stock=%s where auxcode=%s",
GetSQLValueString($val[6], "float"),
GetSQLValueString($val[4], "int"),
GetSQLValueString($val[0], "text"));
$result = mysql_query($sql) or die(mysql_error());
} //end of checking that row contains data before inserting
} echo '<h2>'.$rowsadded. ' rows updated successfully</h2>';