我的模特(action.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.Controller(Index_cont.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>
我的观点(index.php)
Komentar.php
问题是我的代码只是查看$contents = $this->load->view("Komentar", $data);
(Komentar.php
)而不执行下载xls代码。如果我删除该行,它可以下载空白的xls文件。我想在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
下载我的数据,而不仅仅是空白页。
{{1}}
答案 0 :(得分:1)
您没有返回视图加载的数据,因此您无法echo
它,您需要将第3个参数设置为TRUE
$contents = $this->load->view("Komentar", $data, TRUE); // Assign it to $contents
还可以使用CodeIgniter提供的Output库。
您的代码应如下所示:
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));
}
答案 1 :(得分:0)
您使用创建Excel文件的方式不正确。
使用PHPExcel
或PhpSpreadsheet
库在打开文件时制作excel文件时没有错误。
<强> PHPExcel 强>
https://github.com/PHPOffice/PHPExcel
<强> PhpSpreadsheet 强>
https://github.com/PHPOffice/PhpSpreadsheet
PHPExcel教程
https://arjunphp.com/how-to-use-phpexcel-with-codeigniter/ http://www.webslesson.info/2017/04/generate-excel-file-in-codeigniter-using-phpexcel.html
PhpSpreadsheet教程