I can advise yoo to use PHPExcel. Below example based on your issue.
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();