如何在excel的最后一行打印数据

时间:2014-03-02 06:22:38

标签: php excel

我有一个显示客户端的下拉框,当选择客户端并单击导出按钮时,它将在前单元格中生成数据,但我想将此数据“确保正确”添加到生成的Excel的最后一行,但我不知道确切的最后一行是什么,因为每个客户都有不同的数据量,任何人都可以指导我,

我的代码

<?php
$dbHost = 'localhost'; // usually localhost
$dbUsername = 'xxx';
$dbPassword = 'xxxx';
$dbDatabase = 'xxxx';
$db = mysql_connect($dbHost, $dbUsername, $dbPassword) ;
mysql_select_db ($dbDatabase, $db) ;

require_once 'Classes/PHPExcel.php';

$clientid=$_POST['clientid'];

$date = date('d/m/Y', time());


$sql_select= "SELECT supplierprice.country, supplierprice.networkname, supplierprice.mcc, supplierprice.mnc, `$clientid`.clientprice, client_list.currency
FROM supplierprice
INNER JOIN `$clientid` ON supplierprice.supp_price_id = `$clientid`.net_id
INNER JOIN client_list ON `$clientid`.clientid = client_list.clientid
WHERE supplierprice.networkname <> 'default' and `$clientid`.clientprice <> 0
ORDER BY supplierprice.country, supplierprice.networkname ASC";
$queryRes = mysql_query($sql_select);

// start creating excel
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$sheet = $objPHPExcel->getActiveSheet();

// your data
 $sheet->setCellValueByColumnAndRow(0,1,$_POST['clientid']);


 $sheet->setCellValueByColumnAndRow(0,3,'Country');
 $sheet->setCellValueByColumnAndRow(4,3,'Client Price');
  $sheet->setCellValueByColumnAndRow(5,3,'Currency');

 // start list
 $offset = 4;
 $total_cost = 0;
 $total_sms = 0;

 while( $row = mysql_fetch_assoc($queryRes)){
   $sheet->setCellValueByColumnAndRow(0,$offset,$row['country']);
   $sheet->setCellValueByColumnAndRow(1,$offset,$row['networkname']);
   $sheet->setCellValueByColumnAndRow(2,$offset,$row['mcc']);
   $sheet->setCellValueByColumnAndRow(3,$offset,$row['mnc']);
   $sheet->setCellValueByColumnAndRow(4,$offset,$row['clientprice']);
   $sheet->setCellValueByColumnAndRow(5,$offset,$row['currency']);

   $total_cost += $row['clienttotalprice'];
   $total_sms += $row['client_inv_totalsms'];

   $offset++;
 }



 //OUTPUT 
 header("Content-Type:application/vnd.ms-excel");
 header("Content-Disposition:attachment;filename='export.xls'");
 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); 
 $objWriter->save('php://output');
 exit();
?>

1 个答案:

答案 0 :(得分:0)

可以通过调用

来识别工作表中的最后一行数据
$lastRow = $sheet->getHighestRow();