creating excel file in php

Here $booking_pop  is an sql result array passed through booking_export_excel function  

 $booking_pop = $wpdb->get_results('SELECT sku_code as SKUCODE ,barcode AS Barcode ,asinno as ASINNO FROM amazon_product_info where status=1');
 if(count($booking_pop)>0){
 $filename = "amzon_export.xls"; 
 header("Content-Disposition: attachment; filename=\"$filename\""); 
 header("Content-Type: application/vnd.ms-excel"); 
  booking_export_excel_gen($booking_pop); 
 }
 
function booking_export_excel_gen($booking_det) {
 $val_array = json_decode(json_encode($booking_det), true);
 $heading = false;
 if(!empty($val_array))
 foreach($val_array as $row) {
 
 if($row['ASINNO']!=''){
 
 if(!$heading) {
 echo implode("\t", array_keys($row)) . "\n";
 $heading = true;
 }
 echo implode("\t", array_values($row)) . "\n";
 }
 } 
 }

Leave a Reply

Your email address will not be published. Required fields are marked *