Prevent PHPExcel to calculate values when writing

2019-06-19 19:49发布

When writing my data in an Excel file with class PHPExcel.

This is happening when the writer works, not when adding data to the cells $this->sheet->setCellValue()

Fatal error: Uncaught exception 'PHPExcel_Calculation_Exception' with message 'Adressen!AF20955 -> Formula Error: Unexpected operator '>'' in \Cell.php:300 Stack trace: 
#0 \PHPExcel\Worksheet.php(754): PHPExcel_Cell->getCalculatedValue()
#1 \PHPExcel\Writer\Excel2007\Worksheet.php(373): PHPExcel_Worksheet->calculateColumnWidths()
#2 \PHPExcel\Writer\Excel2007\Worksheet.php(80): PHPExcel_Writer_Excel2007_Worksheet->_writeCols(Object(PHPExcel_Shared_XMLWriter), Object(PHPExcel_Worksheet))
#3 \PHPExcel\Writer\Excel2007.php(304): PHPExcel_Writer_Excel2007_Worksheet->writeWorksheet(Object(PHPExcel_Worksheet), Array, false)
#4 \excel.php(131): PHPExcel_Writer_Excel2007->save('...')
#5 \excel. in \PHPExcel\Cell.php on line 300

I never use the function PHPExcel_Cell->getCalculatedValue() writing to my Excel but only $this->sheet->setCellValue('A1', $value).

I don't need to calculate any formula in my Excel file. Just exporting DB2 data to this file.

It is obviously possible that $value contains formula characters like = + - > < (as the cell AF20955 did causing the fatal error with >) but this should not be interpreted as formula but only as part of string. How can I solve the problem?

1条回答
放我归山
2楼-- · 2019-06-19 20:17

The PHPExcel calculation engine is called by default when you save, but you can tell the writer not to apply it by calling

$objWriter->setPreCalculateFormulas(false);

before the call to save


When you set a cell value with a call to setCellValue() or similar, PHPExcel will attempt to identify the datatype and set it appropriately internally (in exactly the same way as MS Excel) using the value binder. Cells that contain a = as the first character will be considered as formulae by this.

If you want to ensure that they are treated as strings instead of formulae, then the easiest approach is to use setCellValueExplicit() instead.

查看更多
登录 后发表回答