1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App\Exports;
  3. use App\Models\Mutation;
  4. use Illuminate\Contracts\Support\Responsable;
  5. use Illuminate\Contracts\View\View;
  6. use Illuminate\Http\Request;
  7. use Maatwebsite\Excel\Concerns\Exportable;
  8. use Maatwebsite\Excel\Concerns\FromView;
  9. use Maatwebsite\Excel\Concerns\ShouldAutoSize;
  10. use Maatwebsite\Excel\Concerns\WithStyles;
  11. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  12. class MutationExport implements ShouldAutoSize, Responsable, FromView, WithStyles
  13. {
  14. use Exportable;
  15. private $fileName = 'mutation-report.xls';
  16. public function __construct(private Request $request)
  17. {}
  18. public function view(): View
  19. {
  20. $mutations = Mutation::filter($this->request->only('startDate', 'endDate'))->get();
  21. return view('excel.mutation-report', compact('mutations'));
  22. }
  23. public function styles(Worksheet $sheet)
  24. {
  25. $lastRow = $sheet->getHighestDataRow();
  26. $lastSecondRow = $lastRow - 1;
  27. $lastThirdRow = $lastSecondRow - 1;
  28. $lastFourRow = $lastThirdRow - 1;
  29. return [
  30. 1 => [
  31. 'font' => ['bold' => true, 'size' => 12],
  32. 'alignment' => ['vertical' => 'center', 'horizontal' => 'center'],
  33. ],
  34. 2 => [
  35. 'font' => ['bold' => true, 'size' => 12],
  36. 'alignment' => ['vertical' => 'center', 'horizontal' => 'center'],
  37. ],
  38. 4 => [
  39. 'font' => ['bold' => true],
  40. ],
  41. $lastRow => [
  42. 'font' => ['bold' => true, 'size' => 12],
  43. 'alignment' => ['horizontal' => 'left'],
  44. ],
  45. $lastSecondRow => [
  46. 'font' => ['bold' => true, 'size' => 12],
  47. 'alignment' => ['horizontal' => 'left'],
  48. ],
  49. $lastThirdRow => [
  50. 'font' => ['bold' => true, 'size' => 12],
  51. 'alignment' => ['horizontal' => 'left'],
  52. ],
  53. $lastFourRow => [
  54. 'font' => ['bold' => true, 'size' => 12],
  55. 'alignment' => ['horizontal' => 'left'],
  56. ],
  57. ];
  58. }
  59. }