我现在正在出口国家,网络,短信,价格/短信,excel中的总成本,但我想在Excel上显示BILLING DATA,客户ID,结算年份,结算月份,我想在图像中可以任何人都指导我如何操作,请参阅下面的代码
代码
<?php
$dbHost = 'localhost'; // usually localhost
$dbUsername = 'xxx';
$dbPassword = 'xxxxxx';
$dbDatabase = 'xxxxxx';
$db = mysql_connect($dbHost, $dbUsername, $dbPassword) or die ("Unable to connect to Database Server.");
mysql_select_db ($dbDatabase, $db) or die ("Could not select database.");
$clientid=$_POST['clientid'];
$id=$_POST['billingmonthid'];
$billingmonth=$_POST['billingmonth'];
$billingyear=$_POST['billingyear'];
//print $clientid;
$sql_select= "SELECT country,networkname,client_inv_totalsms,client_inv_costpersms,clienttotalprice FROM billingdetailedreport where billingmonthid = '$id' and clientid = '$clientid' and billingmonth = '$billingmonth' and billingyear='$billingyear'";
$queryRes = mysql_query($sql_select);
//print $sql_select;
$header = "Country" . "\t";
$header .= "Network" . "\t";
$header .= "No of SMS" . "\t";
$header .= "Price/SMS" . "\t";
$header .= "Total Cost" . "\t";
//Reading the data thro' POST
$data = '';
while( $row = mysql_fetch_assoc($queryRes)){
$row1 = array();
$row1[] = $row['country'];
$row1[] = $row['networkname'];
$row1[] = $row['client_inv_totalsms'];
$row1[] = $row['client_inv_costpersms'];
$row1[] = $row['clienttotalprice'];
$data .= join("\t", $row1)."\n";
//$data= $first_name."\t";
//$data .= $row['originator']."\t";
}
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=expotdetailedreport.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
exit();
?>
答案 0 :(得分:1)
我可以建议yoo使用PHPExcel。以下示例基于您的问题。
require_once 'Classes/PHPExcel.php';
// get data
$dbHost = 'localhost'; // usually localhost
$dbUsername = 'xxx';
$dbPassword = 'xxxxxx';
$dbDatabase = 'xxxxxx';
$db = mysql_connect($dbHost, $dbUsername, $dbPassword) or die ("Unable to connect to Database Server.");
mysql_select_db ($dbDatabase, $db) or die ("Could not select database.");
$sql_select= "SELECT country,networkname,client_inv_totalsms,client_inv_costpersms,clienttotalprice FROM billingdetailedreport where billingmonthid = '$id' and clientid = '$clientid' and billingmonth = '$billingmonth' and billingyear='$billingyear'";
$queryRes = mysql_query($sql_select);
// start creating excel
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$sheet = $objPHPExcel->getActiveSheet();
// your data
$sheet->setCellValueByColumnAndRow(0,1,'BILLING DATA');
sheet->setCellValueByColumnAndRow(0,2,'Client ID');
$sheet->setCellValueByColumnAndRow(1,2,$_POST['clientid']);
$sheet->setCellValueByColumnAndRow(0,3,'Billing year,');
$sheet->setCellValueByColumnAndRow(1,3,$_POST['billingyear']);
$sheet->setCellValueByColumnAndRow(0,4,'Billing month');
$sheet->setCellValueByColumnAndRow(1,4,$_POST['billingyear']);
$sheet->setCellValueByColumnAndRow(0,6,'Country');
$sheet->setCellValueByColumnAndRow(1,6,'Network');
$sheet->setCellValueByColumnAndRow(2,6,'No of SMS');
$sheet->setCellValueByColumnAndRow(3,6,'Price/SMS');
$sheet->setCellValueByColumnAndRow(4,6,'Total Cost');
// start list
$offset = 7;
$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['client_inv_totalsms']);
$sheet->setCellValueByColumnAndRow(3,$offset,$row['client_inv_costpersms']);
$sheet->setCellValueByColumnAndRow(4,$offset,$row['clienttotalprice']);
$total_cost += $row['clienttotalprice'];
$total_sms += ['client_inv_totalsms'];
$offset++;
}
$sheet->setCellValueByColumnAndRow(0,$offset, 'TOTAL');
$sheet->setCellValueByColumnAndRow(2,$offset, $total_sms);
$sheet->setCellValueByColumnAndRow(4,$offset, $total_cost);
//OUTPUT
header("Content-Type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename='example.xls'");
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit();