123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- require_once (__DIR__.SLASH.'..'.SLASH.'constant.php');
- require_once (LIB_PATH.'XLSXWriter'.SLASH.'XLSXWriter.class.php');
- require_once (LIB_PATH.'SimpleXLSX'.SLASH.'SimpleXLSX.class.php');
- class Excel
- {
- //Parse un fichier xlsx et retourne le contenu
- public static function parse($xlsxFile) {
- return SimpleXLSX::parse($xlsxFile);
- }
-
- //Retourne le contenu d'un tableau convertit au format XLSX
- public static function exportArray($rows, $mapping, $sheetname='Classeur 1'){
- $data = $header = array();
- $style = array(
- 'border' => 'top,left,bottom,right',
- 'border-style' => 'thin'
- );
- if(!isset($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 => $slug) {
- $header[$label] = '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 => $slug) {
- $header[$label] = '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;
- }
- }
|