I am trying to export db rows using fputcsv() in csv file. how i can add heading on first, center align then columns then data my code works well without heading. I know there is many api's but is this possible with in my code.
Here is my code:-
Enquiry Report
id name class func
1 rk ba call()
2 bk bd that()
function exportdata_to_excel($details) {
// filename for download
$filename = date("Y-m-d").".csv";
header('Content-Type: text/csv');
header("Content-Disposition: attachment; filename=\"$filename\"");
$out = fopen("php://output", 'w');
$flag = false;
//$result = $orderDetails;
while($row = mysql_fetch_assoc($details)) {
$arr =array('Enquiry id'=>$row['id'],'Date'=>$row['created_on'],'Name'=>$row['name'], 'Email'=>$row['email'], 'Telephone'=>$row['telephone'], 'Customer Request'=>$row['customer_request'], 'Special Request'=>$row['special_request']);
if(!$flag) {
// display field/column names as first row
fputcsv($out, array_keys($arr), ',', '"');
$flag = true;
}
fputcsv($out, array_values($arr), ',', '"');
}
fclose($out);
exit();
}
CSV
, this file format is designed to stores data only. If you want to make a nice looking data sheet, you will need to export it intoXSLX
, and without third-party api it would be very hard. – Lermontov