Read Excel's internal calculated values with PHPExcel
PHPExcel does indeed have a method to retrieve that value for any cell:
$objPHPExcel->getActiveSheet()->getCell('A1')->getOldCalculatedValue();
Note that this is not guaranteed.... if automatic calculation has been disabled in MS Excel, then it will only be as valid as the the last time that MS Excel actually executed the calculation PHPExcel excel read is not working for some cells with calculation
Finally I get old getOldCalculatedValue using loop.
$data = $this->excel->getActiveSheet()->toArray(null, true, true, true);
//This code add for some calculated cells were not reading in xlsx format
foreach ($data as $no => $row) {
foreach ($row as $key => $value) {
if (isset($value) && $value == '#VALUE!') {
$data[$no][$key] = $this->excel->getActiveSheet()->getCell($key.$no)->getOldCalculatedValue();
}
}
}
PHPExcel generated excel file not auto-calculating formula
Although it will not be a solution, it may be improved to some extent by recalculating on the server side.
$writer = PHPExcel_IOFactory::createWriter($excel, self::EXCEL_TYPE);
$writer->setPreCalculateFormulas();
$writer->save($tempfile);
How to read the visible data of the cell, not the underlying formula in PHPExcel?
You can use ...->getCell($columnAsLetters.$row)->getCalculatedValue();
as described in this thread: How to automatically read in calculated values with PHPExcel?
PHPExcel How to automatically create new excel file in every week routine?
This you can archive with DateTime()
to determine the current week of the year.
This should then look like below.
// Get the current week of year
$date = new DateTime('now');
$week = $date->format("W");
// Path to the file storage (in this case the same folder as this script)
$path = __DIR__;
// Set file name for excel file and combine with the path
$file = $path . '/week-' . $week . '.xls';
// Determine if file exists
if (!is_file($file)) {
// File doesn't exists, so we create a new file
$objPHPExcel = new PHPExcel();
} else {
// File exists, so we load this file
$objPHPExcel = PHPExcel_IOFactory::load($file);
}
// Your existing code to write the data
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('A1','Name of Stakeholder(s)');
$objPHPExcel->getActiveSheet()->setCellValue('B1','NRIC No./Passport No./Company No.');
$objPHPExcel->getActiveSheet()->setCellValue('C1','Email');
$objPHPExcel->getActiveSheet()->setCellValue('D1','Contact Number');
$objPHPExcel->getActiveSheet()->setCellValue('E1','CDS Account No.');
$objPHPExcel->getActiveSheet()->setCellValue('F1','Correspondence Address');
$objPHPExcel->getActiveSheet()->setCellValue('G1','Date Submitted');
$row = $objPHPExcel->getActiveSheet()->getHighestRow()+1;
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$row, $_POST['name']);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$row, $_POST['ic']);
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$row, $_POST['email']);
$objPHPExcel->getActiveSheet()->SetCellValue('D'.$row, $_POST['phone']);
$objPHPExcel->getActiveSheet()->SetCellValue('E'.$row, $_POST['cds']);
$objPHPExcel->getActiveSheet()->SetCellValue('F'.$row, $_POST['address']);
$objPHPExcel->getActiveSheet()->SetCellValue('G'.$row, $_POST['date']);
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$from = "A1";
$to = "G1";
$objPHPExcel->getActiveSheet()->getStyle("$from:$to")->getFont()->setBold(true);
// Save the excel file
$objWriter->save($file);
Related Topics
How to Select a MySQL Database to Use with Pdo in PHP
Programming Multi-Language PHP Applications
Dynamic Class Property $$Value in PHP
Paypal Ipn Bad Request 400 Error
Facebook Graph API - Friends Using Application
Usort Issue with Decimal Numbers
PHP Dynamically Create CSV: Skip the First Line of a CSV File
Why a Full Stop, "." and Not a Plus Symbol, "+", for String Concatenation in PHP
Laravel 4 - Including a "Partial" View Within a View (Without Using Blade Template)
File_Get_Contents('Php://Input') Always Returns an Empty String
Laravel Errno 150 Foreign Key Constraint Is Incorrectly Formed
Converting Between Timezones in PHP
Force Cache Refresh After Deployment
How to Tag a User in a Photo Using the Facebook Graph API
How to Access Elements in an Array Returned from a Function