June 6, 2016

Getting 2-dimensional array data to Excel File with PHP



 header('Content-type: application/ms-excel');
 header('Content-Disposition: attachment; 

 filename=REPORT_ORDER.csv');

 $fp = fopen("php://output", "w");

 $test = array ();
 $test[0]['collumn1'] = '00'; 
 $test[0]['collumn2'] = '10'; 
 $test[0]['collumn3'] = '01'; 
 $test[1]['collumn1'] = '11'; 
 $test[1]['collumn2'] = '20'; 
 $test[1]['collumn3'] = '21'; 

  //insert collumn names
 fputcsv($fp, array_keys($test[0]));

 //insert data rows
 for($x=0; $x
     fputcsv($fp, $test[$x]);
 }

 fclose($fp);
 exit;

?>