2016-09-15 47 views
0

我試圖在yii2中創建報告以優化。第一,我使excel文件適應數據,當我點擊view/index.php中的按鈕導出。我把文件app/views/donatur。 第二,我在控制器中添加了actionExportExcel,目標是使用庫PHPExcel生成數據以格式化excel。 有什麼不對,我不知道爲什麼,錯誤如下。在yii2中報告excel出錯

無法打開$ app/views/donatur/excel.xlsx進行閱讀!文件不存在。

在控制器代碼

public function actionExportExcel() 
 
    { 
 
     $searchModel = new DonaturSearch(); 
 
     $dataProvider = $searchModel->search(Yii::$app->request->queryParams); 
 
     $objReader = \PHPExcel_IOFactory::createReader('Excel2007'); 
 
     
 
     // set tempate 
 
     $template = Yii::getAlias('$app/views/donatur').'/excel.xlsx'; 
 
     $objPHPExcel = $objReader->load($template); 
 
     $activeSheet = $objPHPExcel->getActiveSheet(); 
 

 
     // Set orientasi & ukuran kertas 
 
     $activeSheet->getPageSetup()->setOrientation(\PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE)-> 
 
     setPaperSize(\PHPExcel_Worksheet_PageSetup::PAPERSIZE_FOLIO); 
 
     $baseRow = 4; 
 
     foreach ($dataProvider->getModels() as $donatur) { 
 
      $activeSheet->setCellValue('A'.$baseRow, $baseRow-2)-> 
 
      setCellValue('B'.$baseRow, $category->nama_donatur)-> 
 
      setCellValue('C'.$baseRow, $category->alamat)-> 
 
      setCellValue('D'.$baseRow, $category->telepon); 
 
      $baseRow++; 
 
     } 
 
     header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); 
 
     header('Content-Disposition: attachment; filename= "excel.xlsx"'); 
 
     header('Cache-Control: max-age=0'); 
 
     $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel2007"); 
 
     $objWriter->save('php://output'); 
 
     exit; 
 
    }

+1

它不應該是'@應用程序/視圖/ donatur'而不是'$ app/views/donatur'? – Bizley

+0

我試過了@app/views/donatur。它適合我 – CrashBurn

回答

0

別名,你應該使用@而不是$

$template = Yii::getAlias('@app/views/donatur').'/excel.xlsx';