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

问题描述 投票:1回答:1

此外观如何生成excel文件:带有警告消息:消息:“继续”定向开关等效于“中断”。您是要使用“继续2”吗?文件名:Shared / OLE.php

这是excel文件中的-þÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ@€ÅfôíÖ@€ÅfôíÖþÿÕÍÕœ。” — +,ù®0¼HPX`hp

我已经尝试使用iconv(mb_detect_encoding($ result,mb_detect_order(),true),“ UTF-8”,$ result);也包括在内。

这是代码:

 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;

} 
php codeigniter phpexcel
1个回答
0
投票

请尝试此操作,可能会有所帮助。这对我有用。

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');
© www.soinside.com 2019 - 2024. All rights reserved.