/
v.bolshakov
/
AIEcosystem-Testing
Обзор
Документация
Войти
/
v.bolshakov
/
AIEcosystem-Testing
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
common/components/ExcelGenerator.php
183 строки
5 KB
Developer
Initial commit
03 авг 2026, 17:43
03 авг 2026, 17:43
7433917
Код
Авторство
О чём код?
<?php namespace common\components; use common\components\base\BaseFileGenerator; use PhpOffice\PhpSpreadsheet\Cell\Coordinate; use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Style\Fill; use PhpOffice\PhpSpreadsheet\Style\Font; use PhpOffice\PhpSpreadsheet\Style\Style; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PhpOffice\PhpSpreadsheet\Writer\Exception; use yii\helpers\ArrayHelper; /** * Базовый класс для генерации файлов в формате Excel * * @author Dmitry E. Semenov <sde.tomsk@gmail.com> * @copyright Self (c) 2020 */ abstract class ExcelGenerator extends BaseFileGenerator { /** * Объек Execl с которым работаем в настоящее время * @var Spreadsheet */ protected $spreadsheet = null; /** * Расширение файла * @var string */ protected $extension = 'Xlsx'; /** * @inheritdoc */ protected function getExtension() { return strtolower($this->extension); } /** * @inheritdoc */ protected function load() { if ($this->template) { parent::load(); $this->spreadsheet = IOFactory::load($this->template); } else { $this->spreadsheet = new Spreadsheet(); } return true; } /** * @return bool * @throws Exception */ protected function save() { $path = $this->getTempFileName(); $writer = IOFactory::createWriter($this->spreadsheet, $this->extension); $writer->save($path); $this->tmp_path = $path; return true; } /** * Установить матрицу * @param $sheet * @param $matrix * @param integer $j колонка * @param integer $i строка * @param array $options * @param int $j_pad * @return int * @throws \Exception */ protected function setMatrix($sheet, $matrix, $j, $i, $options = [], $j_pad = 0) { $k = $i; $delta = 1; foreach ((array)$matrix as $index => $row) { $delta = $this->setValue($sheet, $row, $j, $k, $options, $j_pad); $k++; } if ($j_pad) { return min($delta, $j_pad); } else { return $delta; } } /** * Установить значение * * @param Worksheet $sheet Лист * @param mixed $value Значение - которые добавляем в лист * @param int $j колонка * @param int $i строка * @param array $options формат текста * @param int $j_pad * @return int * @throws \Exception */ protected function setValue($sheet, $value, $j, $i, $options = [], $j_pad = 0) { if (is_null($value)) { return 1; } else { $value = (array)$value; // стартовая колонка для вывод данных $start_j = $j; // выводим все по колонкам foreach ($value as $index => $cell_val) { if ($cell_val) { // расчёт индекса колонки $j = $start_j + $index; $sheet->setCellValue([$j, $i], $cell_val); $str = Coordinate::stringFromColumnIndex($j) . $i; $font = ArrayHelper::getValue($options, 'font'); if ($font) { /** @var Style $style */ $style = $sheet ->getStyle($str); /** @var Font $_font */ $_font = $style->getFont(); $_font->applyFromArray($font); } $bg_color = ArrayHelper::getValue($options, 'bg_color'); if ($bg_color) { if (is_array($bg_color)) { $color = ArrayHelper::getValue($bg_color, $j); } else { $color = $bg_color; } // закрашиваем цвет ячеек /** @var Style $style */ $style = $sheet ->getStyle($str); $style->getFill() ->applyFromArray([ 'fillType' => Fill::FILL_SOLID, 'startColor' => [ 'rgb' => $color ] ]); } $format_cell = ArrayHelper::getValue($options, 'format_cell'); if ($format_cell) { // устанавливаем формат ячейки if ($format_id = ArrayHelper::getValue($format_cell, $j)) { $sheet ->getStyle($str) ->getNumberFormat() ->setFormatCode($format_id); } } } } if ($j_pad) { return $j_pad; } else { return count($value); } } } }