【问题标题】:PHPExcel : CodeIgniter error on outputPHPExcel:输出时出现 CodeIgniter 错误
【发布时间】:2015-07-21 10:28:35
【问题描述】:

我有这个控制器,它将我的数据从数据库呈现到 PHPExcel 库。

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 文件。唯一的问题是数据混淆了,都是错误的。

为什么会这样?我的控制器有问题吗?

请提供解决方案...

【问题讨论】:

  • 在excel中打开时是否提示选择字符集?在打开 Excel 时尝试将字符集设置为 UTF-8。
  • 如果您将电子表格直接发送到浏览器,那么您的代码或框架中的任何地方都绝对不会产生额外的输出
  • @DishaV。 :不,它不会提示我设置字符集。错误是“'xxx.xls'的文件格式和扩展名不匹配......”
  • @MarkBaker:我没有看到在标头和 createWriter 之后/之前生成任何其他输出。但我不是在阅读电子表格,而是从数据库数据中生成电子表格。问题可能出在我加载 PHPExcel 库的方式上吗? CodeIgniter 对加载这个库有限制吗?

标签: php codeigniter phpexcel


【解决方案1】:

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

最终代码是

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多