Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Fatal error: Uncaught exception 'PHPExcel_Calculation_Exception'

Tags:

phpexcel

can somebody tell me why am I getting this error?

PHP Fatal error:  Uncaught exception 'PHPExcel_Calculation_Exception' with message 'Worksheet!E48 -> Formula Error: An unexpected error occured' in PHPExcel/1.8.0/Classes/PHPExcel/Cell.php:300
Stack trace:
#0 PHPExcel/1.8.0/Classes/PHPExcel/Writer/Excel5/Worksheet.php(460): PHPExcel_Cell->getCalculatedValue()
#1 PHPExcel/1.8.0/Classes/PHPExcel/Writer/Excel5.php(187): PHPExcel_Writer_Excel5_Worksheet->close()
#2 export/the_script.php(98): PHPExcel_Writer_Excel5->save('the_export.xls')
#3 {main}
  thrown in PHPExcel/1.8.0/Classes/PHPExcel/Cell.php on line 300

The code that generates this error:

$excel = new PHPExcel();
$excel->setActiveSheetIndex(0);
$R = $db->query("SELECT col1, col2, col3 from table1");
while($row = $R->fetch(Zend_Db::FETCH_NUM)) {
    $excel->getActiveSheet()->fromArray($row, false, 'A'.$i);
    $i++;
}
$excelWriter = PHPExcel_IOFactory::createWriter($excel, 'Excel5');
$excelWriter->save('the_export.xls');

As you can see, there are no formulas, no nothing... simple dumping of data from database into an excel file... Running PHP 5.3.2

like image 422
Lukáš Budoš Avatar asked Aug 02 '26 07:08

Lukáš Budoš


1 Answers

I'm guessing that one of the values retrieved from your database begins with an = sign, in which case PHPExcel will "guess" that it's supposed to be a formula and store it as such.

If you have values that begin with an = that aren't formulae, then you need to tell PHPExcel that it is a string and that it should be stored as such.

This behaviour is defined in the default cell binder (PHPExcel/Cell/DefaultValueBinder.php).

If you want to force PHPExcel to store these values as strings, then you can use the setCellValueExplicit() method (which defaults to enforcing a string)

Or you can write a custom value binder and apply that instead of the default value binder

like image 86
Mark Baker Avatar answered Aug 06 '26 19:08

Mark Baker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!