示例#1
0
 /**
  * Create the Excel file and save it to the temp file
  */
 protected function createFile()
 {
     if (!$this->_created) {
         $workbook = $this->getWorkbook();
         $i = 0;
         foreach ($this->sheets as $title => $config) {
             if (is_string($config)) {
                 $config = ['class' => $config];
             } elseif (is_array($config)) {
                 if (!isset($config['class'])) {
                     $config['class'] = ExcelSheet::className();
                 }
             } elseif (!is_object($config)) {
                 throw new \Exception('Invalid sheet configuration');
             }
             $sheet = 0 === $i++ ? $workbook->getActiveSheet() : $workbook->createSheet();
             if (is_string($title)) {
                 $sheet->setTitle($title);
             }
             Yii::createObject($config, [$sheet])->render();
         }
         $class = $this->writer;
         $writer = new $class($workbook);
         $writer->save((string) $this->getTmpFile());
         $this->_created = true;
     }
 }
 /**
  * @inheritdoc
  */
 protected function renderRow($data, $row, $formats, $formatters, $callbacks, $types)
 {
     $values = array_map(function ($attr) use($data) {
         return ArrayHelper::getValue($data, $attr);
     }, $this->getAttributes());
     return parent::renderRow($values, $row, $formats, $formatters, $callbacks, $types);
 }