im using PHPExcel to import xlsx file to a database, the following code works great for passing $val as the cell value and avoid the insertion of colum headers:
$dataArr = array();
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$worksheetTitle = $worksheet->getTitle();
$highestRow = $worksheet->getHighestRow(); // e.g. 10
$highestColumn = $worksheet->getHighestColumn(); // e.g 'F'
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
for ($row = 1; $row <= $highestRow; ++ $row) {
for ($col = 0; $col < $highestColumnIndex; ++ $col) {
$cell = $worksheet->getCellByColumnAndRow($col, $row);
$val = $cell->getValue();
$dataArr[$row][$col] = $val;
}
}
}
unset($dataArr[1]);
The problem is when I try to insert a date value, the current Excel cells date format is 10/03/17, but inserts 0000-00-00 in my database.
I tried the reply here but not luck.
Here is my current code:
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$worksheetTitle = $worksheet->getTitle();
$highestRow = $worksheet->getHighestRow(); // e.g. 10
$highestColumn = $worksheet->getHighestColumn(); // e.g 'F'
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
for ($row = 1; $row <= $highestRow; ++ $row) {
for ($col = 0; $col < $highestColumnIndex; ++ $col) {
$cell = $worksheet->getCellByColumnAndRow($col, $row);
$val = $cell->getValue();
if (PHPExcel_Shared_Date::isDateTime($val)) {
echo $cell->getFormattedValue() , EOL;
}
else {
echo $cell->getValue() , EOL;
}
$dataArr[$row][$col] = $val;
}
}
}
But this leads me to a Fatal Error "Uncaught TypeError: Argument 1 passed to PHPExcel_Shared_Date::isDateTime() must be an instance of PHPExcel_Cell, string given"
Any suggestions?