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?
However, this error message is not limited to the Excel files that a user gets via emails. Instead, it is often encountered with commonly used files also. You may find that an Excel workbook, on which they were working for a long time, suddenly starts throwing this error message when you try to open it.
To find out the unique number associated with this error message, press 'CTRL+SHIFT+I' together. After that, you get the following number besides the error message:
This error message is encountered when:
Causes:
Resolutions: You can try the below mentioned resolutions to rectify this error message and regain access to your Excel sheet.
Change the File Extension of Your Excel Sheet:
Sometimes, the problem is seen due to the unsupported file extension of your Excel file. To check the support of xlsx extension, go to 'My Computer'. Browse 'Tools >> Folder Options >> File Types'. Check if 'xlsx' extension is included there or not. If not, then rename this file and change '.xlsx' to '.xls'.
Use 'Open and Repair' Option of MS Excel:
For this, open Excel, and then go to 'File >> Open'. Select the file that you want to open, but do not double-click it. Go to the 'Open' drop-down provided in the 'Open' dialog box, and then select 'Open and Repair' option from there. This process repairs the corrupt Excel sheet and opens it normally.
Excel Recovery Software:
If none of the above mentioned resolutions is able to do any needful to you, then use of Excel repair software is recommended. Stellar Phoenix Excel Repair is one such software. It repairs corrupt excel sheet and restores entire data stored in it without altering its original formatting.