正如标题中所说,我在PHP中使用的SUM()函数总是显示" 0"在生成的文档中。
奇怪的是,当我直接在doc中执行相同的公式时,通过选择单元格等,结果很好:/
以下是代码:
public function generateDocument($name, $description, $params, $criteres, $options) {
// création du doc au format xsl
$doc = new \PHPExcel();
$doc->getProperties()->setCreator(UASG_NAME)
->setTitle($name)
->setDescription($description);
$doc->setActiveSheetIndex(0);
$worksheet = $doc->getActiveSheet();
// création du nom de l'onglet pour les datas
$worksheet->setTitle($this->createSheetTitle($name));
$dataService = new DataService($this->pdo, $this->context, $this->logger);
$table=$dataService->search($params, $criteres);
$this->startTimer("GENEREDATAS");
$headRow=null;$totalRow=null;
$this->currencyColumns = array();
$lineNumber = 1;
// ajout du titre en haut du fichier
if(isset($options->heading) && $options->heading!=""){
$worksheet->mergeCells('A1:Z2');
$worksheet->setCellValue('A1', $options->heading);
$worksheet->getStyle('A1')->getFont()->setSize(13);
$worksheet->getStyle('A1')->getFont()->setBold(true);
$previousLineNumber=4;
}else{
$previousLineNumber=0;
}
foreach ($table['rows'] as $rowindex => $row) {
$nbColumn=count($row['cells']);
foreach ($row['cells'] as $column => $cell) {
// header
if ($rowindex == 0) {
$title = $table['headers'][$column];
$this->setCellValue($worksheet, $column, $previousLineNumber + 1, $title);
$headRow = $previousLineNumber + 1;
}
// cellule
if ($row['level'] == 0) {
if ($column == 0) {
$lineNumber++;
}
$columnCell=$column;
$this->setCellValue($worksheet, $columnCell, $lineNumber + $previousLineNumber, $cell);
}
$worksheet->getColumnDimensionByColumn($column)->setAutoSize(true);
}
}
if(isset($options->totalPrice) && $options->totalPrice == 1){
$borderStyle= array(
'borders' => array(
'allborders' => array(
'style' => PHPExcel_Style_Border::BORDER_THICK
)
)
);
$worksheet->getStyleByColumnAndRow($nbColumn-1, $lineNumber+$previousLineNumber+2)->applyFromArray($borderStyle);
$worksheet->setCellValueByColumnAndRow($nbColumn-1, $lineNumber+$previousLineNumber+2, '=SUM(E2:E100)');
// '=SUM(F'.($previousLineNumber+2).':F'.($lineNumber+2).')');
}
if (isset($nbColumn)) {
$lastColumn = $nbColumn - 1;
// mise en forme des titres
$range = \PHPExcel_Cell::stringFromColumnIndex(0) . $headRow . ':' . \PHPExcel_Cell::stringFromColumnIndex($lastColumn) . $headRow;
$worksheet->getStyle($range)->applyFromArray($this->getHeadStyle());
$range = \PHPExcel_Cell::stringFromColumnIndex(0) . ($previousLineNumber) . ':' . \PHPExcel_Cell::stringFromColumnIndex($nbColumn) . ($lineNumber + $previousLineNumber + 1);
$worksheet->getStyle($range)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
// mise en forme des colonnes des devises
if (count($this->currencyColumns)>0) {
foreach($this->currencyColumns as $column) {
$range = \PHPExcel_Cell::stringFromColumnIndex($column) . ($previousLineNumber) . ':' . \PHPExcel_Cell::stringFromColumnIndex($column) . ($lineNumber + $previousLineNumber + 1);
$worksheet->getStyle($range)->getNumberFormat()->setFormatCode('#,##0.00_-[$' . $this->currencySymbol . ' ]');
}
}
unset($nbColumn);
}
$this->stopTimer("GENEREDATAS");
$doc->setActiveSheetIndex(0);
$this->startTimer("WRITE");
$writer = \PHPExcel_IOFactory::createWriter($doc, 'Excel2007');
//$writer->setPreCalculateFormulas(false);
$writer->save('php://output');
$this->stopTimer("WRITE");
}
我认为这可能是语言问题。因为我在英语Ubuntu中打开文档这可能是问题所在,但我不知道如何轻松地通过它...
如果有人可以提供帮助那就太棒了。
答案 0 :(得分:5)
你需要调整它
$writer->setPreCalculateFormulas(true);