7

I am new with PHPExcel and I need your help- I have problem with setCellValue in PHPExcel when calculating the sum. It gives me always 0.

Here is my code:

$objPHPExcel = PHPExcel_IOFactory::load("test.xls");

$row = 5; 
$S = $objPHPExcel->getActiveSheet();
while($row_data = mysql_fetch_array($result)){


$S->setCellValueExplicit('B'.$row, $row_data['cn']);
$S->setCellValueExplicit('C'.$row, $row_data['ld']);
$S->setCellValueExplicit('D'.$row, $row_data['cust_notify']);
$S->setCellValueExplicit('E'.$row, $row_data['code']);
$S->setCellValueExplicit('F'.$row, $row_data['company_name']);
$S->setCellValueExplicit('G'.$row, $row_data['rs']);
$S->setCellValueExplicit('H'.$row, $row_data['status']);
$S->setCellValueExplicit('I'.$row, $row_data['sueend']);
$S->setCellValueExplicit('J'.$row, $row_data['vclaimed']);
$S->setCellValueExplicit('K'.$row, $row_data['ref']);
$S->setCellValueExplicit('M'.$row, $row_data['out']);

$row++;

$S->setCellValue("I$row", "Total");
$S->setCellValue("J$row", "=SUM(J5:J".($row-1).")");

}

The result I get is always "0". Bellow the print screen RESULT EXCEL

I would really appreciate if someone can help me with this issue.

Thanks in advance

Lilou
  • 179
  • 1
  • 5
  • 14

2 Answers2

17

try this

$writer = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
$writer->setPreCalculateFormulas(true);
AlumnoPower
  • 181
  • 1
  • 5
  • 11
    Please try to flesh out your answer a bit more. Just posting a code block without any explanation or context is not good. In its current state your answer might not be of any use to people looking for an answer to this question in the future. – Xaver Kapeller May 14 '14 at 16:06
  • 1
    @XaverKapeller is right. But it still was the problem for me so +1 – DevDonkey Sep 29 '15 at 11:00
3

Oups I think I have found my mistake in the code. I have changed the $S->setCellValueExplicit('J'.$row, $row_data['vclaimed']);

to $S->setCellValue('J'.$row, $row_data['vclaimed']);

and now it works.

Lilou
  • 179
  • 1
  • 5
  • 14