I'm trying to read some cell values from a .xlsx
file using PhpSpreadSheet, but I don't know why this isn't working. The file opens but when I read the values of the cell it returns nothing. I have checked that the filename is correct and that cells have data. I want to read the SECOND sheet of the file, that why I have used getSheet(1)
.
This is my code:
use PhpOffice\PhpSpreadsheet\IOFactory;
$inputFileType = 'Xlsx';
$inputFileName = 'Test.xlsx';
$reader = IOFactory::createReader($inputFileType);
$reader->setReadDataOnly(true);
$spreadsheet = $reader->load($inputFileName);
$sheet = $spreadsheet->getSheet(1);
$filterHours = $sheet->getCell('D309')->getOldCalculatedValue();
$otherHour = $sheet->getCell('D295')->getOldCalculatedValue();
$title = $sheet->getCell('B297')->getOldCalculatedValue();
printf($title);
printf($filterHours);
printf($otherHour);
EDIT: If I use getCalculatedValue()
instead of getOldCalculatedValue()
I get this error:
Fatal error: Uncaught PhpOffice\PhpSpreadsheet\Calculation\Exception:
Configurador!D309 -> Configurador!D745 -> Configurador!D755 -> Configurador!C776 -> Formula Error: An unexpected error occured in C:\xampp\htdocs\OfferConfigurator\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Cell\Cell.php:274
Stack trace: #0 C:\xampp\htdocs\OfferConfigurator\php\generateDocumentation\prueba.php(26): PhpOffice\PhpSpreadsheet\Cell\Cell->getCalculatedValue()
#1 {main} thrown in C:\xampp\htdocs\OfferConfigurator\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Cell\Cell.php on line 274
Can that be because Configurador!C776
has a formula that references to another sheet?