I am using phpspreadsheet to generate excel files from my database in CodeIgniter. While I try to open the exported excel file it shows me an error:
Excel cannot open the file 'abc.xlsx' because the file format or extension is not valid. Verify that file has not been corrupted and that the file extension matches the format of the file
Here is my code:
model
public function getAllMembersSearchByFamily($term)
{
$this->db->select('*,tab_family.family_name');
$this->db->from('tab_members');
$this->db->join('tab_family', 'tab_family.family_id = tab_members.family_id');
$this->db->where('tab_members.family_id', $term);
$query = $this->db->get();
return $query->result();
}
Controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require "vendor/autoload.php";
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
class Applications extends CI_Controller {
public function generatefamilyreport()
{
$output="";
$term = trim($this->input->post('searchterm'));
echo $term;
$result = $this->Family_model->getAllMembersSearchByFamily($term);
if(!empty($result))
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$i = 1;
foreach($result as $res)
{
$sheet->setCellValue('A'.$i, $res->name);
$i++;
}
$writer = new Xlsx($spreadsheet);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="users.xlsx"');
header('Cache-Control: max-age=0');
header('Expires: Fri, 11 Nov 2011 11:11:11 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: cache, must-revalidate');
header('Pragma: public');
$writer->save('php://output');
}
}
}
view
<?php
$attributes = array('class' => 'search-form', 'id' => 'generatereport');
echo form_open('Applications/generatefamilyreport',$attributes);
?>
<input type="text" name="searchterm" id="generatereporttext" <?php if(isset($term) && !empty($term)) { ?> value="<?php echo $term;?>" <?php } ?>>
<button class="btn btn-danger" style="margin-left: 15px;">Generate Report</button>
<?php echo form_close(); ?>
I am using phpspreadsheet for the first time. Can anyone help me to fix this?? Thanks in advance!
Edit 1
This same code works when I try with inline data, that is not using the data from database. The excel file gets exported and I am able to open it. here is the edited controller code for inline data:
public function generatefamilyreport()
{
$spreadsheet = new Spreadsheet();
$spreadsheet->getActiveSheet()->setTitle('Simple');
$spreadsheet->setActiveSheetIndex(0)
->setCellValue('A1', 'This')
->setCellValue('B2', 'is')
->setCellValue('C1', 'a')
->setCellValue('D2', 'test.');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="simple.xlsx"');
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
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
}