/** * Renders the excel, this will will always offer a file download * @param boolean $return Not implemented * @return null */ public function render($return = true) { $workbook = new Workbook(); $formatFactory = new FormatFactory(); $rowNumber = 1; if ($this->title) { $titleFormat = $formatFactory->createTitleFormat(); $filename = $this->title . '.xls'; $worksheet = $workbook->addWorksheet(substr($this->title, 0, 31)); $worksheet->write($rowNumber, 0, $this->title, $titleFormat); $rowNumber += 2; } else { $filename = self::DEFAULT_TITLE . '.xls'; $worksheet = $workbook->addWorksheet(self::DEFAULT_TITLE); } $subTitleFormat = $formatFactory->createSubtitleFormat(); $subTitleFormat->setBackgroundColor('#E3E3E3'); foreach ($this->headers as $header) { $cells = $header->getCells(); $colNumber = 0; foreach ($cells as $cell) { $worksheet->write($rowNumber, $colNumber++, $cell->getValue(), $subTitleFormat); } $rowNumber++; } $defaultFormat = $formatFactory->createDefaultFormat(); $evenFormat = clone $defaultFormat; $evenFormat->setBackgroundColor('#F3F3F3'); $groupFormat = $formatFactory->createBoldFormat(); $groupFormat->setBackgroundColor('#EBEBEB'); $zebraIndex = 0; foreach ($this->rows as $index => $row) { $format = $defaultFormat; if (in_array($index, $this->groupRows)) { $format = $groupFormat; } else { if ($zebraIndex) { $format = $evenFormat; $zebraIndex = 0; } else { $zebraIndex = 1; } } $cells = $row->getCells(); $colNumber = 0; foreach ($cells as $cell) { try { $worksheet->write($rowNumber, $colNumber++, strip_tags($cell->getValue()), $format); } catch (ZiboException $e) { $worksheet->write($rowNumber, $colNumber, '###', $format); } } $rowNumber++; } $worksheet->calculateColumnWidths(); $this->processWorksheet($worksheet); $workbook->send($filename); }
public function testWriteWorkbook() { $file = new File('/tmp/test.xls'); $workbook = new Workbook(); $worksheet = $workbook->addWorksheet('My first worksheet'); $worksheet->write(0, 0, 'Name'); $worksheet->write(0, 1, 'Age'); $worksheet->write(1, 0, 'John Smith'); $worksheet->write(1, 1, 30); $worksheet->write(2, 0, 'Johann Schmidt'); $worksheet->write(2, 1, 31); $worksheet->write(3, 0, 'Juan Herrera'); $worksheet->write(3, 1, 32); $workbook->write($file); $exists = $file->exists(); $file->delete(); $this->assertTrue($exists, 'file was not saved'); }