2016-03-03 126 views
0

開發者
我正在使用Yii1,我想使用PHPExcel擴展生成報告,但我的文件尚未準備好下載,而是出現在控制檯中。我也設置了標題,但仍然沒有準備好下載文件。我找不到確切的答案。請解決我的問題php excel下載文件

public function GenerateReport($dataProvider) 
{ 

    $phpExcelPath = Yii::getPathOfAlias('ext.phpexcel.Classes'); 
    include($phpExcelPath . DIRECTORY_SEPARATOR . 'PHPExcel.php'); 

    $objPHPExcel = new PHPExcel(); 
    $fileName = 'report-'.uniqid().'.xlsx'; 
    $objPHPExcel->getProperties()->setCreator("Palash Gupta"); 
    $objPHPExcel->getProperties()->setTitle($fileName); 
    $objPHPExcel->getProperties()->setSubject("Placement Student List"); 
    $objPHPExcel->getProperties()->setDescription("Placement Student List"); 
    $objPHPExcel->setActiveSheetIndex(0); 

    $rowCount = 1; 
    $objPHPExcel->getActiveSheet()->SetCellValue('A' . $rowCount, 'Name'); 
    $objPHPExcel->getActiveSheet()->SetCellValue('B' . $rowCount, 'Course'); 

    $rowCount = 2; 
    foreach ($dataProvider->getData() as $data) 
    { 
     $objPHPExcel->getActiveSheet()->SetCellValue('A' . $rowCount, $data['sd_fname']); 
     $objPHPExcel->getActiveSheet()->SetCellValue('B' . $rowCount, $data['fk_mc_id']); 
     $rowCount++; 
    } 

    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); 
    header('Content-Disposition: attachment;filename="'.$fileName.'"'); 

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); 
    $objWriter->save('php://output'); 

enter image description here}

+0

的截圖以字符'PK'開始。這意味着我們正在尋找壓縮(壓縮)的數據。 – user5329483

+0

它看起來也像.xlsx文件。 – user5329483

回答

0

試試這個:

// Redirect output to a client’s web browser (Excel2007) 
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); 
header('Content-Disposition: attachment;filename="'. $fileName .'"'); 
header('Cache-Control: max-age=0'); 
// If you're serving to IE 9, then the following may be needed 
header('Cache-Control: max-age=1'); 
// If you're serving to IE over SSL, then the following may be needed 
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past 
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified 
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1 
header ('Pragma: public'); // HTTP/1.0 
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); 
$objWriter->save('php://output'); 
exit; 

編號:https://github.com/PHPOffice/PHPExcel/blob/1.8/Examples/01simple-download-xls.php

+0

先生!仍然無法下載文件 –