如何在列背景色的php中导出excel?

I am trying to export an excel with the following code -

$data = array(
    array("firstname" => "Mary", "lastname" => "Johnson", "age" => 25),
    array("firstname" => "Amanda", "lastname" => "Miller", "age" => 18),
    array("firstname" => "James", "lastname" => "Brown", "age" => 31),
    array("firstname" => "Patricia", "lastname" => "Williams", "age" => 7),
    array("firstname" => "Michael", "lastname" => "Davis", "age" => 43),
    array("firstname" => "Sarah", "lastname" => "Miller", "age" => 24),
    array("firstname" => "Patrick", "lastname" => "Miller", "age" => 27)
  );

  function cleanData(&$str)
  {
    $str = preg_replace("/\t/", "\\t", $str);
    $str = preg_replace("/?
/", "\
", $str);
    if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
  }

  $filename = "website_data_" . date('Ymd') . ".xls";

  header("Content-Disposition: attachment; filename=\"$filename\"");
  header("Content-Type: application/excel");

   $flag = false;
  foreach($data as $row)
  {
    if(!$flag){

      echo implode("\t", array_keys($row)) . "
";
      $flag = true;
    }
    array_walk($row, __NAMESPACE__ . '\cleanData');
    echo implode("\t", array_values($row)) . "
";
   }

  exit;

The above code is working fine for exporting excel, but i want the header of the excel to be coloured in any specific colour, but could not found anything to do this. Please help.

what you're exporting is a tab seperated *.csv file, not an xlsx.

If you want to apply styling you have to export/generate a real xlsx file using something like https://github.com/box/spout, https://github.com/PHPOffice/PHPExcel or (shameless plug) https://github.com/nimmneun/onesheet