Форматирование

This commit is contained in:
brusnitsyn
2026-04-24 16:46:10 +09:00
parent fd0e6ee817
commit 63daa62888
87 changed files with 1380 additions and 791 deletions

View File

@@ -7,16 +7,18 @@ use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class StatisticsExport implements FromCollection, WithHeadings, WithMapping, WithStyles
{
protected array $data;
protected array $dateRange;
protected string $reportName;
public function __construct(array $data, array $dateRange, string $reportName = 'Статистика по отделениям')
@@ -42,7 +44,7 @@ class StatisticsExport implements FromCollection, WithHeadings, WithMapping, Wit
return [
// Шапка отчета (первые 3 строки)
[$this->reportName],
['Дата создания: ' . now()->format('d.m.Y H:i:s')],
['Дата создания: '.now()->format('d.m.Y H:i:s')],
[$this->formatDateRange()],
[], // Пустая строка для отступа
@@ -61,7 +63,7 @@ class StatisticsExport implements FromCollection, WithHeadings, WithMapping, Wit
'Операции', // Будет объединено с 2 колонками
'', // Пустые для заполнения
'Умерло',
'Мед. персонал'
'Мед. персонал',
],
// Второй уровень заголовков (детализация)
@@ -81,8 +83,8 @@ class StatisticsExport implements FromCollection, WithHeadings, WithMapping, Wit
'Э',
'П',
'',
''
]
'',
],
];
}
@@ -94,8 +96,10 @@ class StatisticsExport implements FromCollection, WithHeadings, WithMapping, Wit
if (isset($this->dateRange[0]) && isset($this->dateRange[1])) {
$startAt = Carbon::create($this->dateRange[0])->format('d.m.Y H:i');
$endAt = Carbon::create($this->dateRange[1])->format('d.m.Y H:i');
return 'Период: ' . $startAt . ' - ' . $endAt;
return 'Период: '.$startAt.' - '.$endAt;
}
return 'Период: За весь период';
}
@@ -108,7 +112,7 @@ class StatisticsExport implements FromCollection, WithHeadings, WithMapping, Wit
if (isset($row['isGroupHeader']) && $row['isGroupHeader']) {
return [
$row['groupName'], // Название группы
'', '', '', '', '', '', '', '', '', '', '', '', ''
'', '', '', '', '', '', '', '', '', '', '', '', '',
];
}
@@ -130,7 +134,7 @@ class StatisticsExport implements FromCollection, WithHeadings, WithMapping, Wit
$this->formatZero($row['surgical']['emergency'] ?? 0),
$this->formatZero($row['surgical']['plan'] ?? 0),
$this->formatZero($row['deceased'] ?? 0),
$this->formatZero($row['countStaff'] ?? 0)
$this->formatZero($row['countStaff'] ?? 0),
];
}
@@ -151,7 +155,7 @@ class StatisticsExport implements FromCollection, WithHeadings, WithMapping, Wit
$this->formatZero($row['surgical']['emergency'] ?? 0),
$this->formatZero($row['surgical']['plan'] ?? 0),
$this->formatZero($row['deceased'] ?? 0),
$this->formatZero($row['countStaff'] ?? 0)
$this->formatZero($row['countStaff'] ?? 0),
];
}
@@ -173,15 +177,15 @@ class StatisticsExport implements FromCollection, WithHeadings, WithMapping, Wit
$sheet->getPageSetup()->setFitToWidth(1); // 1 страница в ширину
$sheet->getPageSetup()->setFitToHeight(1); // 1 страница в высоту (опционально)
// $sheet->getPageSetup()->setScale(90);
// $sheet->getPageSetup()->setScale(90);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
// ОБЪЕДИНЕНИЕ ЯЧЕЕК ДЛЯ ШАПКИ (строки 1-3)
$sheet->mergeCells('A1:' . $highestColumn . '1'); // Наименование
$sheet->mergeCells('A2:' . $highestColumn . '2'); // Дата создания
$sheet->mergeCells('A3:' . $highestColumn . '3'); // Временной интервал
$sheet->mergeCells('A1:'.$highestColumn.'1'); // Наименование
$sheet->mergeCells('A2:'.$highestColumn.'2'); // Дата создания
$sheet->mergeCells('A3:'.$highestColumn.'3'); // Временной интервал
// ОБЪЕДИНЕНИЕ ДЛЯ ОБЫЧНЫХ ЗАГОЛОВКОВ
$sheet->mergeCells('A5:A6');
@@ -217,7 +221,7 @@ class StatisticsExport implements FromCollection, WithHeadings, WithMapping, Wit
]);
// СТИЛИ ДЛЯ ЗАГОЛОВКОВ (строки 5 и 6)
$sheet->getStyle('A5:' . $highestColumn . '6')->applyFromArray([
$sheet->getStyle('A5:'.$highestColumn.'6')->applyFromArray([
'font' => [
'bold' => true,
'color' => ['argb' => '000000'],
@@ -250,13 +254,12 @@ class StatisticsExport implements FromCollection, WithHeadings, WithMapping, Wit
// СТИЛИ ДЛЯ ВСЕХ СТРОК С ДАННЫМИ
$indexData = 0;
for ($row = 7; $row <= $highestRow; $row++) {
$cellValue = $sheet->getCell('A' . $row)->getValue();
$cellValue = $sheet->getCell('A'.$row)->getValue();
$currentRowInData = $this->data[$indexData];
// Заголовки групп (Хирургические отделения, Терапевтические отделения)
if (array_key_exists('isGroupHeader', $currentRowInData))
{
$sheet->getStyle('A' . $row . ':' . $highestColumn . $row)->applyFromArray([
if (array_key_exists('isGroupHeader', $currentRowInData)) {
$sheet->getStyle('A'.$row.':'.$highestColumn.$row)->applyFromArray([
'font' => [
'bold' => true,
'size' => 11,
@@ -268,12 +271,12 @@ class StatisticsExport implements FromCollection, WithHeadings, WithMapping, Wit
]);
// Объединяем ячейки для группы
$sheet->mergeCells('A' . $row . ':' . $highestColumn . $row);
$sheet->mergeCells('A'.$row.':'.$highestColumn.$row);
}
// Итоговые строки (ИТОГО:)
if (array_key_exists('isTotalRow', $currentRowInData)) {
$sheet->getStyle('A' . $row . ':' . $highestColumn . $row)->applyFromArray([
$sheet->getStyle('A'.$row.':'.$highestColumn.$row)->applyFromArray([
'font' => [
'bold' => true,
],
@@ -290,12 +293,13 @@ class StatisticsExport implements FromCollection, WithHeadings, WithMapping, Wit
]);
}
if ($indexData < count($this->data))
if ($indexData < count($this->data)) {
$indexData++;
}
}
// ГРАНИЦЫ ДЛЯ ВСЕЙ ТАБЛИЦЫ
$sheet->getStyle('A5:' . $highestColumn . $highestRow)->applyFromArray([
$sheet->getStyle('A5:'.$highestColumn.$highestRow)->applyFromArray([
'borders' => [
'allBorders' => [
'borderStyle' => Border::BORDER_THIN,
@@ -305,14 +309,14 @@ class StatisticsExport implements FromCollection, WithHeadings, WithMapping, Wit
]);
// ВЫРАВНИВАНИЕ ДЛЯ ЧИСЛОВЫХ КОЛОНОК
$sheet->getStyle('B7:' . $highestColumn . $highestRow)->applyFromArray([
$sheet->getStyle('B7:'.$highestColumn.$highestRow)->applyFromArray([
'alignment' => [
'horizontal' => Alignment::HORIZONTAL_CENTER,
],
]);
// ВЫРАВНИВАНИЕ ДЛЯ НАЗВАНИЙ ОТДЕЛЕНИЙ
$sheet->getStyle('A7:A' . $highestRow)->applyFromArray([
$sheet->getStyle('A7:A'.$highestRow)->applyFromArray([
'alignment' => [
'horizontal' => Alignment::HORIZONTAL_LEFT,
'vertical' => Alignment::VERTICAL_CENTER,
@@ -342,7 +346,7 @@ class StatisticsExport implements FromCollection, WithHeadings, WithMapping, Wit
$sheet->getRowDimension(6)->setRowHeight(25);
// Добавляем примечание внизу
// $this->addFootnote($sheet, $highestRow, $highestColumn);
// $this->addFootnote($sheet, $highestRow, $highestColumn);
return [];
}
@@ -378,16 +382,16 @@ class StatisticsExport implements FromCollection, WithHeadings, WithMapping, Wit
{
$footnoteRow = $startRow + 2; // Отступаем 2 строки от таблицы
$sheet->setCellValue('A' . $footnoteRow, 'ПРИМЕЧАНИЕ:');
$sheet->setCellValue('A'.$footnoteRow, 'ПРИМЕЧАНИЕ:');
$sheet->setCellValue('B' . $footnoteRow, '• % загруженности актуален на дату формирования отчёта (' . now()->format('d.m.Y') . ')');
$sheet->mergeCells('B' . $footnoteRow . ':' . $highestColumn . $footnoteRow);
$sheet->setCellValue('B'.$footnoteRow, '• % загруженности актуален на дату формирования отчёта ('.now()->format('d.m.Y').')');
$sheet->mergeCells('B'.$footnoteRow.':'.$highestColumn.$footnoteRow);
$sheet->setCellValue('B' . ($footnoteRow + 1), '• Поступление, выбытие, операции — за указанный в шапке период');
$sheet->mergeCells('B' . ($footnoteRow + 1) . ':' . $highestColumn . ($footnoteRow + 1));
$sheet->setCellValue('B'.($footnoteRow + 1), '• Поступление, выбытие, операции — за указанный в шапке период');
$sheet->mergeCells('B'.($footnoteRow + 1).':'.$highestColumn.($footnoteRow + 1));
// Стили для примечания
$sheet->getStyle('A' . $footnoteRow . ':' . $highestColumn . ($footnoteRow + 1))->applyFromArray([
$sheet->getStyle('A'.$footnoteRow.':'.$highestColumn.($footnoteRow + 1))->applyFromArray([
'font' => [
'italic' => true,
'size' => 9,
@@ -399,7 +403,7 @@ class StatisticsExport implements FromCollection, WithHeadings, WithMapping, Wit
]);
// Жирный шрифт для слова "ПРИМЕЧАНИЕ"
$sheet->getStyle('A' . $footnoteRow)->applyFromArray([
$sheet->getStyle('A'.$footnoteRow)->applyFromArray([
'font' => [
'bold' => true,
'color' => ['argb' => '000000'],