使用 php excel 库错误将数据导出到 codeigniter 中的 excel

exporting data to excel in codeigniter usingt php excel library error

我正在尝试使用 php excel 库将数据从 table 导出到 excel 文件,我的控制器如下所示:

public function exportexcel() {
    $this->load->model("excel_import_model");

    $var = $this->excel_import_model->excelexport();
    $this->load->library('excel');

    $object = new PHPExcel();
    $object->setActiveSheetIndex(0);

    $table_columns = array("Name", "Address", "Gender", "Designation", "Age");

    $column = 0;

    foreach($table_columns as $field) {
        $object->getActiveSheet()->setCellValueByColumnAndRow($column, 1, $field);
        $column++;
    }

    $employee_data = $this->excel_import_model->excelexport();

    $excel_row = 2;

    foreach($employee_data as $row) {
        $object->getActiveSheet()->setCellValueByColumnAndRow(0, $excel_row, $row->sendername);
        $object->getActiveSheet()->setCellValueByColumnAndRow(1, $excel_row, $row->sendercity);
        $object->getActiveSheet()->setCellValueByColumnAndRow(2, $excel_row, $row->senderphone);
        $object->getActiveSheet()->setCellValueByColumnAndRow(3, $excel_row, $row->awb);
        $object->getActiveSheet()->setCellValueByColumnAndRow(4, $excel_row, $row->sendermobile);
        $excel_row++;
    }
    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

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

我的模型如下:

function excelexport()
{
   $this->db->order_by("id", "DESC");
   $query = $this->db->get("consignments");
   return $query->result();
}

单击按钮时,正在下载 excel 文件,但文件显示如下图所示:

任何人都可以告诉我这里有什么问题,在此先感谢

您可以使用以下代码,它将以 .xlsx 格式下载

$file_name = time().".xlsx";
$object_writer = PHPExcel_IOFactory::createWriter($object, 'Excel2007');
ob_end_clean();
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename='.$file_name);
$object_writer->save('php://output');