简体   繁体   English

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

[英]how to export an excel in php with columns background coloured?

I am trying to export an excel with the following code - 我正在尝试使用以下代码导出Excel-

$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("/\r?\n/", "\\n", $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)) . "\r\n";
      $flag = true;
    }
    array_walk($row, __NAMESPACE__ . '\cleanData');
    echo implode("\t", array_values($row)) . "\r\n";
   }

  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. 上面的代码对于导出excel来说工作正常,但是我希望excel的标头可以使用任何特定的颜色进行着色,但是找不到任何可以做到这一点的方法。 Please help. 请帮忙。

what you're exporting is a tab seperated *.csv file, not an xlsx. 您要导出的是制表符分隔的* .csv文件,而不是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 如果你想申请必须导出造型/使用类似产生一个真正的XLSX文件https://github.com/box/spouthttps://github.com/PHPOffice/PHPExcel或(无耻插头) /:HTTPS /github.com/nimmneun/onesheet

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM