PurchaseDetailsExport.php 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace App\Exports;
  3. use Illuminate\Contracts\View\View;
  4. use Maatwebsite\Excel\Concerns\FromView;
  5. use Maatwebsite\Excel\Concerns\Exportable;
  6. use Maatwebsite\Excel\Concerns\WithStyles;
  7. use Illuminate\Contracts\Support\Responsable;
  8. use Maatwebsite\Excel\Concerns\ShouldAutoSize;
  9. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  10. use Maatwebsite\Excel\Concerns\WithPreCalculateFormulas;
  11. class PurchaseDetailsExport implements
  12. FromView,
  13. Responsable,
  14. WithStyles,
  15. ShouldAutoSize,
  16. WithPreCalculateFormulas
  17. {
  18. use Exportable;
  19. private $fileName = "purchase-report.xlsx";
  20. public function __construct(private array $data)
  21. {
  22. }
  23. public function view(): View
  24. {
  25. ["purchases" => $purchases] = $this->data;
  26. return view("Excel.Purchases.Export", compact("purchases"));
  27. }
  28. public function styles(Worksheet $sheet)
  29. {
  30. $lastRow = $sheet->getHighestDataRow();
  31. $lastContent = $lastRow - 1;
  32. $sheet->setCellValue("E$lastRow", "=SUM(E5:E$lastContent)");
  33. $sheet
  34. ->getStyle("E")
  35. ->getNumberFormat()
  36. ->setFormatCode("#,###");
  37. return [
  38. 1 => [
  39. "font" => ["bold" => true, "size" => 12],
  40. "alignment" => [
  41. "vertical" => "center",
  42. "horizontal" => "center",
  43. ],
  44. ],
  45. 2 => [
  46. "font" => ["bold" => true, "size" => 12],
  47. "alignment" => [
  48. "vertical" => "center",
  49. "horizontal" => "center",
  50. ],
  51. ],
  52. 4 => [
  53. "font" => ["bold" => true],
  54. ],
  55. $lastRow => [
  56. "font" => ["bold" => true, "size" => 12],
  57. ],
  58. ];
  59. }
  60. }