2015-09-07 235 views
0

我已经在View上创建了表格,现在我想使用CodeIgniter将其报告给Excel(PHPExcel),我试图找到一个简单的报告系统,这里是我的控制器上的脚本远:如何使用CodeIgniter从HTML表格创建Excel电子表格

function excel(){ 
    $data['bf']=$this->report->branch(); 


    $tahun = $this->input->post('tahun'); 
    $branch= $this->input->post('branch'); 

    /*$data['data'] = $this->report->getdata($tahun); 
    $data['data2'] = $this->report->getdata($tahun); 
    // $data['bfb'] = $this->report->getdata($tahun,$branch); */ 
    $data['show'] = $this->report->show(); 

    $data['bal'] = $this->report->getbal($tahun); 
    $data['bdg'] = $this->report->getbdg($tahun); 
     $data['bgr'] = $this->report->getbgr($tahun); 
     $data['bjm'] = $this->report->getbjm($tahun); 
     $data['bkp'] = $this->report->getbkp($tahun); 
     $data['bks'] = $this->report->getbks($tahun); 
     $data['bnk'] = $this->report->getbnk($tahun); 
     $data['crb'] = $this->report->getcrb($tahun); 
     $data['jkt'] = $this->report->getjkt($tahun); 
     $data['jktm'] = $this->report->getjktm($tahun); 
     $data['jmb'] = $this->report->getjmb($tahun); 
     $data['knd'] = $this->report->getknd($tahun); 
     $data['lpg'] = $this->report->getlpg($tahun); 
     $data['mad'] = $this->report->getmad($tahun); 
     $data['mdn'] = $this->report->getmdn($tahun); 
     $data['mks'] = $this->report->getmks($tahun); 
     $data['mlg'] = $this->report->getmlg($tahun); 
     $data['pkb'] = $this->report->getpkb($tahun); 
     $data['plb'] = $this->report->getplb($tahun); 
     $data['pnt'] = $this->report->getpnt($tahun); 


    $html = $this->load->view('report/report', $data, true); 


$tmpfile = time().'.html'; 
file_put_contents($tmpfile, $html); 


$reader = new PHPExcel_Reader_HTML; 
$content = $reader->load($tmpfile); 

// Pass to writer and output as needed 
$objWriter = PHPExcel_IOFactory::createWriter($content, 'Excel5'); 
$objWriter->save('excelfile.xls'); 


unlink($tmpfile); 

    } 

当我使用该脚本,,我不能下载它,请纠正我的剧本,感谢

+0

你将文件保存到'在Web服务器上excelfile.xls' Excel导出....你再使用' readfile()'或类似的尝试和下载? –

+0

你为什么不简单地设置适当的头文件,然后保存到'php://输出' –

+0

你能给我举例使用头文件吗? – Pandu44

回答

0

这样?

function excel(){ 

    $CI->load->library('Excel'); 
    $data['bf']=$this->report->branch(); 


    $tahun = $this->input->post('tahun'); 
    $branch= $this->input->post('branch'); 

    /*$data['data'] = $this->report->getdata($tahun); 
    $data['data2'] = $this->report->getdata($tahun); 
    // $data['bfb'] = $this->report->getdata($tahun,$branch); */ 
    $data['show'] = $this->report->show(); 

    $data['bal'] = $this->report->getbal($tahun); 
    $data['bdg'] = $this->report->getbdg($tahun); 
     $data['bgr'] = $this->report->getbgr($tahun); 
     $data['bjm'] = $this->report->getbjm($tahun); 
     $data['bkp'] = $this->report->getbkp($tahun); 
     $data['bks'] = $this->report->getbks($tahun); 
     $data['bnk'] = $this->report->getbnk($tahun); 
     $data['crb'] = $this->report->getcrb($tahun); 
     $data['jkt'] = $this->report->getjkt($tahun); 
     $data['jktm'] = $this->report->getjktm($tahun); 
     $data['jmb'] = $this->report->getjmb($tahun); 
     $data['knd'] = $this->report->getknd($tahun); 
     $data['lpg'] = $this->report->getlpg($tahun); 
     $data['mad'] = $this->report->getmad($tahun); 
     $data['mdn'] = $this->report->getmdn($tahun); 
     $data['mks'] = $this->report->getmks($tahun); 
     $data['mlg'] = $this->report->getmlg($tahun); 
     $data['pkb'] = $this->report->getpkb($tahun); 
     $data['plb'] = $this->report->getplb($tahun); 
     $data['pnt'] = $this->report->getpnt($tahun); 


    $html = $this->load->view('report/report', $data, true); 
    $filename = 'Report Data Actual vs Target Branch '.date('Y-m-d').'.xls';      //save our workbook as this file name 
    header('Content-Type: application/vnd.ms-excel'); //mime type 
    header('Content-Disposition: attachment;filename="' . $filename . '"'); //tell browser what's the file name 
    header('Cache-Control: max-age=0'); //no cache 
    //save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type) 
    //if you want to save it as .XLSX Excel 2007 format 
    $objWriter = PHPExcel_IOFactory::createWriter($CI->excel, 'Excel5'); 
    //force user to download the Excel file without writing it to server's HD 
    $objWriter->save('php://output'); 
+0

我应该在哪里加载$ html? – Pandu44