Domanda

I am now exporting country,network,No of SMS,Price/SMS,Total Cost in excel,but i want to display BILLING DATA,Client ID,Billing year,billing month on excel, i want like in the image can any one guide me how to do it please see my code below

enter image description here

Code

<?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(); 
?>
È stato utile?

Soluzione

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();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top