php 用 PHPExcel 计算总和

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15875744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:07:10  来源:igfitidea点击:

calculating sum with PHPExcel

phpsumphpexcel

提问by Lilou

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.

我是 PHPExcel 的新手,我需要你的帮助 - 我在计算总和时在 PHPExcel 中遇到 setCellValue 问题。它总是给我 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

我得到的结果总是“0”。打印屏幕下方 结果Excel

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

如果有人能帮助我解决这个问题,我将不胜感激。

Thanks in advance

提前致谢

回答by AlumnoPower

try this

尝试这个

$writer = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
$writer->setPreCalculateFormulas(true);

回答by Lilou

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

糟糕,我想我在代码中发现了我的错误。我已经改变了 $S->setCellValueExplicit('J'.$row, $row_data['vclaimed']);

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

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

and now it works.

现在它起作用了。