HEX
Server: nginx/1.18.0
System: Linux test-ipsremont 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
User: ips (1000)
PHP: 8.0.30
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/ipsremont-demo/app/Services/ExcelService.php
<?php

namespace App\Services;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Color;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PhpOffice\PhpSpreadsheet\Writer\Xls;

class ExcelService
{

    public array $rows = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
    public string $center = Alignment::HORIZONTAL_CENTER;
    public string $right = Alignment::HORIZONTAL_RIGHT;
    public array $borders = ['allBorders' => ['borderStyle' => Border::BORDER_THIN]];
    public Spreadsheet $spreadsheet;
    public Worksheet $sheet;

    /**
     * ExcelService constructor.
     */
    public function __construct()
    {
        $this->spreadsheet = new Spreadsheet();
        $this->sheet = $this->spreadsheet->getActiveSheet();
    }

    public function getSheet(): Worksheet
    {
        return $this->sheet;
    }

    public function setColor($cell)
    {
        $this->getSheet()->getStyle($cell)->getFont()->getColor()->setARGB(Color::COLOR_RED);
    }

    /**
     * @param $cell
     * @param $h_alignment
     * @param $font
     * @param $bold
     * @param $size
     * @param $borders
     */
    public function setStyle($cell, $h_alignment, $font, $bold, $size, $borders)
    {
        $styles = [
            'font' => [
                'name' => $font,
                'bold' => $bold,
                'size' => $size,
            ],
            'alignment' => [
                'horizontal' => $h_alignment,
                'vertical' => Alignment::VERTICAL_CENTER,
            ],
        ];

        if (false === $borders) {
            $styles['wrap'] = true;
        } else {
            if (true === $borders) {
                $styles['borders'] = $this->borders;
            } else {
                $borders = explode(',', $borders);
                $fullBorders = [];
                foreach ($borders as $border) {
                    $fullBorders[$border] = ['borderStyle' => Border::BORDER_THIN];
                }
                $styles['borders'] = $fullBorders;
            }
        }
        $this->sheet->getStyle($cell)->applyFromArray($styles);
    }

    public function setCellValue($cell, $text, $font, $size, $bold, $h_alignment = '', $row_h = [], $col_w = [], $merge = '', $borders = false)
    {
        if ($merge) {
            $this->mergeCells($merge);
        }
        if (!empty($row_h)) {
            $this->setRowHeight($row_h[0], $row_h[1]);
        }
        if (!empty($col_w)) {
            $this->setColumnWidth($col_w[0], $col_w[1]);
        }

        $this->setStyle($cell, $h_alignment, $font, $bold, $size, $borders);
        $this->sheet->setCellValue($cell, $text);
    }

    public function generateDocument($file_path)
    {
        $writer = new Xls($this->spreadsheet);
        $writer->save($file_path);
    }

    public function mergeCells($cells)
    {
        $this->sheet->mergeCells($cells);
    }

    public function setColumnWidth($column, $width)
    {
        $this->sheet->getColumnDimension($column)->setWidth($width);
    }

    public function setRowHeight($row, $height)
    {
        $this->sheet->getRowDimension($row)->setRowHeight($height);
    }

    public function setWrapText($cellCoordinate)
    {
        $this->sheet->getStyle($cellCoordinate)->getAlignment()->setWrapText(true);
    }
}