TraceExport.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace App\Exports;
  3. use Maatwebsite\Excel\Concerns\FromCollection;
  4. class TraceExport implements FromCollection
  5. {
  6. private $row;
  7. private $data;
  8. public function __construct($row,$data)
  9. {
  10. $this->row = $row;
  11. $this->data = $data;
  12. }
  13. public function collection()
  14. {
  15. $row = $this->row;
  16. $data = $this->data;
  17. //设置表头
  18. $key_arr=[];
  19. foreach ($row[0] as $key => $value) {
  20. $key_arr[] = $key;
  21. }
  22. //输入数据
  23. foreach ($data as $keys => $value) {
  24. $js = [];
  25. for ($i=0; $i < count($key_arr); $i++) {
  26. if($key_arr[$i]=='code'){
  27. $js = array_merge($js, [$key_arr[$i] => env('APP_URL').(string)$value[$key_arr[$i]]]);
  28. }else{
  29. $js = array_merge($js, [$key_arr[$i] => (string)$value[$key_arr[$i]]]);
  30. }
  31. }
  32. $row[]=$js;
  33. if($value['get_child']){
  34. foreach($value['get_child'] as $key=>$val){
  35. $js = [];
  36. for ($j=0; $j < count($key_arr); $j++) {
  37. if($key_arr[$j]=='code'){
  38. $js = array_merge($js, [$key_arr[$j] => env('APP_URL').(string)$val[$key_arr[$j]]]);
  39. }else{
  40. $js = array_merge($js, [$key_arr[$j] => (string)$val[$key_arr[$j]]]);
  41. }
  42. }
  43. $row[]=$js;
  44. if($val['get_child']){
  45. foreach($val['get_child'] as $k=>$v){
  46. $js=[];
  47. for ($z=0; $z < count($key_arr); $z++) {
  48. if($key_arr[$z]=='code'){
  49. $js = array_merge($js, [$key_arr[$z] => env('APP_URL').(string)$v[$key_arr[$z]]]);
  50. }else{
  51. $js = array_merge($js, [$key_arr[$z] => (string)$v[$key_arr[$z]]]);
  52. }
  53. }
  54. $row[]=$js;
  55. }
  56. }
  57. }
  58. }
  59. }
  60. return collect($row);
  61. }
  62. }