How to Automatically Read in Calculated Values with PHPexcel

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



Leave a reply



Submit