PHPExcel - 致命错误:未捕获的异常'异常'与消息

时间:2014-02-26 16:36:15

标签: php excel-formula phpexcel

每次尝试导出Excel电子表格时都会收到错误消息。错误如下:

Fatal error: Uncaught exception 'Exception' with message 'Worksheet!J6 -> Worksheet!J6 -> Cyclic Reference in Formula' in /classes/PHPExcel/Cell.php:293 Stack trace: 
    #0 /classes/PHPExcel/Writer/Excel2007/Worksheet.php(997): PHPExcel_Cell->getCalculatedValue() 
    #1 /classes/PHPExcel/Writer/Excel2007/Worksheet.php(943): PHPExcel_Writer_Excel2007_Worksheet->_writeCell(Object(PHPExcel_Shared_XMLWriter), Object(PHPExcel_Worksheet), 'J6', Array, Array) 
    #2 /classes/PHPExcel/Writer/Excel2007/Worksheet.php(83): PHPExcel_Writer_Excel2007_Worksheet->_writeSheetData(Object(PHPExcel_Shared_XMLWriter), Object(PHPExcel_Worksheet), Array) 
    #3 /classes/PHPExcel/Writer/Excel2007.php(282): PHPExcel_Writer_Excel2007_Worksheet->writeWorksheet(Object(PHPExcel_Worksheet), Array, false) 
    #4 /includes/reports_funcs.php(620): PHPExcel_Writer_Excel2007->save('reports/risk-bo...') #5 /createXlsxReport. in /classes/PHPExcel/Cell.php on line 293

我试图找一个解决方案,但到目前为止没有运气。我发现另一个stackoverflow问题建议在文件保存之前添加$objWriter->setPreCalculateFormulas(FALSE);,但这会给我一个空的电子表格。

它最近才开始发生,所以我真的很困惑为什么会这样。任何帮助将非常感谢,欢呼

示例代码 - 编辑

$objPHPExcel->getActiveSheet(0)
    ->setCellValueByColumnAndRow(0, $row, $pol_ref . 'e' . $end_numb)
    ->setCellValueByColumnAndRow(1, $row, $insured_name)
    ->setCellValueByColumnAndRow(2, $row, $vessel_name)
    ->setCellValueByColumnAndRow(3, $row, $insurance_type . ' - ' . $end_type)
    ->setCellValueByColumnAndRow(4, $row, $insured_domicile)
    ->setCellValueByColumnAndRow(5, $row, gmdate("d-m-Y",$date_logged))
    ->setCellValueByColumnAndRow(6, $row, $inception_date)
    ->setCellValueByColumnAndRow(7, $row, $expiry_date)
    ->setCellValueByColumnAndRow(8, $row, ($policy_limit == 0.00?$loh_limit:$policy_limit))
    ->setCellValueByColumnAndRow(9, $row, $premium)
    ->setCellValueByColumnAndRow(10, $row, "=sum(J".($row)."*".($seacurus_percent/100).")")
    ->setCellValueByColumnAndRow(11, $row, "=sum(J".($row)."*".($placing_percent/100).")")
    ->setCellValueByColumnAndRow(12, $row, "=sum(J".($row)."*".($rebate_percent/100).")")
    ->setCellValueByColumnAndRow(13, $row, "=sum(J".($row)."*".($iptpercent/100).")")
    ->setCellValueByColumnAndRow(14, $row, "=sum(".($binder_id==6?"(J".$row." - (P".$row."+L".$row."+O".$row."+P".$row."))":"J".($row))."*".$bgn_percent.")")
    ->setCellValueByColumnAndRow(15, $row, "=sum(J".($row)."*".$ghc_percent.")")
    ->setCellValueByColumnAndRow(16, $row, "=sum(J".($row)."*".($commission_percentage_sum/100).")");
$row++;

1 个答案:

答案 0 :(得分:1)

如果公式包含循环引用,则需要选择如何处理它们,就像在MS Excel中一样。默认行为是抛出这样的异常,这类似于MS Excel默认行为的方式。 另一种方法是通过将计算引擎设置为:

告诉PHPExcel遵循循环进行固定次数的迭代。
  

PHPExcel_Calculation :: getInstance() - > cyclicFormulaCount = 1;

或任何值> = 1表示要循环的迭代次数

  

PHPExcel_Calculation :: getInstance() - > cyclicFormulaCount = 10;

// 10次迭代