1

I am using PHPExcel library as shown here. But, tt's not working as expected. When I searched, someone has suggested replacing PHPEXcel_IOFactory to IOFactory. It downloads the file, but it couldn't be opened. Any suggestions?

Here is my library code:

Excel.php

require_once APPPATH.'/third_party/PHPExcel.php';
class Excel extends PHPExcel
{
    public function __construct()
    {
        parent::__construct();
    }
}

Here is the controller code:

        #load our new PHPExcel library
        $this->load->library('excel');

        $data = $this->report_model->get_all_report_product_info();
        $filename = "Output";

        # Set the active Excel worksheet to sheet 0 
        $this->excel->setActiveSheetIndex(0);

        $row_count = 2;
        foreach ($data as $row) {
            $this->excel->getActiveSheet()->setCellValue('A' . $row_count, $row->product_name);
            $this->excel->getActiveSheet()->setCellValue('B' . $row_count, $row->product_sku);
            $this->excel->getActiveSheet()->setCellValue('C' . $row_count, $row->customer_name);
            $this->excel->getActiveSheet()->setCellValue('D' . $row_count, $row->site_url);
            $row_count++;
        }

        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $filename . '.xlsx"');
        header('Cache-Control: max-age=0');

        // Instantiate a Writer to create an OfficeOpenXML Excel .xlsx file
        $objWriter = IOFactory::createWriter($this->excel, 'Excel5');

        // Write the Excel file to filename
        $objWriter->save('php://output');
Mahm00d
  • 3,881
  • 8
  • 44
  • 83
Satish Saini
  • 2,880
  • 3
  • 24
  • 38

1 Answers1

0
$objWriter = IOFactory::createWriter($this->excel, 'Excel5');

"Excel5" is the old spreadsheet format (used for .XLS files). If you want to create a XLSX file, you need to use "Excel2007".

Adrien
  • 1,929
  • 1
  • 13
  • 23