예제 #1
0
 public function actionDownload()
 {
     $request = Yii::$app->getRequest()->get();
     if ($request['company'] == 'letual') {
         $command = Yii::$app->getDb()->createCommand('SELECT * FROM letual_product');
         $attr = new LetualProduct();
         $reader = $command->query();
     } elseif ($request['company'] == 'rive') {
         $command = Yii::$app->getDb()->createCommand('SELECT * FROM rivegauche_product');
         $attr = new RivegaucheProduct();
         $reader = $command->query();
     } elseif ($request['company'] == 'ile') {
         $command = Yii::$app->getDb()->createCommand('SELECT * FROM iledebeaute_product');
         $attr = new IledebeauteProduct();
         $reader = $command->query();
     } elseif ($request['company'] == 'eli') {
         $command = Yii::$app->getDb()->createCommand('SELECT * FROM elize_product');
         $attr = new ElizeProduct();
         $reader = $command->query();
     } else {
         $let = [];
     }
     $filename = sprintf('%s_%s.xls', $request['company'], date_format(new \DateTime(), 'Y-m-d'));
     $export = new ExportExcel($filename, count($attr->getAttributes()), $reader->count() + 1);
     $export->openWriter();
     $export->openWorkbook();
     $export->writeDocumentProperties();
     $export->writeStyles();
     $export->openWorksheet();
     //title row
     $export->resetRow();
     $export->openRow(true);
     foreach ($attr->getAttributes() as $code => $format) {
         $export->appendCellString($attr->getAttributeLabel($code));
     }
     $export->closeRow();
     $export->flushRow();
     while ($row = $reader->read()) {
         $export->resetRow();
         $export->openRow();
         foreach ($attr->getAttributes() as $code => $format) {
             $export->appendCellString($row[$code]);
         }
         $export->closeRow();
         $export->flushRow();
     }
     //close all
     $export->closeWorksheet();
     $export->closeWorkbook();
     $export->closeWriter();
     header('Content-Description: File Transfer');
     header('Content-Type: application/octet-stream');
     header('Content-Disposition: attachment; filename=' . basename($filename));
     header('Content-Transfer-Encoding: binary');
     header('Expires: 0');
     header('Cache-Control: must-revalidate');
     header('Pragma: public');
     header('Content-Length: ' . filesize($export->getBaseFullFileName()));
     readfile($export->getBaseFullFileName());
 }