在codeigniter中使用PHPExcel从数据库创buildExcel工作表

我正在尝试使用Codeigniter中的PHPExcel来创build一个Excel数据库来生成报告。 我有主表sales和超过4个表格提取数据。 我已经使用表之间的多个JOINS写入查询。

这是我的情况:

在处理生成销售报告时,客户可能已经购买了多个产品,这些详细信息存储在另一个表sale_items 。 目前我已经写了一个主要查询与其他表的连接,并在foreach循环内获得结果后,我正在写另一个查询来从sale_items表中select相应的数据。

我试图在Excel表单中显示单个客户,如果他已经购买了多个产品,我需要在Excel表格中以逗号分隔显示这些产品。 我使用第二个查询来获取产品名称,并将结果分解,并试图在excel的相应行中显示。 它正常工作,如果我试图与一个客户。 如果我为多个客户尝试,该页面会显示服务器错误。

这是我的代码:

  $dateTmp = "DATE_FORMAT(".$this->db->dbprefix('sales').".date,'%m/%Y')"; $condition = "if(".$this->db->dbprefix('sales').".biller_id = 5, 'A', if(".$this->db->dbprefix('sales').".biller_id = 6, 'B',if(".$this->db->dbprefix('sales').".biller_id = 7,'C','D' )))"; if ($pdf || $xls) { $this->db ->select("date, ".$this->db->dbprefix('warehouses').".name, CONCAT(".$this->db->dbprefix('warehouses').".name,'-',".$dateTmp.",'-', ".$condition.",".$this->db->dbprefix('sales').".invoice_no) as month," ." biller, customer, scan_no, CONCAT(" . $this->db->dbprefix('sale_items') . ".product_name, '__', " . $this->db->dbprefix('sale_items') . ".quantity) as iname, unit_price,if(tax_rate_id=2,item_tax,0) as taxId1,if(tax_rate_id=4,item_tax,0) as taxId2 , tin,cin,cst,total_discount, item_tax, shipping,subtotal,payment_status,sales.id as saleid ", FALSE) ->from('sales') ->join('sale_items', 'sale_items.sale_id=sales.id', 'left') ->join('companies','companies.id=sales.customer_id', 'left') ->join('salersperson','salersperson.id=companies.salesperson_id', 'left') ->join('hosipital_group','hosipital_group.id=companies.hospital_id', 'left') ->join('doctor','doctor.id=companies.dr_id', 'left') ->join('warehouses', 'warehouses.id=sales.warehouse_id', 'left') ->group_by('sales.id') ->order_by('sales.date desc'); //MAIN JOIN QUERY $q = $this->db->get(); if ($q->num_rows() > 0) { foreach (($q->result()) as $row) { $data[] = $row; } } else { $data = NULL; } if (!empty($data)) { $this->load->library('excel'); $this->excel->setActiveSheetIndex(0); $this->excel->getActiveSheet()->setTitle(lang('sales_report')); $this->excel->getActiveSheet()->SetCellValue('A1', lang('date')); $this->excel->getActiveSheet()->SetCellValue('B1', lang('branch')); $this->excel->getActiveSheet()->SetCellValue('C1', lang('invoice_no')); $this->excel->getActiveSheet()->SetCellValue('D1', lang('biller')); $this->excel->getActiveSheet()->SetCellValue('E1', lang('customer')); $this->excel->getActiveSheet()->SetCellValue('F1', lang('tin')); $this->excel->getActiveSheet()->SetCellValue('G1', lang('cin')); $this->excel->getActiveSheet()->SetCellValue('H1', lang('cst')); $this->excel->getActiveSheet()->SetCellValue('I1', lang('scan_no')); $this->excel->getActiveSheet()->SetCellValue('J1', lang('product_qty')); $this->excel->getActiveSheet()->SetCellValue('K1', lang('prod_price')); $this->excel->getActiveSheet()->SetCellValue('L1', lang('vat1')); $this->excel->getActiveSheet()->SetCellValue('M1', lang('vat2')); $this->excel->getActiveSheet()->SetCellValue('N1', lang('discount')); $this->excel->getActiveSheet()->SetCellValue('O1', lang('product_tax')); $this->excel->getActiveSheet()->SetCellValue('P1', lang('shipping')); $this->excel->getActiveSheet()->SetCellValue('Q1', lang('total')); $row = 2; $total = 0; $paid = 0; $balance = 0; foreach ($data as $data_row) { $saleid=$data_row->saleid; $this->db->select("product_name") ->from('sale_items') ->where('sale_items.sale_id',$saleid); /*SECOND QUERY TO FETCH FROM SALE_ITEMS TABLE*/ $q1 = $this->db->get(); if ($q1->num_rows() > 0) { foreach (($q1->result()) as $row1) { $prdtarray[] = $row1->product_name; } $product=implode(',',$prdtarray); /*PRODUCTS NAME SEPARATED BY COMMA AND STORED IN A VARIABLE*/ } else { $data1 = NULL; } $this->excel->getActiveSheet()->SetCellValue('A' . $row, $this->sma->hrld($data_row->date)); $this->excel->getActiveSheet()->SetCellValue('B' . $row, $data_row->name); $this->excel->getActiveSheet()->SetCellValue('C' . $row, $data_row->month); $this->excel->getActiveSheet()->SetCellValue('D' . $row, $data_row->biller); $this->excel->getActiveSheet()->SetCellValue('E' . $row, $data_row->customer); $this->excel->getActiveSheet()->SetCellValue('F' . $row, $data_row->tin); $this->excel->getActiveSheet()->SetCellValue('G' . $row, $data_row->cin); $this->excel->getActiveSheet()->SetCellValue('H' . $row, $data_row->cst); $this->excel->getActiveSheet()->SetCellValue('I' . $row, $data_row->scan_no); $this->excel->getActiveSheet()->SetCellValue('J' . $row, $product); //THE COMMA SEPARATED PRODUCT NAMES $this->excel->getActiveSheet()->SetCellValue('K' . $row, $data_row->unit_price); $this->excel->getActiveSheet()->SetCellValue('L' . $row, $data_row->taxId1); $this->excel->getActiveSheet()->SetCellValue('M' . $row, $data_row->taxId2); $this->excel->getActiveSheet()->SetCellValue('N' . $row, $data_row->total_discount); $this->excel->getActiveSheet()->SetCellValue('O' . $row, $data_row->item_tax); $this->excel->getActiveSheet()->SetCellValue('P' . $row, $data_row->shipping); $this->excel->getActiveSheet()->SetCellValue('Q' . $row, $data_row->subtotal); $total += $data_row->subtotal; $paid += $data_row->item_tax; // $balance += ($data_row->grand_total - $data_row->paid); $row++; } $this->excel->getActiveSheet()->getStyle("L" . $row . ":M" . $row)->getBorders() ->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_MEDIUM); $this->excel->getActiveSheet()->SetCellValue('Q' . $row, $total); $this->excel->getActiveSheet()->SetCellValue('O' . $row, $paid); // $this->excel->getActiveSheet()->SetCellValue('H' . $row, $balance); $this->excel->getActiveSheet()->getColumnDimension('A')->setWidth(20); $this->excel->getActiveSheet()->getColumnDimension('B')->setWidth(20); $this->excel->getActiveSheet()->getColumnDimension('C')->setWidth(20); $this->excel->getActiveSheet()->getColumnDimension('D')->setWidth(30); $this->excel->getActiveSheet()->getColumnDimension('E')->setWidth(15); $this->excel->getActiveSheet()->getColumnDimension('F')->setWidth(15); $this->excel->getActiveSheet()->getColumnDimension('G')->setWidth(15); $this->excel->getActiveSheet()->getColumnDimension('H')->setWidth(20); $this->excel->getActiveSheet()->getColumnDimension('I')->setWidth(20); $this->excel->getActiveSheet()->getColumnDimension('J')->setWidth(20); $this->excel->getActiveSheet()->getColumnDimension('K')->setWidth(15); $this->excel->getActiveSheet()->getColumnDimension('L')->setWidth(30); $this->excel->getActiveSheet()->getColumnDimension('M')->setWidth(30); $this->excel->getActiveSheet()->getColumnDimension('N')->setWidth(15); $this->excel->getActiveSheet()->getColumnDimension('O')->setWidth(30); $this->excel->getActiveSheet()->getColumnDimension('P')->setWidth(30); $this->excel->getActiveSheet()->getColumnDimension('Q')->setWidth(30); $filename = 'sales_report'; $this->excel->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); if ($xls) { $this->excel->getActiveSheet()->getStyle('E2:E' . $row)->getAlignment()->setWrapText(true); ob_clean(); header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="' . $filename . '.xls"'); header('Cache-Control: max-age=0'); ob_clean(); $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5'); $objWriter->save('php://output'); exit(); } } $this->session->set_flashdata('error', lang('nothing_found')); //redirect($_SERVER["HTTP_REFERER"]); } 

我不知道当我尝试多个客户时会发生什么情况。 可能是我的逻辑错了。 任何人都可以帮我find我的错误..? 提前致谢