/** * @param DriverInterface $driver * @param $type * @param $extension * @param Workbook $workbook * * @throws DriverNotFoundException * @throws ExportFailedException * @return Writer */ public static function create(DriverInterface $driver, $type, $extension, Workbook $workbook) { // Protected export if ($workbook->getSheetCount() < 1) { throw new ExportFailedException('No sheets are added to your Excel file. Make sure you do so, before attempting to export'); } $class = self::getClassByDriverAndType($driver, $type); if (class_exists($class)) { return new $class($type, $extension, $workbook); } // Default writer $class = self::getClassByDriver($driver); if (class_exists($class)) { return new $class($type, $extension, $workbook); } throw new DriverNotFoundException("Writer driver [{$driver->getName()}] was not found"); }
/** * @param WorkbookInterface $workbook * * @return \PHPExcel */ protected function convertToDriver(WorkbookInterface $workbook) { $styleWriter = new StyleWriter(); /* * FIND DRIVER */ $driver = $workbook->getDriver(); /* * WORKBOOK STYLES */ if ($workbook->hasStyles()) { $styles = $styleWriter->convert($workbook->getStyles()); $driver->getDefaultStyle()->applyFromArray($styles); } /* * WORKBOOK SHEETS */ foreach ($workbook->getSheets() as $sheet) { $driverWorksheet = $sheet->getDriver(); /* * WORKSHEET STYLES */ if ($sheet->hasStyles()) { $styles = $styleWriter->convert($sheet->getStyles()); $driverWorksheet->getDefaultStyle()->applyFromArray($styles); } /* * CELLS */ $cellWriter = new CellWriter($driverWorksheet); // Add sheet to workbook $driver->addSheet($driverWorksheet); // Add cells foreach ($sheet->getCells() as $cell) { $cellWriter->write($cell); } // Merge given cells foreach ($sheet->getMergeCells() as $range) { $driverWorksheet->mergeCells($range); } } // Rewind $driver->setActiveSheetIndex(0); return $driver; }
/** * Convert cells to array * @param Workbook $workbook * * @return array */ protected function convertCellsToArray(Workbook $workbook) { $source = []; $r = 0; $row = 1; foreach ($workbook->getSheets() as $sheet) { foreach ($sheet->getCells() as $cell) { // Go to next row when needed if ($cell->getCoordinate()->getRow() != $row) { $r++; } // Set value $source[$r][] = $cell->getValue(); // Save last row $row = $cell->getCoordinate()->getRow(); } } return $source; }
/** * @param WorkbookInterface $workbook * @param $title * @param Closure $callback * @param LeagueWriter $driver */ public function __construct(WorkbookInterface $workbook, $title = null, Closure $callback = null, LeagueWriter $driver = null) { // Set PHPExcel worksheet $this->driver = $driver ?: $workbook->getDriver(); parent::__construct($title, $callback); }
/** * @param WorkbookInterface $workbook * @param $title * @param Closure $callback * @param PHPExcel_Worksheet $driver */ public function __construct(WorkbookInterface $workbook, $title = 'New sheet', Closure $callback = null, PHPExcel_Worksheet $driver = null) { // Set PHPExcel worksheet $this->driver = $driver ?: new PHPExcel_Worksheet($workbook->getDriver()); parent::__construct($title, $callback); }