简体   繁体   English

使用 phpExcel 在 php codeigniter 中打开生成的 excel 文件时出错

[英]Error while opening generated excel file in php codeigniter using phpExcel

This how look generated excel file: with the warning messages: Message: "continue" targeting switch is equivalent to "break".这看起来如何生成 excel 文件:带有警告消息:消息:“继续”目标开关相当于“中断”。 Did you mean to use "continue 2"?您是要使用“继续 2”吗? Filename: Shared/OLE.php文件名:共享/OLE.php

This is comes in excel file - þÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ @€Åfôí Ö@€Åfôí ÖþÿÕÍÕœ.“—+,ù®0¼HPX`hp This is comes in excel file - þÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ @€Åfôí Ö@€Åfôí ÖþÿÕÍÕœ.“—+,ù®0¼HPX`hp

I have tried with iconv(mb_detect_encoding($result, mb_detect_order(), true), "UTF-8", $result);我试过 iconv(mb_detect_encoding($result, mb_detect_order(), true), "UTF-8", $result); coversion as well.覆盖也是如此。

This is the code:这是代码:

 public function exportExcel() {
    $this->load->library('excel');
    $registrationIDArr = $_POST["registrationID"];
    $resigtrationIDStr = implode(",",$registrationIDArr);
    $currDate = date("d-m-Y_H_i");
    $file = 'VolunteerRegistration_'.$currDate.'.xls';
    $objPHPExcel = new PHPExcel();
    $objPHPExcel->setActiveSheetIndex(0);
    $objPHPExcel->getActiveSheet()->setTitle('Volunteer Details');
    $style = array('font' => array('size' => 12,'bold' => true));

    $objPHPExcel->getActiveSheet()->getStyle('A1:B1')->applyFromArray($style);

    $objPHPExcel->getActiveSheet()->setCellValue('A1', 'Name');
    $objPHPExcel->getActiveSheet()->setCellValue('B1', 'Email');

    $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(25);
    $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(25);

    $rows = 2;

    $result = $this->getVolunteerDetailsForExcelExport($resigtrationIDStr);

    foreach($result as $row){ 
      $objPHPExcel->getActiveSheet()->setCellValue('A'.$rows, $row->name);
      $objPHPExcel->getActiveSheet()->setCellValue('B'.$rows, $row->email);
      $rows++;  

    }


    header('Content-Type: application/vnd.ms-excel; charset=UTF-8');
    header('Content-Disposition: attachment; filename="'.$file.'"');



    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');


   ob_end_clean();
   ob_start();
    $objWriter->save('php://output');
     exit;
}

public function getVolunteerDetailsForExcelExport($registrationIDStr){

    $this->db->select("CONCAT(fname,' ',mname,' ',lname) AS name, email");
    $this->db->from('UserRegistration');
    $wherelist = "id in($registrationIDStr)";
    $this->db->where($wherelist);
    $query= $this->db->get();

    $result = $query->result();

    return $result;

} 

In PHPExcel/Shared/OLE.php, function _readPpsWks($blockId) , there is an error in the for / switch structure.在PHPExcel/Shared/OLE.php, function _readPpsWks($blockId)中,for / switch 结构有错误。
In the default case of the switch, you write continue;在 switch 的默认情况下,你写continue; . . But this does NOT exit the for loop.但这不会退出for循环。 It only exits the "switch" structure, and continue under.它只退出“switch”结构,继续下。 You have to write: continue 2;你必须写: continue 2; , at line number 290. ,第 290 行。

default:
    continue 2;

Please try this, it might helps.请试试这个,它可能会有所帮助。 This works for me.这对我有用。

require 'PHPExcel.php';
$objPHPExcel = new PHPExcel();
/*CODE for excel*/
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="file.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM