简体   繁体   English

如何使用PHPExcel Cakephp 2.0将数据导出到Excel文件

[英]How to export data to an excel file using PHPExcel Cakephp 2.0

I an trying to export data to an Excel file using PHPExcel libraries with Cakephp 2.5. 我试图使用带有Cakephp 2.5的PHPExcel库将数据导出到Excel文件。 My Codes : 我的代码:

<?php

App::import('Vendor', 'PHPExcel', array('file' => 'PHPExcel'.DS.'PHPExcel.php'));
App::import('Vendor', 'PHPExcel_IOFactory', array('file' => 'PHPExcel'.DS.'PHPExcel'.DS.'IOFactory.php'));
App::import('Vendor', 'PHPExcel_IOFactory', array('file' => 'PHPExcel'.DS.'PHPExcel'.DS.'Style.php'));

class LeadUploadController extends AppController {

    public function exel_download($emp_id='')
    {
        $this->autoRender = false;
        $this-> layout='ajax';

        $objPHPExcel = new PHPExcel();
        $serialnumber=0;
        $tmparray =array("Sr.Number","Employee ID","Employee Name");
        $sheet =array($tmparray);
        $tmparray =array();
        $serialnumber = $serialnumber + 1;
        array_push($tmparray,$serialnumber);
        $employeelogin = 'aa';
        array_push($tmparray,$employeelogin);
        $employeename = 'bb';
        array_push($tmparray,$employeename);   
        array_push($sheet,$tmparray);
        header('Content-type: application/vnd.ms-excel');
        header('Content-Disposition: attachment; filename="file.xlsx"');
        $worksheet = $objPHPExcel->getActiveSheet();
        foreach($sheet as $row => $columns) {
        foreach($columns as $column => $data) {
        $worksheet->setCellValueByColumnAndRow($column, $row + 1, $data);
        }
        }
        $objPHPExcel->getActiveSheet()->getStyle("A1:I1")->getFont()->setBold(true);
        $objPHPExcel->setActiveSheetIndex(0);
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save(str_replace('.php', '.xlsx', __FILE__));
    }
}

The problem is the downloaded Excel file contain no any data it completely blank with an error "can not open the file because of file format or extension not valid ". 问题是下载的Excel文件不包含任何数据,它完全空白,并出现错误“由于文件格式或扩展名无效而无法打开文件”。 Have not any idea what's wrong with these code. 不知道这些代码有什么问题。

The following code should work on xls extension. 以下代码应适用于xls扩展。

    $objPHPExcel = new PHPExcel();
    $serialnumber=0;
    $tmparray =array("Sr.Number","Employee ID","Employee Name");
    $sheet =array($tmparray);
    $tmparray =array();
    $serialnumber = $serialnumber + 1;
    array_push($tmparray,$serialnumber);
    $employeelogin = 'aa';
    array_push($tmparray,$employeelogin);
    $employeename = 'bb';
    array_push($tmparray,$employeename);   
    array_push($sheet,$tmparray);
    header('Content-type: application/vnd.ms-excel');
    header('Content-Disposition: attachment; filename="file.xls"');
    $worksheet = $objPHPExcel->getActiveSheet();
    foreach($sheet as $row => $columns) {
        foreach($columns as $column => $data) {
            $worksheet->setCellValueByColumnAndRow($column, $row + 1, $data);
        }
    }
    $objPHPExcel->getActiveSheet()->getStyle("A1:I1")->getFont()->setBold(true);
    $objPHPExcel->setActiveSheetIndex(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