Export Excel In CodeIgniter

    public function export(){
        //$this->check_permission('device', 'update', array('download'));

        $options = $this->request_params();
        $options['limit'] = 9999;
        $options['start'] = 0;
        //logg($options);

        require(APPPATH.'libraries/PHPExcel.php');

        $file_name = 'receipts.xls';
        $file_path = FCPATH . 'uploads_temp' . DIRECTORY_SEPARATOR . $file_name;
        $file_url = base_url() . 'uploads_temp/' . $file_name . '?t=' . time();
        
        // Initiate cache
        $cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;
        $cacheSettings = array( 'memoryCacheSize' => '128MB');
        PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
        $objPHPExcel = new PHPExcel();

        //Create $worksheet object to save data
        $worksheet = $objPHPExcel->setActiveSheetIndex(0);

        $rowN = 1;
        $columnN = 0;
        $fields = $this->config->item('download_fields', 'receipt');

        foreach($fields as $key => $item){
            $label = $item[0];
            //if($key == 'partner_id') $label = '經銷商名稱';

            $worksheet->setCellValueByColumnAndRow($columnN, $rowN, $label);
            $columnN++;
        }

        $data = $this->receipt_m->items($options);

        $rowN++;
        foreach($data as $row){
            $columnN = 0;
            $line = [];

            $products = $row->products;
            $products = json_decode($products);
            //logg($products);

            foreach ($products as $key => $p) {
                $value = val($row, 'created_at');
                $worksheet->setCellValueExplicitByColumnAndRow($columnN, $rowN, $value, PHPExcel_Cell_DataType::TYPE_STRING);
                $columnN++;

                $value = val($row, 'rid');
                $worksheet->setCellValueExplicitByColumnAndRow($columnN, $rowN, $value, PHPExcel_Cell_DataType::TYPE_STRING);
                $columnN++;

                $value = val($p, 'uid');
                $worksheet->setCellValueExplicitByColumnAndRow($columnN, $rowN, $value, PHPExcel_Cell_DataType::TYPE_STRING);
                $columnN++;

                $value = val($p, 'name');
                $worksheet->setCellValueExplicitByColumnAndRow($columnN, $rowN, $value, PHPExcel_Cell_DataType::TYPE_STRING);
                $columnN++;

                $value = val($p, 'price');
                $worksheet->setCellValueExplicitByColumnAndRow($columnN, $rowN, $value, PHPExcel_Cell_DataType::TYPE_STRING);
                $columnN++;
                
                $value = val($p, 'qty');
                $worksheet->setCellValueExplicitByColumnAndRow($columnN, $rowN, $value, PHPExcel_Cell_DataType::TYPE_STRING);
                $columnN++;

                $value = val($p, 'total');
                $worksheet->setCellValueExplicitByColumnAndRow($columnN, $rowN, $value, PHPExcel_Cell_DataType::TYPE_STRING);
                $columnN++;
            }

            $rowN++;
        }

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save($file_path);

        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="'.basename($file_path).'"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file_path));
        readfile($file_path);
        exit;
    }