Now I'm reading an excel file and showing it on a html table.
like this:
<?php
function load_table(){
require_once('Classes/PHPExcel.php');
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load("SampleData.xlsx");
$objWorksheet = $objPHPExcel->getActiveSheet();
$highestRow = $objWorksheet->getHighestRow(); // e.g. 10
$highestColumn = $objWorksheet->getHighestColumn(); // e.g 'F'
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); // e.g. 5
echo '<table>' . "\n";
for ($row = 1; $row <= $highestRow; ++$row) {
echo '<tr>' . "\n";
for ($col = 0; $col <= $highestColumnIndex; ++$col) {
echo '<td>';
echo $objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
echo '</td>' . "\n";
}
echo '</tr>' . "\n";
}
echo '</table>' . "\n";
}
?>
Sometimes the cell content is a formula. I'd like to calculate it and echo
the result (just like excel does). I tried the code below but I got the following error:
Warning: Illegal string offset 'value' in...
echo $objWorksheet->getCellByColumnAndRow($col, $row)->getCalculatedValue();
I also tried something like:
echo $objWorksheet->getCellByColumnAndRow($col, $row)->getOldCalculatedValue();
This one worked, but I do not want to use GetOldCalculatedValue
because:
getOldCalculatedValues() reads the value as it was last calculated by MS Excel (if available) though you can't guarantee it will always be there, or be correct if it is (you can disable autocalculation in MS Excel, or it won't be set if loading a CSV file); while getCalculatedValue() actually recalculates the value within PHPExcel itself.
Also I used echo
to print just the formula string and it appears to be just fine:
echo $objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
Fórmula printed:
=F2*E2
=F3*E3
=F4*E4
=F14*E14 ...
So how could I get the result of the formula and ALSO format it as currency ?