I am trying to export my database info to Excel with CakePHP 2.5.4. However, Excel cannot open the resulting file myfilename.xlsx
. The error given is: "The file format or file extension is not valid. Verify the the file has not been corrupted and that the file extension matches the format of the file".
I use the following code to search the data in my controller and export to Excel:
<?php
$this->PhpExcel->createWorksheet();
$this->PhpExcel->setDefaultFont('Calibri', 12);
// define table cells
$table = array(
array('label' => __('Name'), 'width' => 'auto', 'filter' => true),
array('label' => __('Email'), 'width' => 'auto'),
array('label' => __('Second Email'), 'width' => 'auto'),
array('label' => __('Third Email'), 'width' => 'auto'),
);
// heading
$this->PhpExcel->addTableHeader($table, array('name' => 'Cambria', 'bold' => true));
// data
foreach ($data as $d) {
//if($d['Applicant']['name'] != '(NO BORRAR!!!!!)'){
$this->PhpExcel->addTableRow(array(
$d['Applicant']['name'],
$d['Applicant']['mail'],
$d['Applicant']['mail2'],
$d['Applicant']['mail_mother']
));
//}
}
$this->PhpExcel->addTableFooter();
$this->PhpExcel->output();
exit;
?>
Can anyone tell a way to fix it?