2013-04-08 47 views
6

我是PHPExcel的新手,我需要您的幫助 - 我在計算總和時遇到了PHPExcel中的setCellValue問題。這讓我始終爲0用PHPExcel計算總數

這裏是我的代碼:

$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).")"); 

} 

結果我得到的是始終爲「0」。波紋管的打印屏幕 RESULT EXCEL

我真的很感激,如果有人可以幫我解決這個問題。

在此先感謝

回答

3

Oups我想我已經找到了我的代碼錯誤。我已經改變了 $S->setCellValueExplicit('J'.$row, $row_data['vclaimed']);

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

,現在它的工作原理。

15

試試這個

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

請儘量充實你的答案多一點。只是在沒有任何解釋或上下文的情況下發布代碼塊並不好。在目前的狀態下,你的答案可能對將來尋找這個問題的答案的人沒有任何用處。 – 2014-05-14 16:06:22

+1

@XaverKapeller是對的。但它仍然是我的問題,所以+1 – DevDonkey 2015-09-29 11:00:06