spreadsheet = $spreadsheet; $this->defaultFont = $this->spreadsheet->getDefaultStyle()->getFont(); } /** * Save Spreadsheet to file. * * @param resource|string $filename */ public function save($filename, int $flags = 0): void { $this->processFlags($flags); // Open file $this->openFileHandle($filename); // Write html fwrite($this->fileHandle, $this->generateHTMLAll()); // Close file $this->maybeCloseFileHandle(); } /** * Save Spreadsheet as html to variable. * * @return string */ public function generateHtmlAll() { // garbage collect $this->spreadsheet->garbageCollect(); $saveDebugLog = Calculation::getInstance($this->spreadsheet)->getDebugLog()->getWriteDebugLog(); Calculation::getInstance($this->spreadsheet)->getDebugLog()->setWriteDebugLog(false); $saveArrayReturnType = Calculation::getArrayReturnType(); Calculation::setArrayReturnType(Calculation::RETURN_ARRAY_AS_VALUE); // Build CSS $this->buildCSS(!$this->useInlineCss); $html = ''; // Write headers $html .= $this->generateHTMLHeader(!$this->useInlineCss); // Write navigation (tabs) if ((!$this->isPdf) && ($this->generateSheetNavigationBlock)) { $html .= $this->generateNavigation(); } // Write data $html .= $this->generateSheetData(); // Write footer $html .= $this->generateHTMLFooter(); $callback = $this->editHtmlCallback; if ($callback) { $html = $callback($html); } Calculation::setArrayReturnType($saveArrayReturnType); Calculation::getInstance($this->spreadsheet)->getDebugLog()->setWriteDebugLog($saveDebugLog); return $html; } /** * Set a callback to edit the entire HTML. * * The callback must accept the HTML as string as first parameter, * and it must return the edited HTML as string. */ public function setEditHtmlCallback(?callable $callback): void { $this->editHtmlCallback = $callback; } /** * Map VAlign. * * @param string $vAlign Vertical alignment * * @return string */ private function mapVAlign($vAlign) { return Alignment::VERTICAL_ALIGNMENT_FOR_HTML[$vAlign] ?? ''; } /** * Map HAlign. * * @param string $hAlign Horizontal alignment * * @return string */ private function mapHAlign($hAlign) { return Alignment::HORIZONTAL_ALIGNMENT_FOR_HTML[$hAlign] ?? ''; } const BORDER_ARR = [ Border::BORDER_NONE => 'none', Border::BORDER_DASHDOT => '1px dashed', Border::BORDER_DASHDOTDOT => '1px dotted', Border::BORDER_DASHED => '1px dashed', Border::BORDER_DOTTED => '1px dotted', Border::BORDER_DOUBLE => '3px double', Border::BORDER_HAIR => '1px solid', Border::BORDER_MEDIUM => '2px solid', Border::BORDER_MEDIUMDASHDOT => '2px dashed', Border::BORDER_MEDIUMDASHDOTDOT => '2px dotted', Border::BORDER_SLANTDASHDOT => '2px dashed', Border::BORDER_THICK => '3px solid', ]; /** * Map border style. * * @param int|string $borderStyle Sheet index * * @return string */ private function mapBorderStyle($borderStyle) { return array_key_exists($borderStyle, self::BORDER_ARR) ? self::BORDER_ARR[$borderStyle] : '1px solid'; } /** * Get sheet index. */ public function getSheetIndex(): ?int { return $this->sheetIndex; } /** * Set sheet index. * * @param int $sheetIndex Sheet index * * @return $this */ public function setSheetIndex($sheetIndex) { $this->sheetIndex = $sheetIndex; return $this; } /** * Get sheet index. * * @return bool */ public function getGenerateSheetNavigationBlock() { return $this->generateSheetNavigationBlock; } /** * Set sheet index. * * @param bool $generateSheetNavigationBlock Flag indicating whether the sheet navigation block should be generated or not * * @return $this */ public function setGenerateSheetNavigationBlock($generateSheetNavigationBlock) { $this->generateSheetNavigationBlock = (bool) $generateSheetNavigationBlock; return $this; } /** * Write all sheets (resets sheetIndex to NULL). * * @return $this */ public function writeAllSheets() { $this->sheetIndex = null; return $this; } private static function generateMeta(?string $val, string $desc): string { return $val ? (' ' . PHP_EOL) : ''; } public const BODY_LINE = '
' . PHP_EOL; /** * Generate HTML header. * * @param bool $includeStyles Include styles? * * @return string */ public function generateHTMLHeader($includeStyles = false) { // Construct HTML $properties = $this->spreadsheet->getProperties(); $html = '' . PHP_EOL; $html .= '' . PHP_EOL; $html .= ' ' . PHP_EOL; $html .= ' ' . PHP_EOL; $html .= ' ' . PHP_EOL; $html .= '