* @license PHP License * @package WB * @subpackage base */ WBClass::load('WBFile'); /** * File operations for Spreadsheet files * * @version 1.0.1 * @package WB * @subpackage base */ class WBFile_Spreadsheet extends WBFile { /** * column map * @var array */ private $map = array(); /** * PhpSpreadsheet * * @var PhpOffice\PhpSpreadsheet\Spreadsheet */ private $sheet; /** * Current Sheet * @var int */ private $sheetIndex = 0; /** * Current Row * @var int */ private $row = array(0 => 0); /** * SpreadSheet File Type * @var string */ private $spreadsheetType = 'ods'; /** * Named Columns in Excel-Style * @var array */ private $cols = array(); /** * Set File Type of Spreadsheet * * @param string */ public function setSpreadsheetType($type = 'ods') { $this->spreadsheetType = strtolower($type); } /** * Open CSV file * * Wrapper to fopen() * * @see fopen() * @param string $mode */ public function open($mode = 'r') { parent::open($mode); $this->sheet = WBClass::create('PhpOffice\PhpSpreadsheet\Spreadsheet'); $this->row = array(0 => 0); return $this; } /** * Close * * Close open file handle */ public function close() { $writer = PhpOffice\PhpSpreadsheet\IOFactory::createWriter($this->sheet, ucfirst($this->spreadsheetType)); $writer->save($this->fh); parent::close(); return $this; } /** * Get Properties * @return PhpOffice\PhpSpreadsheet\Document\Properties */ public function getProperties() { return $this->sheet->getProperties(); } /** * Set Active Sheet * * @param int * @return PhpOffice\PhpSpreadsheet\Worksheet */ public function setActiveSheetIndex($index) { $this->sheetIndex = $index; return $this->sheet->setActiveSheetIndex($index); } public function getActiveSheet() { return $this->sheet->getActiveSheet(); } public function getColumnNames() { return $this->cols; } /** * Add an new Spreadsheet * * Add Sheet at the end */ public function addSheet($title = '') { $cnt = $this->sheet->getSheetCount(); $this->row[$cnt] = 0; if (empty($title)) { $title = 'sheet ' . $cnt; } $ws = new \PhpOffice\PhpSpreadsheet\Worksheet($this->sheet, $title); $this->sheet->addSheet($ws); } /** * Write row * * Put row to spreadsheet * * * @todo extend column limit * @param array $data */ public function write($data) { ++$this->row[$this->sheetIndex]; if (empty($this->map)) { $row = array_values($data); if (empty($this->cols)) { $this->initColumnNames(count($row)); } } else { $row = array(); foreach ($this->map as $k => $i) { $row[$i] = ''; if (isset($data[$k])) { $row[$i] = $data[$k]; } } } $sheet = $this->sheet->setActiveSheetIndex($this->sheetIndex); foreach ($row as $i => $v) { if (!isset($this->cols[$i])) { $ex = array( 'class' => __CLASS__, 'code' => 1, 'msg' => 'Maximum number of columns exceeded!' ); throw WBClass::create('WBException_Argument', $ex); } $sheet->setCellValue($this->cols[$i] . $this->row[$this->sheetIndex], $v); } } /** * Set map to support associative arrays * * Optional map allow to support associative arrays * * @param array $map */ public function setMap($map = array()) { $this->map = $map; $this->initColumnNames(count($this->map)); } private function initColumnNames($count) { $alpha = range('A', 'Z'); $this->cols = $alpha; if (count($this->cols) > $count) { return; } foreach ($alpha as $a1) { foreach ($alpha as $a2) { $this->cols[] = $a1 . $a2; if (count($this->cols) > $count) { return; } } } } }