Excel.class.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. require_once (__DIR__.SLASH.'..'.SLASH.'constant.php');
  3. require_once (LIB_PATH.'XLSXWriter'.SLASH.'XLSXWriter.class.php');
  4. require_once (LIB_PATH.'SimpleXLSX'.SLASH.'SimpleXLSX.class.php');
  5. class Excel
  6. {
  7. //Parse un fichier xlsx et retourne le contenu
  8. public static function parse($xlsxFile) {
  9. return SimpleXLSX::parse($xlsxFile);
  10. }
  11. //Retourne le contenu d'un tableau convertit au format XLSX
  12. public static function exportArray($rows, $mapping, $sheetname='Classeur 1'){
  13. $data = $header = array();
  14. $style = array(
  15. 'border' => 'top,left,bottom,right',
  16. 'border-style' => 'thin'
  17. );
  18. if(!isset($mapping) && isset($rows[0])){
  19. foreach($rows[0] as $key=>$value){
  20. $mapping[$key] = $key;
  21. }
  22. }
  23. foreach ($rows as $key => $item) {
  24. $itemContent = array();
  25. if (isset($mapping)) {
  26. foreach ($mapping as $label => $slug) {
  27. $header[$label] = 'string';
  28. isset($item[$slug]) ? array_push($itemContent, $item[$slug]) : array_push($itemContent, '');
  29. }
  30. } else {
  31. foreach ($item as $field => $value) {
  32. array_push($itemContent, $item[$field]);
  33. }
  34. }
  35. array_push($data, $itemContent);
  36. }
  37. ob_start();
  38. $excel = new XLSXWriter();
  39. $excel->writeSheetHeader($sheetname, $header, array_merge($style, array('font-style'=> 'bold','border-color' => '#2a2a28')));
  40. foreach($data as $row)
  41. $excel->writeSheetRow($sheetname, $row, array_merge($style, array('border-color' => '#cecece')));
  42. $excel->writeToStdOut();
  43. $output = ob_get_clean();
  44. return $output;
  45. }
  46. /**
  47. * Export de données sur plusieurs feuilles Excel
  48. * @param array $content [Contenu à exporter]
  49. * Format du tableau de contenu attendu :
  50. * array(
  51. * 'Classeur 1' => array(
  52. * 'mapping' => array(
  53. * 'Libellé' => 'slug',
  54. * 'Libellé' => 'slug',
  55. * 'Libellé' => 'slug'
  56. * ),
  57. * 'rows' => array(
  58. * 0 => objet/entité exporté,
  59. * 1 => objet/entité exporté,
  60. * 2 => objet/entité exporté
  61. * )
  62. * ),
  63. * 'Classeur 2' => array(
  64. * 'mapping' => array(
  65. * 'Libellé' => 'slug',
  66. * 'Libellé' => 'slug',
  67. * 'Libellé' => 'slug'
  68. * ),
  69. * 'rows' => array(
  70. * 0 => objet/entité exporté,
  71. * 1 => objet/entité exporté,
  72. * 2 => objet/entité exporté
  73. * )
  74. * ), etc...
  75. * );
  76. * On place généralement $response['rows'] dans l'index 'rows' d'une feuille Excel
  77. *
  78. * @param string $filename [Nom du fichier à exporter]
  79. *
  80. * @return string [Fichier XSLX]
  81. */
  82. public static function exportMultipleSheets($content){
  83. $commonStyle = array('border'=> 'top,left,bottom,right', 'border-style'=> 'thin');
  84. $headerStyle = array_merge($commonStyle, array('font-style'=> 'bold','border-color'=> '#2a2a28'));
  85. $contentStyle = array_merge($commonStyle, array('border-color'=> '#cecece'));
  86. $styles = array(
  87. 'header' => $headerStyle,
  88. 'content'=> $contentStyle
  89. );
  90. $excel = new XLSXWriter();
  91. foreach ($content as $sheetName => $sheetContent) {
  92. $data = $header = array();
  93. foreach ($sheetContent['rows'] as $key => $item) {
  94. $itemContent = array();
  95. if (isset($sheetContent['mapping'])) {
  96. foreach ($sheetContent['mapping'] as $label => $slug) {
  97. $header[$label] = 'string';
  98. isset($item[$slug]) ? array_push($itemContent, $item[$slug]) : array_push($itemContent, '');
  99. }
  100. } else {
  101. foreach ($item as $field => $value) {
  102. array_push($itemContent, $item[$field]);
  103. }
  104. }
  105. array_push($data, $itemContent);
  106. }
  107. $excel->writeSheet($data, $sheetName, $header, $styles);
  108. }
  109. ob_start();
  110. $excel->writeToStdOut();
  111. $output = ob_get_clean();
  112. return $output;
  113. }
  114. }