From: Vincent Vanwaelscappel Date: Wed, 24 Aug 2022 11:32:02 +0000 (+0200) Subject: wip #5414 @0.25 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=b6f1ddf50862ffef4dfaf50836d5bfad02a66152;p=cubist_excel.git wip #5414 @0.25 --- diff --git a/src/Excel.php b/src/Excel.php new file mode 100644 index 0000000..80e784a --- /dev/null +++ b/src/Excel.php @@ -0,0 +1,84 @@ +getDefaultStyle() + ->getNumberFormat() + ->setFormatCode( + NumberFormat::FORMAT_TEXT + ); + $sheet = $excel->getActiveSheet(); + $sheet->setTitle($sheetname); + + $line = 1; + if (!is_null($head)) { + $c = 0; + foreach ($head as $label) { + $columns = max($columns, $c); + $cell = $sheet->getCellByColumnAndRow($c, $line); + $cell->setValue($label); + $style = $sheet->getStyleByColumnAndRow($c, $line); + $style->getFont()->setBold(true); + $style->getAlignment()->setVertical(Alignment::VERTICAL_CENTER); + + $c++; + } + $line++; + } + + foreach ($datas as $l) { + $c = 0; + foreach ($l as $v) { + $columns = max($columns, $c); + $sheet->getCellByColumnAndRow($c, $line)->setValue($v); + $style = $sheet->getStyleByColumnAndRow($c, $line); + $style->getAlignment()->setVertical(Alignment::VERTICAL_CENTER); + $style->getAlignment()->setWrapText(true); + $c++; + } + $line++; + } + + for ($i = 0; $i <= $columns; $i++) { + if (null === $width || !isset($width[$i]) || null === $width[$i] || $width[$i] == 'auto') { + $sheet->getColumnDimensionByColumn($i)->setAutoSize(true); + } else { + $sheet->getColumnDimensionByColumn($i)->setAutoSize(false)->setWidth($width[$i]); + } + } + + if (null !== $output) { + self::_save($excel, $output); + } + + return $excel; + } + + protected static function _save($excel, $output) + { + $writer = new Xlsx(); + $writer->setSpreadsheet($excel); + $writer->save($output); + } +} \ No newline at end of file