2015-07-21 46 views
1

我有這個控制器,它將我的數據從數據庫呈現給PHPExcel庫。PHPExcel:輸出CodeIgniter錯誤

https://arjunphp.com/how-to-use-phpexcel-with-codeigniter/

這是我的控制器:

public function downloadTournamentData() { 
    $this->load->model("Tournament"); 

    $var_data = array(); 

    $var_data['tournament'] = $this->Tournament->getTournamentData(); 

    $this->load->library('excel'); 

    $this->excel->setActiveSheetIndex(0); 

    $this->excel->getActiveSheet()->setTitle('test'); 

    $this->excel->getActiveSheet()->setCellValue('A1', 'member_username'); 
    $this->excel->getActiveSheet()->setCellValue('B1', 'member_name'); 
    $this->excel->getActiveSheet()->setCellValue('C1', 'member_phone'); 
    $this->excel->getActiveSheet()->setCellValue('D1', 'member_email'); 
    $this->excel->getActiveSheet()->setCellValue('E1', 'member_idcard'); 
    $this->excel->getActiveSheet()->setCellValue('F1', 'Date_created'); 
    $this->excel->getActiveSheet()->setCellValue('G1', 'team_name'); 
    $this->excel->getActiveSheet()->setCellValue('H1', 'warnet_name'); 
    $this->excel->getActiveSheet()->setCellValue('I1', 'warnet_cp'); 
    $this->excel->getActiveSheet()->setCellValue('J1', 'warnet_phone'); 
    $this->excel->getActiveSheet()->setCellValue('K1', 'region_name'); 
    $this->excel->getActiveSheet()->setCellValue('L1', 'City'); 


    $cell_inc = 2; 
    foreach($var_data['tournament'] as $k => $v) { 
     $this->excel->getActiveSheet()->setCellValue('A'.$cell_inc, $v['member_username']); 
     $this->excel->getActiveSheet()->setCellValue('B'.$cell_inc, $v['member_name']); 
     $this->excel->getActiveSheet()->setCellValue('C'.$cell_inc, $v['member_phone']); 
     $this->excel->getActiveSheet()->setCellValue('D'.$cell_inc, $v['member_email']); 
     $this->excel->getActiveSheet()->setCellValue('E'.$cell_inc, $v['member_idcard']); 
     $this->excel->getActiveSheet()->setCellValue('F'.$cell_inc, $v['Date_created']); 
     $this->excel->getActiveSheet()->setCellValue('G'.$cell_inc, $v['team_name']); 
     $this->excel->getActiveSheet()->setCellValue('H'.$cell_inc, $v['warnet_name']); 
     $this->excel->getActiveSheet()->setCellValue('I'.$cell_inc, $v['warnet_cp']); 
     $this->excel->getActiveSheet()->setCellValue('J'.$cell_inc, $v['warnet_phone']); 
     $this->excel->getActiveSheet()->setCellValue('K'.$cell_inc, $v['region_name']); 
     $this->excel->getActiveSheet()->setCellValue('L'.$cell_inc, $v['City']); 

     $cell_inc++; 
    } 

    date_default_timezone_set("Asia/Jakarta"); 

    $this_date = date("Y-m-d"); 

    $filename='pb_turnamen_data-'.$this_date.'.xls'; //save our workbook as this file name 
    header('Content-Type: application/vnd.ms-excel; charset=UTF-8'); //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($this->excel, 'Excel5'); 
    //force user to download the Excel file without writing it to server's HD 
    $objWriter->save('php://output'); 
} 

它下載Excel文件。唯一的問題是數據混淆,並且都是錯誤的。

Screenshot of downloaded excel file

這究竟是爲什麼?我的控制器有問題嗎?

請提供解決方案...

+0

難道是提示您選擇字符集,而在Excel中打開它?嘗試將字符集設置爲UTF-8,同時將其打開爲excel。 –

+0

如果您直接向瀏覽器發送電子表格,則代碼中的任何地方或框架 –

+0

@DishaV都絕對不會生成額外的輸出。 :不,它不會提示我設置字符集。錯誤是「xxx.xls的文件格式和擴展名不匹配...」 – godheaper

回答

2

添加 ob_end_clean(); $ objWriter = PHPExcel_IOFactory :: createWriter($ this-> excel,'Excel5');

最終代碼是

$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5'); 
ob_end_clean(); 
$objWriter->save('php://output');