HistoryStockProductExport.php 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 HistoryStockProductExport implements
  12. FromView,
  13. Responsable,
  14. WithStyles,
  15. ShouldAutoSize,
  16. WithPreCalculateFormulas
  17. {
  18. use Exportable;
  19. private $fileName = "history-stock-product.xlsx";
  20. public function __construct(private array $data)
  21. {
  22. }
  23. public function view(): View
  24. {
  25. ["historyStockProducts" => $historyStockProducts] = $this->data;
  26. return view(
  27. "Excel.StockProducts.Export",
  28. compact("historyStockProducts")
  29. );
  30. }
  31. public function styles(Worksheet $sheet)
  32. {
  33. $lastRow = $sheet->getHighestDataRow();
  34. $lastContent = $lastRow - 1;
  35. $sheet->setCellValue("G$lastRow", "=SUM(G5:G$lastContent)");
  36. $sheet
  37. ->getStyle("G")
  38. ->getNumberFormat()
  39. ->setFormatCode("#,###");
  40. return [
  41. 1 => [
  42. "font" => ["bold" => true, "size" => 12],
  43. "alignment" => [
  44. "vertical" => "center",
  45. "horizontal" => "center",
  46. ],
  47. ],
  48. 2 => [
  49. "font" => ["bold" => true, "size" => 12],
  50. "alignment" => [
  51. "vertical" => "center",
  52. "horizontal" => "center",
  53. ],
  54. ],
  55. 4 => [
  56. "font" => ["bold" => true],
  57. ],
  58. $lastRow => [
  59. "font" => ["bold" => true, "size" => 12],
  60. ],
  61. ];
  62. }
  63. }