OrderExport.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2021/11/19
  8. * Time: 18:07
  9. */
  10. namespace app\backend\modules\order\models;
  11. use Maatwebsite\Excel\Concerns\FromArray;
  12. use Maatwebsite\Excel\Concerns\WithMapping;
  13. use Maatwebsite\Excel\Concerns\WithColumnFormatting;
  14. use Maatwebsite\Excel\Concerns\ShouldAutoSize;
  15. use Maatwebsite\Excel\Concerns\WithEvents;
  16. use Maatwebsite\Excel\Events\AfterSheet;
  17. use Maatwebsite\Excel\Concerns\WithStyles;
  18. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  19. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  20. //临时使用
  21. class OrderExport implements FromArray,WithStyles
  22. {
  23. public $data;//订单数据
  24. public $status; //sheet名称(订单状态)
  25. public $column; //总行数
  26. public $goodsNum = []; //一个订单的商品数量
  27. public $mergeRows = [];//每笔订单需要单独合并单元格的数据
  28. public function __construct($data)
  29. {
  30. $this->data = $data;
  31. }
  32. public function array(): array
  33. {
  34. $this->column = count($this->data[0]);
  35. $list = [];
  36. foreach ($this->data as $key => $value) {
  37. if ($key > 0) {
  38. $column_array = [];
  39. $column = [];
  40. $orderMergeColumn = [
  41. 'total_row' => 1, //订单总合并列数
  42. 'goods_category' => [], //每个商品合并列数
  43. ];
  44. foreach ($value as $column_key => $column_value) {
  45. if (is_array($column_value)) {
  46. foreach ($column_value as $goods_key => $goods_value) {
  47. $goods_column = $column;
  48. $goods_column_array = [];
  49. foreach ($goods_value as $cate_key => $cate_value) {
  50. if (is_array($cate_value)) {
  51. $orderMergeColumn['goods_category'][] = count($cate_value);
  52. foreach ($cate_value as $val) {
  53. $goods_column_array[] = array_merge($goods_column, $val);
  54. }
  55. } else {
  56. if ($goods_column_array) {
  57. array_walk($goods_column_array, function (&$goods_column_array, $key, $value) {
  58. $goods_column_array[] = $value;
  59. }, $cate_value);
  60. } else {
  61. $goods_column[] = $cate_value;
  62. }
  63. }
  64. }
  65. if ($goods_column_array) {
  66. $column_array = array_merge($column_array, $goods_column_array);
  67. } else {
  68. $column_array = array_merge($column_array, [$goods_column]);
  69. }
  70. }
  71. $orderMergeColumn['total_row'] = max(count($column_value), array_sum($orderMergeColumn['goods_category']));
  72. $this->pushMergeNum($orderMergeColumn);
  73. } else {
  74. if ($column_array) {
  75. foreach ($column_array as $k => $v) {
  76. $column_array[$k][] = $column_value;
  77. }
  78. } else {
  79. $column[] = $column_value;
  80. }
  81. }
  82. }
  83. if ($column_array) {
  84. $list = array_merge($list, $column_array);
  85. } else {
  86. $list = array_merge($list, [$column]);
  87. }
  88. } else {
  89. $list[] = $value;
  90. }
  91. }
  92. // dd($list, $this->mergeRows);
  93. return $list;
  94. }
  95. public function pushMergeNum($data)
  96. {
  97. $this->mergeRows[] = $data;
  98. }
  99. public function styles(Worksheet $sheet)
  100. {
  101. //获取对应个数表格编号
  102. for($i=0;$i< $this->column;$i++) {
  103. $y = ($i / 26);
  104. if ($y >= 1) {
  105. $y = intval($y);
  106. $cell[] = chr($y + 64).chr($i - $y * 26 + 65);
  107. } else {
  108. $cell[] = chr($i + 65);
  109. }
  110. }
  111. // $cell = array_filter($cell, function ($v) {
  112. // return !in_array($v, ['L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W']);
  113. // });
  114. // $second = ['L', 'M', 'N', 'O', 'P', 'Q', 'R', 'V', 'W'];
  115. //不需要总合并合并的列
  116. $not_cells = ['L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y'];
  117. //商品需要合并的列
  118. $goods_yes_cells = ['L', 'M', 'N', 'O', 'P', 'Q', 'R','S','W','X', 'Y'];
  119. //设置样式
  120. $alignment = $sheet->getStyle('A1')->getAlignment();
  121. $alignment->setHorizontal(Alignment::HORIZONTAL_CENTER); //水平居中
  122. $alignment->setVertical(Alignment::VERTICAL_CENTER);////垂直居中
  123. foreach ($cell as $item) {
  124. $start = 2;
  125. $category_start = 2;
  126. foreach ($this->mergeRows as $key => $rowConfig) {
  127. $end = $start + $rowConfig['total_row'] - 1;
  128. if (!in_array($item, $not_cells)) {
  129. //复制样式
  130. $sheet->duplicateStyle($sheet->getStyle('A1'), $item . $start . ':' . $item . $end);
  131. if ($rowConfig['total_row'] > 1) {
  132. $sheet->mergeCells($item . $start . ':' . $item . $end); //合并单元格
  133. }
  134. }
  135. if (in_array($item, $goods_yes_cells) && $rowConfig['goods_category']) {
  136. foreach ($rowConfig['goods_category'] as $category_num) {
  137. $category_end = $category_start + $category_num - 1;
  138. if ($category_num > 1) {
  139. $sheet->mergeCells($item . $category_start . ':' . $item . $category_end); //合并单元格
  140. }
  141. $category_start = $category_end + 1;
  142. }
  143. }
  144. $start = $end + 1;
  145. }
  146. }
  147. // foreach ($cell as $item) {
  148. // $start = 2;
  149. // foreach ($this->goodsNum as $key => $value) {
  150. // $end = $start + array_sum($value['cate_num']) - 1;
  151. //
  152. // //复制样式
  153. // $sheet->duplicateStyle($sheet->getStyle('A1'), $item . $start . ':' . $item . $end);
  154. //
  155. // if (array_sum($value['cate_num']) > 1) {
  156. // $sheet->mergeCells($item . $start . ':' . $item . $end); //合并单元格
  157. // }
  158. //
  159. // $start = $end + 1;
  160. //
  161. // }
  162. // }
  163. // foreach($second as $item){
  164. // $start = 2;
  165. // foreach ($this->goodsNum as $key => $value) {
  166. // foreach($value['cate_num'] as $k=>$v){
  167. // $end = $start + $v - 1;
  168. // if ($v > 1) {
  169. // $sheet->mergeCells($item . $start . ':' . $item . $end); //合并单元格
  170. // }
  171. // $start = $end + 1;
  172. // }
  173. // }
  174. // }
  175. }
  176. }