Home » Php » php – calculating sum with PHPExcel

php – calculating sum with PHPExcel

Posted by: admin July 12, 2020 Leave a comment


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']);


$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

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

Thanks in advance

How to&Answers:

try this

$writer = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');


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.