【发布时间】:2013-09-26 11:26:19
【问题描述】:
我在一个页面中查看了我的报告。我想在单击导出按钮时将报告作为 excel 文件查看/下载的功能。我试过这个:Reports in Codeigniter 但我没有运气。有人可以帮我吗?谢谢!
我的控制器:
public function export_reports(){
$this->load->dbutil();
$this->load->helper('file');
$filter = $this->input->post('filter');
$year_date = $this->input->post('year_val');
$today = $this->input->post('today');
$month_start = $this->input->post('month_start');
$month_end = $this->input->post('month_end');
$last_month_start = $this->input->post('last_month_start');
$last_month_end = $this->input->post('last_month_end');
$this_year_start = $this->input->post('this_year_start');
if($filter == "this_month"){
$report = $this->getdata_model->get_reports($filter, $year_date, $today, $month_start, $month_end, $last_month_start, $last_month_end, $this_year_start);
$new_report = $this->dbutil->xml_from_result($report);
write_file('billing_report_this_month.xml',$new_report);
}else{
$report = $this->getdata_model->default_report($month_start, $month_end);
$new_report = $this->dbutil->xml_from_result($report);
write_file('monthly_billing_report.xml',$new_report);
}
}
我的模特:
public function default_report($month_start, $month_end){
return $this->db->select("b.*, CONCAT(c.first_name,' ',c.last_name) as fullname", FALSE)
->order_by('fullname')
->from('billing as b')
->where('billing_date >=', $month_start)
->where('billing_date <=', $month_end)
->join('clients as c', 'b.customer_uuid=c.uuid')
->get()
->result();
}
public function get_reports($filter, $year_date, $today, $month_start, $month_end, $last_month_start, $last_month_end, $this_year_start){
if($filter==""){
return $this->db->select("b.*, CONCAT(c.first_name,' ',c.last_name) as fullname", FALSE)
->order_by('fullname')
->from('billing as b')
->where('billing_date >=', $month_start)
->where('billing_date <=', $month_end)
->join('clients as c', 'b.customer_uuid=c.uuid')
->get()
->result();
}elseif($filter == "this_month"){
return $this->db->select("b.*, CONCAT(c.first_name,' ',c.last_name) as fullname", FALSE)
->order_by('fullname')
->from('billing as b')
->where('billing_date >=', $month_start)
->where('billing_date <=', $month_end)
->join('clients as c', 'b.customer_uuid=c.uuid')
->get()
->result();
}elseif($filter == "last_month"){
return $this->db->select("b.*, CONCAT(c.first_name,' ',c.last_name) as fullname", FALSE)
->order_by('fullname')
->from('billing as b')
->where('billing_date >=', $last_month_start)
->where('billing_date <=', $last_month_end)
->join('clients as c', 'b.customer_uuid=c.uuid')
->get()
->result();
}elseif($filter == "monthly" || $filter == "annual"){
return $this->db->select("b.*, CONCAT(c.first_name,' ',c.last_name) as fullname", FALSE)
->order_by('fullname')
->from('billing as b')
->where('billing_year', $year_date)
->join('clients as c', 'b.customer_uuid=c.uuid')
->get()
->result();
}elseif($filter == "ytd"){
return $this->db->select("b.*, CONCAT(c.first_name,' ',c.last_name) as fullname", FALSE)
->order_by('fullname')
->from('billing as b')
->where('billing_date >=', $this_year_start)
->where('billing_date <=', $today)
->join('clients as c', 'b.customer_uuid=c.uuid')
->get()
->result();
}
}
【问题讨论】:
-
@ahmad:见我上面的代码。
-
无论如何,您正在编写一个文件,该文件保留在服务器上,请添加您的模型代码并让我知道哪些地方不能正常工作,以便您获得更多帮助。
-
@ahmad:我已经添加了模型。请参阅上面的代码。
-
从您的查询中丢失 ->result(),xml_from_result 获取查询而不是结果本身。
-
@ahmad:还是不行。
标签: codeigniter export-to-excel