Система управления «Сайт PRO»
Версия 20240107

table.excel.php

Исходный код
<?php

$Table = $Page;

require_once($root . '/cms/modules/vendor/autoload.php');

\PhpOffice\PhpSpreadsheet\Settings::setLocale('ru');

$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$spreadsheet->getProperties()
    ->setCreator('Сайт PRO')
    ->setLastModifiedBy('Сергей Фролов')
    ->setTitle($Conf->get('email_title'))
    ->setSubject($Conf->get('email_title'))
    ->setDescription($Conf->get('email_title'))
    ->setKeywords($Conf->get('email_title'))
    ->setCategory($Conf->get('email_title'))
;
$spreadsheet->getDefaultStyle()->getFont()->setName('Arial')->setSize(10);
$spreadsheet->getDefaultStyle()->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER)->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_TOP)->setWrapText(true);


$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle(date('d.m.Y'));

$sheet->getColumnDimension('A')->setWidth(1);
$sheet->getRowDimension('1')->setRowHeight(5);

$y = 2; $sheet->getRowDimension($y)->setRowHeight(-1);
$x = 'A'; foreach ($Table->getCols() as $Col) {
    $title = $Col->title;
    $x ++; $sheet->getCell($x . $y)->setValue($title)->getStyle()->getFont()->setSize(12)->setBold(true);
    $sheet->getColumnDimension($x)->setWidth(20);
    $width = $Col->excel_width;
    if (strlen($width)) $sheet->getColumnDimension($x)->setWidth($width);
}

foreach ($Table->getRows() as $Row) {
    $y ++; $sheet->getRowDimension($y)->setRowHeight(-1);
    $x = 'A'; foreach ($Table->getCols() as $Col) {
        $value = $Col->displayValue($Row);
        if ($value === '&nbsp;') $value = '';
        $value = str_replace('<br>', "\n", $value);
        $value = str_replace('&nbsp;', ' ', $value);
        $value = html_entity_decode(strip_tags($value), ENT_HTML5, 'UTF-8');
        $x ++; $cell = $sheet->getCell($x . $y)->setValue($value);
        if (strlen($format = $Col->excel_format)) $sheet->getStyle($x . $y)->getNumberFormat()->setFormatCode($format);
    }
}

$sheet->setSelectedCell('A1');

while (ob_end_clean()) { }

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="' . date('Ymd-His') . '.xlsx"');

$Main->Request->sendCookie();

$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);

$writer->save('php://output');

exit;