'clé', * 'Autre label' => 'autre clé', * etc... * ); * @param string $sheetname le nom à donner à la feuille de calcul courante * @return string le stream du fichier généré */ public static function exportArray($rows, $mapping, $sheetname='Classeur 1'){ $data = $header = array(); $style = array( 'border' => 'top,left,bottom,right', 'border-style' => 'thin' ); if(empty($mapping) && isset($rows[0])){ foreach($rows[0] as $key=>$value){ $mapping[$key] = $key; } } foreach ($rows as $key => $item) { $itemContent = array(); if (isset($mapping)) { foreach ($mapping as $label => $attributes) { $slug = is_array($attributes) ? (isset($attributes['slug']) ? $attributes['slug'] : '') : $attributes; $header[$label] = (is_array($attributes) && isset($attributes['type'])) ? $attributes['type'] : 'string'; isset($item[$slug]) ? array_push($itemContent, $item[$slug]) : array_push($itemContent, ''); } } else { foreach ($item as $field => $value) { array_push($itemContent, $item[$field]); } } array_push($data, $itemContent); } ob_start(); $excel = new XLSXWriter(); $excel->writeSheetHeader($sheetname, $header, array_merge($style, array('font-style'=> 'bold','border-color' => '#2a2a28'))); foreach($data as $row) $excel->writeSheetRow($sheetname, $row, array_merge($style, array('border-color' => '#cecece'))); $excel->writeToStdOut(); $output = ob_get_clean(); return $output; } /** * Export de données sur plusieurs feuilles Excel * @param array $content [Contenu à exporter] * Format du tableau de contenu attendu : * array( * 'Classeur 1' => array( * 'mapping' => array( * 'Libellé' => 'slug', * 'Libellé' => 'slug', * 'Libellé' => 'slug' * ), * 'rows' => array( * 0 => objet/entité exporté, * 1 => objet/entité exporté, * 2 => objet/entité exporté * ) * ), * 'Classeur 2' => array( * 'mapping' => array( * 'Libellé' => 'slug', * 'Libellé' => 'slug', * 'Libellé' => 'slug' * ), * 'rows' => array( * 0 => objet/entité exporté, * 1 => objet/entité exporté, * 2 => objet/entité exporté * ) * ), etc... * ); * On place généralement $response['rows'] dans l'index 'rows' d'une feuille Excel * * @param string $filename [Nom du fichier à exporter] * * @return string [Fichier XSLX] */ public static function exportMultipleSheets($content){ $commonStyle = array('border'=> 'top,left,bottom,right', 'border-style'=> 'thin'); $headerStyle = array_merge($commonStyle, array('font-style'=> 'bold','border-color'=> '#2a2a28')); $contentStyle = array_merge($commonStyle, array('border-color'=> '#cecece')); $styles = array( 'header' => $headerStyle, 'content'=> $contentStyle ); $excel = new XLSXWriter(); foreach ($content as $sheetName => $sheetContent) { $data = $header = array(); foreach ($sheetContent['rows'] as $key => $item) { $itemContent = array(); if (isset($sheetContent['mapping'])) { foreach ($sheetContent['mapping'] as $label => $attributes) { $slug = is_array($attributes) ? (isset($attributes['slug']) ? $attributes['slug'] : '') : $attributes; $header[$label] = (is_array($attributes) && isset($attributes['type'])) ? $attributes['type'] : 'string'; isset($item[$slug]) ? array_push($itemContent, $item[$slug]) : array_push($itemContent, ''); } } else { foreach ($item as $field => $value) { array_push($itemContent, $item[$field]); } } array_push($data, $itemContent); } $excel->writeSheet($data, $sheetName, $header, $styles); } ob_start(); $excel->writeToStdOut(); $output = ob_get_clean(); return $output; } }