public static function to_excel(array $data, string $file, bool $with_titles = true): bool
  {
    $checked = false;
    $todo = [];
    foreach ( $data as $d ){
      if ( !$checked && self::is_assoc($d) ){
        if ( $with_titles ){
          $line1 = [];
          $line2 = [];
          foreach ( $d as $k => $v ){
            $line1[] = $k;
            $line2[] = '';
          }
          $todo[] = $line1;
          $todo[] = $line2;
        }
        $checked = true;
      }
      $todo[] = array_values($d);
    }
    if ( count($todo) ){
      $objPHPExcel = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
      $objPHPExcel->getActiveSheet()->fromArray($todo, NULL, 'A1');
      $objPHPExcel
        ->getDefaultStyle()
        ->getNumberFormat()
        ->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
      $ow = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($objPHPExcel);
      $ow->save($file);
      return \is_file($file);
    }
    return false;
  }
public static function to_excel(array $data, string $file, bool $with_titles = true): bool
  {
    $checked = false;
    $todo = [];
    foreach ( $data as $d ){
      if ( !$checked && self::is_assoc($d) ){
        if ( $with_titles ){
          $line1 = [];
          $line2 = [];
          foreach ( $d as $k => $v ){
            $line1[] = $k;
            $line2[] = '';
          }
          $todo[] = $line1;
          $todo[] = $line2;
        }
        $checked = true;
      }
      $todo[] = array_values($d);
    }
    if ( count($todo) ){
      $objPHPExcel = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
      $objPHPExcel->getActiveSheet()->fromArray($todo, NULL, 'A1');
      $objPHPExcel
        ->getDefaultStyle()
        ->getNumberFormat()
        ->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
      $ow = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($objPHPExcel);
      $ow->save($file);
      return \is_file($file);
    }
    return false;
  }
public static function to_excel ( array $ data , string $ file , bool $ with_titles = true ) : bool { $ checked = false ; $ todo = [ ] ; foreach ( $ data as $ d ) { if ( ! $ checked && self :: is_assoc ( $ d ) ) { if ( $ with_titles ) { $ line1 = [ ] ; $ line2 = [ ] ; foreach ( $ d as $ k => $ v ) { $ line1 [ ] = $ k ; $ line2 [ ] = '' ; } $ todo [ ] = $ line1 ; $ todo [ ] = $ line2 ; } $ checked = true ; } $ todo [ ] = array_values ( $ d ) ; } if ( count ( $ todo ) ) { $ objPHPExcel = new \ PhpOffice \ PhpSpreadsheet \ Spreadsheet ( ) ; $ objPHPExcel -> getActiveSheet ( ) -> fromArray ( $ todo , NULL , 'A1' ) ; $ objPHPExcel -> getDefaultStyle ( ) -> getNumberFormat ( ) -> setFormatCode ( \ PhpOffice \ PhpSpreadsheet \ Style \ NumberFormat :: FORMAT_TEXT ) ; $ ow = new \ PhpOffice \ PhpSpreadsheet \ Writer \ Xlsx ( $ objPHPExcel ) ; $ ow -> save ( $ file ) ; return \ is_file ( $ file ) ; } return false ; }
Creates an Excel file from a given array.

@param array $array The array to export
@param string $file The file path
@param bool $with_titles Set it to false if you don't want the columns titles. Default true
@return bool
Creates an Excel file from a given array .