Not sure what’s wrong. I’m using PHPExcel, and the file itself works flawlessly.
I serve both .CSV and .XLSX. .CSV works in both localhost and server, but .XLSX doesn’t do anything. It just opens the link as an empty page, and does nothing. Also, curiously enough, when I download the .XLSX in localhost it automatically opens the XLSX file, while the CSV just downloads like normal.
By the way, I’m using nGinx. I thought it could be a mime problem, but it doesn’t seem to be the case.
This is my code:
class Reporter {
//Mime types for each kind of possible report file type.
private $mimeType=array('csv'=>'text/csv', 'xlsx'=>'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
//Variables to make documents.
private $excelObject=null;
private $documentProperties=null;
public function createReport($ext, $name, $data){
//Load the PHPExcel library if the file is not an CSV
if($ext!='csv'){
//Unregister Yii's autoload to prevent conflict with the library's autoloader.
spl_autoload_unregister(array('YiiBase','autoload'));
Yii::import('application.vendors.classes.*');
require_once('PHPExcel.php');
//After the library is imported, register Yii's autoloader again.
spl_autoload_register(array('YiiBase','autoload'));
//Initialize variables for PHPExcel
$this->excelObject=new PHPExcel();
$this->documentProperties=new PHPExcel_DocumentProperties();
$this->documentProperties->setCompany("MyName");
$this->documentProperties->setCreator("MyName");
$this->documentProperties->setTitle("Title");
$this->excelObject->setProperties($this->documentProperties);
}
//Generate the file name. It uses the user_id, a timestamp, a provided name and the file extension.
$filename=Yii::app()->user->id.'-'.time().'-'.$name.'.'.$ext;
$filepath=Yii::app()->basePath.'/'.$filename;
//Prepare the headers
header('Content-Description: File Transfer');
header('Content-Type: '.$this->mimeType[$ext]); //Content-Type mime Type defined by the extension.
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
//Generate a report depending on the solicited file ex: csv, xls, xlsx, pdf, etc.
switch($ext){
case 'csv':
//CSVs don't need the PHPExcel library. Just make a file and generate CSV lines.
$this->createCSV($filepath, $data);
break;
case 'xls':
break;
case 'xlsx':
$this->createXLSX($filepath, $data);
break;
}
//Header added after the file was created to be able to check its size
header('Content-Length: ' . filesize($filepath));
//Clean the buffer, read the file so that the user sees/downloads it, then remove the file.
ob_clean();
flush();
readfile($filepath);
unlink($filepath);
}
public function createXLSX($filepath, $data){
$sheet=$this->excelObject->getActiveSheet();
$headers=array();
foreach($data as $key=>$row){
if($key==0){
//Add headers to the spreadsheet.
$i=0;
foreach($row as $head=>$element){
$sheet->setCellValueByColumnAndRow($i, $key+1, $head);
$headers[$i]=$head;
$i++;
}
}
foreach($headers as $j=>$header)
$sheet->setCellValueByColumnAndRow($j, $key+2, $row[$header]);
}
$objWriter = new PHPExcel_Writer_Excel2007($this->excelObject);
$objWriter->save($filepath);
}
public function createCSV($filepath, $data){
$file=fopen($filepath, 'w');
foreach($data as $key=>$row){
if($key==0){
$headers=array_keys($row);
fputcsv($file, $headers);
}
fputcsv($file, $row);
}
fclose($file);
}
}
I call createReport, and CSV is all right, but XLSX is not downloading anything.