导出Excel始终为空

时间:2018-10-02 11:27:51

标签: php mysql excel export codeigniter-2

我想将数据库中的文件导出为ex​​cel文件但是...

  • 我打开时excel文件始终为空,数据存在于 数据库不为空
  • 但是我对Excel仍然一无所获,我想念什么?

这是我的功能:

$path_file = './assets/report/excel/';      
            $styleBorder = array();
            $data = array();            
            $data['param'] = array(
                "start_date"    => $this->input->post('start_date'),
                "end_date"      => $this->input->post('end_date'),
            );              
                error_reporting(E_ALL);
                ini_set('display_errors', TRUE);
                ini_set('display_startup_errors', TRUE);

                require_once APPPATH.'libraries/PHPExcel.php';
                require_once APPPATH.'libraries/PHPExcel/IOFactory.php';

                $objPHPExcel = new PHPExcel();
                $objPHPExcel->getProperties()->setCreator($this->jCfg['user']['fullname'])
                                             ->setTitle("Export Data Transaksi SMS pt company")
                                             ->setSubject("Export Data Transaksi SMS pt company")
                                             ->setDescription("Export Data Transaksi SMS pt company")
                                             ->setKeywords("pt company")
                                             ->setCategory("Export Data Transaksi SMS pt company");
                $styleMainTitle = array(
                    'font' => array(
                        'bold' => true,
                        'size' => '16'
                    ),
                    'alignment' => array(
                        'vertical'      => PHPExcel_Style_Alignment::VERTICAL_CENTER,
                        'horizontal'    => PHPExcel_Style_Alignment::HORIZONTAL_CENTER
                    )
                );
                $styleTitle = array(
                    'font' => array(
                        'bold' => true,
                        'size' => '12'
                    )
                );
                $styleBorder = array(
                    'borders'   => array(
                        'allborders' => array('style' => PHPExcel_Style_Border::BORDER_THIN)
                    ),
                    'alignment' => array(
                        'vertical'      => PHPExcel_Style_Alignment::VERTICAL_CENTER
                    )
                );
                $styleHeader = array(
                    'font' => array(
                        'bold' => true,
                        'size' => '10'
                    ),
                    'borders' => array(
                        'allborders' => array('style' => PHPExcel_Style_Border::BORDER_THIN),
                        'bottom' => array(
                            'style' => PHPExcel_Style_Border::BORDER_DOUBLE
                        )
                    ),
                    'alignment' => array(
                        'vertical'      => PHPExcel_Style_Alignment::VERTICAL_CENTER,
                        'horizontal'    => PHPExcel_Style_Alignment::HORIZONTAL_CENTER
                    )               
                );
                    $objWorksheet = new PHPExcel_Worksheet($objPHPExcel); 
                    $objWorksheet->setTitle("TRANSAKSI SMS");
                    $objWorksheet->setShowGridlines(true);
                    $objPHPExcel->addSheet($objWorksheet);
                    $objWorksheet->mergeCells('A1:F1');                     $objWorksheet->setCellValue('A1', 'TABEL TRANSAKSI SMS pt company');
                    $objWorksheet->mergeCells('A2:F2');
                    $objWorksheet->setCellValue('A2', 'PERIODE '.$this->input->post('start_date')." - ".$this->input->post('end_date'));
                    $objWorksheet->setCellValue('A3', 'No.');
                    $objWorksheet->setCellValue('B3', 'Tanggal Transaksi');
                    $objWorksheet->setCellValue('C3', 'Jumlah Tarik');
                    $objWorksheet->setCellValue('D3', 'Jumlah SMS Tarik');
                    $objWorksheet->setCellValue('E3', 'Jumlah Transfer');
                    $objWorksheet->setCellValue('F3', 'Jumlah SMS Transfer');
                    $objWorksheet->getColumnDimension('A')->setWidth(10);                   
                    $objWorksheet->getColumnDimension('B')->setWidth(30);                   
                    $objWorksheet->getColumnDimension('C')->setWidth(30);                   
                    $objWorksheet->getColumnDimension('D')->setWidth(30);                   
                    $objWorksheet->getColumnDimension('E')->setWidth(30);                   
                    $objWorksheet->getColumnDimension('F')->setWidth(30);                   
                    $objPHPExcel->getDefaultStyle('A3:F3')->applyFromArray($styleMainTitle);
                    $objWorksheet->getPageSetup()->setRowsToRepeatAtTop(array(1, 3));           
                    $objWorksheet->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_LETTER);
                    $objWorksheet->getPageSetup()->setScale(45);
                    $start_row = 4;
                    $no=1;
                    foreach ((array)get_all_trx_petugas('',$this->input->post('start_date'),$this->input->post('end_date')) as $p => $q) {
                        $total_trx_tarik    = $total_trx_tarik + $q->tarik;
                        $total_trx_transfer = $total_trx_transfer + $q->transfer;
                        $total_trx_sms_tarik    = $total_trx_sms_tarik + ($q->tarik*cfg('app_harga_sms'));
                        $total_trx_sms_transfer = $total_trx_sms_transfer + ($q->transfer*cfg('app_harga_sms'));

                    $objWorksheet->setCellValue('A'.$start_row, $no++)
                                 ->setCellValue('B'.$start_row, ' '.$q->tgl)
                                 ->setCellValue('C'.$start_row, $q->tarik)
                                 ->setCellValue('D'.$start_row, $q->tarik*cfg('app_harga_sms'))
                                 ->setCellValue('E'.$start_row, $q->transfer)
                                 ->setCellValue('F'.$start_row, $q->transfer*cfg('app_harga_sms'));
                    $start_row++;
                    }
                    $start_row = $start_row+1;
                    $objWorksheet->mergeCells('A'.$start_row.':B'.$start_row);                  
                    $objWorksheet->setCellValue('A'.$start_row, 'TOTAL TRANSAKSI');
                    $objWorksheet->setCellValue('C'.$start_row, $total_trx_tarik)
                                 ->setCellValue('D'.$start_row, $total_trx_sms_tarik)
                                 ->setCellValue('E'.$start_row, $total_trx_transfer)
                                 ->setCellValue('F'.$start_row, $total_trx_sms_transfer);
                $file_name = date("dmyhis").'_report_transaksi_sms';        
                // Miscellaneous glyphs, UTF-8
                $id = ""; $count = 0;
                $start_row = 2;
                $no=1;
                header('Content-Type: application/vnd.ms-excel');
                header('Content-Disposition: attachment;filename="'.$file_name.'.xlsx"');
                header('Cache-Control: max-age=0');             
                $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
                $objWriter->save($path_file.$file_name);
                $objWriter->save('php://output');
                ob_clean();
                flush(); 
                readfile($path_file.$file_name);

这是我的get_all_trx_petugas函数,下面是代码:

function get_all_trx_petugas($petugas="",$start_date="",$end_date=""){
    $CI = getCI();
    $petugas = !empty($petugas)?"AND norekPetugas LIKE '%$petugas%'":'';
    $m = $CI->db->query("
            select DATE_FORMAT(dateTrx, '%d %M %Y') as tgl, 
            COUNT(if(trxType = '21', idTransaksi, NULL)) as setor,
            COUNT(if(trxType = '01', idTransaksi, NULL)) as tarik,
            COUNT(if(trxType = '49', idTransaksi, NULL)) as transfer,
            SUM(if(trxType = '21', amount, 0)) as amount_setor,
            SUM(if(trxType = '01', amount, 0)) as amount_tarik,
            SUM(if(trxType = '49', amount, 0)) as amount_transfer
            FROM transaksi
            WHERE DATE_FORMAT(dateTrx,'%Y-%m-%d') >= '$start_date' AND DATE_FORMAT(dateTrx,'%Y-%m-%d') <= '$end_date' 
            $petugas
            GROUP BY DATE_FORMAT(dateTrx,'%Y-%m-%d')
            ORDER BY DATE_FORMAT(dateTrx,'%Y-%m-%d') ASC
            ")->result();
    return $m;
}
  

excel文件始终为空

请帮助我。

0 个答案:

没有答案