Evan
Evan

Reputation: 78

Error Downloading xls file form CI PHP

My model(action.php)

class action extends CI_Model{

public function __construct(){
    $this->load->database();
}

public function get_data(){
  $query =$this->db->query('SELECT * FROM data');
  return $query->result();
}

}

My.Controller(Index_cont.php)

public function report()
{
  $data = array(
    "user"=> $this->action->get_data());
             //var_dump($data); die();

  $filename ="comment.xls";
  $contents = $this->load->view("Komentar", $data);
  header('Content-type: application/ms-excel');
  header('Content-Disposition: attachment; filename='.$filename);
  echo $contents;
}

My View(index.php)

<form class="" action="<?php echo base_url('index_cont/report'); ?>" method="post">
    <button class="btn btn-primary" name="submit"  type="submit"> Download Report</button>
</form>

The problem is my code just view Komentar.php ($contents = $this->load->view("Komentar", $data);) and not execute download xls code. If I remove that line, it can download blank xls file. I want to download my data in Komentar.php not just blank page.

Response Header
Request URL: http://localhost:8000/tekinf-ci/index_cont/report
Request Method: POST
Status Code: 500 Internal Server Error
Remote Address: [::1]:8000
Referrer Policy: no-referrer-when-downgrade
Cache-Control: no-store, no-cache, must-revalidate
Connection: close
Content-Length: 1924
Content-Type: text/html; charset=UTF-8
Date: Tue, 10 Apr 2018 13:59:44 GMT
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Pragma: no-cache
Server: Apache/2.4.23 (Win32) OpenSSL/1.0.2h PHP/7.0.9
Set-Cookie: ci_session=ap0r8obbc47nqqcbt1ehrg0u5bffeg17; expires=Tue, 10-Apr-2018 15:59:45 GMT; Max-Age=7200; path=/; HttpOnly
X-Powered-By: PHP/7.0.9

Upvotes: 1

Views: 1606

Answers (2)

Anfath Hifans
Anfath Hifans

Reputation: 1598

The way you are using to create excel file is not the right way. Use PHPExcel or PhpSpreadsheet library to make excel file without errors in opening the file.

PHPExcel

https://github.com/PHPOffice/PHPExcel

PhpSpreadsheet

https://github.com/PHPOffice/PhpSpreadsheet

PHPExcel Tutorial

https://arjunphp.com/how-to-use-phpexcel-with-codeigniter/ http://www.webslesson.info/2017/04/generate-excel-file-in-codeigniter-using-phpexcel.html

PhpSpreadsheet Tutorial

https://phpspreadsheet.readthedocs.io/en/develop/

Upvotes: 0

Spoody
Spoody

Reputation: 2882

You are not returning the data loaded by the view so you can't echo it, you need to set the 3rd argument to TRUE

$contents = $this->load->view("Komentar", $data, TRUE); // Assign it to $contents

Also use the Output library provided by CodeIgniter.

Your code should look like this:

public function report()
{
    $data = array("user"=> $this->action->get_data());
    $filename = "comment.xls";
    // Set headers
    $this->output->set_content_type('application/ms-excel')
                 ->set_header('Content-Transfer-Encoding: binary')
                 ->set_header('Cache-Control: private, no-transform, no-store, must-revalidate')
                 ->set_header('Content-Disposition: attachment; filename="'.$filename.'"')
                 ->set_output($this->load->view("Komentar", $data, TRUE));
}

Upvotes: 1

Related Questions