Xin chào các bạn nay mình sẽ hướng dẫn các bạn các xuất tất cả khách hàng ra file excel
Trong phần themes : thêm dòng xuất Xuất toàn bộ khách hàng ở trong view để người dùng có thể xuất
<li> <a href="#" id="excel_all" data-action="export_all"> <i class="fa fa-file-excel-o"></i> Xuất toàn bộ khách hàng </a> </li>
Tiếp đó là phần js để khi click vào link xuất toàn bộ khách hàng
<script> $('body').on('click', '#excel_all', function(e) { e.preventDefault(); $('#form_action').val($(this).attr('data-action')); $('#action-form-submit').trigger('click'); }); </script>
Ok vậy là xong đến bây giờ xử lý phần controller :
if ($this->input->post('form_action') == 'export_all') { $this->load->library('excel'); $this->excel->setActiveSheetIndex(0); $this->excel->getActiveSheet()->setTitle(lang('customer')); $this->excel->getActiveSheet()->SetCellValue('A1', lang('company')); $this->excel->getActiveSheet()->SetCellValue('B1', lang('name')); $this->excel->getActiveSheet()->SetCellValue('C1', lang('email')); $this->excel->getActiveSheet()->SetCellValue('D1', lang('phone')); $this->excel->getActiveSheet()->SetCellValue('E1', lang('address')); $this->excel->getActiveSheet()->SetCellValue('F1', lang('city')); $this->excel->getActiveSheet()->SetCellValue('G1', lang('state')); $this->excel->getActiveSheet()->SetCellValue('H1', lang('postal_code')); $this->excel->getActiveSheet()->SetCellValue('I1', lang('country')); $this->excel->getActiveSheet()->SetCellValue('J1', lang('vat_no')); $this->excel->getActiveSheet()->SetCellValue('K1', lang('deposit_amount')); $this->excel->getActiveSheet()->SetCellValue('L1', lang('ccf1')); $this->excel->getActiveSheet()->SetCellValue('M1', lang('ccf2')); $this->excel->getActiveSheet()->SetCellValue('N1', lang('ccf3')); $this->excel->getActiveSheet()->SetCellValue('O1', lang('ccf4')); $this->excel->getActiveSheet()->SetCellValue('P1', lang('ccf5')); $this->excel->getActiveSheet()->SetCellValue('Q1', lang('ccf6')); $row = 2; $all_user = $this->site->getAllCompanies('customer'); foreach ($all_user as $customer) { $this->excel->getActiveSheet()->SetCellValue('A' . $row, $customer->company); $this->excel->getActiveSheet()->SetCellValue('B' . $row, $customer->name); $this->excel->getActiveSheet()->SetCellValue('C' . $row, $customer->email); $this->excel->getActiveSheet()->SetCellValue('D' . $row, $customer->phone); $this->excel->getActiveSheet()->SetCellValue('E' . $row, $customer->address); $this->excel->getActiveSheet()->SetCellValue('F' . $row, $customer->city); $this->excel->getActiveSheet()->SetCellValue('G' . $row, $customer->state); $this->excel->getActiveSheet()->SetCellValue('H' . $row, $customer->postal_code); $this->excel->getActiveSheet()->SetCellValue('I' . $row, $customer->country); $this->excel->getActiveSheet()->SetCellValue('J' . $row, $customer->vat_no); $this->excel->getActiveSheet()->SetCellValue('K' . $row, $customer->deposit_amount); $this->excel->getActiveSheet()->SetCellValue('L' . $row, $customer->cf1); $this->excel->getActiveSheet()->SetCellValue('M' . $row, $customer->cf2); $this->excel->getActiveSheet()->SetCellValue('N' . $row, $customer->cf3); $this->excel->getActiveSheet()->SetCellValue('O' . $row, $customer->cf4); $this->excel->getActiveSheet()->SetCellValue('P' . $row, $customer->cf5); $this->excel->getActiveSheet()->SetCellValue('Q' . $row, $customer->cf6); $row++; } $this->excel->getActiveSheet()->getColumnDimension('A')->setWidth(20); $this->excel->getActiveSheet()->getColumnDimension('B')->setWidth(20); $this->excel->getActiveSheet()->getColumnDimension('C')->setWidth(20); $this->excel->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); $filename = 'customers_all_' . date('Y_m_d_H_i_s'); $this->load->helper('excel'); create_excel($this->excel, $filename); }
Phần model chúng ta sử dụng lại function ở Site :
public function getAllCompanies($group_name) { $q = $this->db->get_where('companies', array('group_name' => $group_name)); if ($q->num_rows() > 0) { foreach (($q->result()) as $row) { $data[] = $row; } return $data; } return FALSE; }