We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

create Excel file in Phalcon php framework

I'm working in phalcon php framework and i am trying to create simple xsl file, but when i call my controller i get empty excel table. Can anyone tell me what is wrong in my code. Here is my method:

   public function createExcelAction(){

$loader = new \Phalcon\Loader();

   $loader->registerClasses(
     array(
    "PHPExcel"=>"C:/wamp/www/phalcon1/Classes/PHPExcel.php",
  "PHPExcel_Writer_Excel2007"=>"C:/wamp/www/phalcon1/Classes/PHPExcel/Writer/PHPExcel_Writer_Excel2007.php"
    )
     );
     $loader->register();
     $excel = new PHPExcel();
           //$excel = new PHPExcel();
    //activate worksheet number 1
    $excel->setActiveSheetIndex(0);
    //name the worksheet
    $excel->getActiveSheet()->setTitle('test worksheet');
    //set cell A1 content with some text

    $excel->getActiveSheet()->setCellValue('A1', 'Rezultati pretrage');
    $excel->getActiveSheet()->setCellValue('A2', "Ime");
    $excel->getActiveSheet()->setCellValue('C2', "Prezime");
    $excel->getActiveSheet()->setCellValue('F2', "Adresa stanovanja");

    $br = rand(0,1000000);
    $naziv = "Pobjeda-Rudet izvjestaj-".$br.".xls";
   //$filename='pobjeda_rudet.xlsx'; //save our workbook as this file name
       header('Content-Type: application/vnd.ms-excel'); //mime typež
     header('Content-Disposition: attachment;filename="'.$naziv.'"');
   // header('Content-Disposition: attachment;filename="'.$filename.'"'); 
      header('Cache-Control: max-age=0'); //no cache
     $objWriter = new PHPExcel_Writer_Excel2007($excel);
     $objWriter->save(str_replace('.php', '.xls', __FILE__));

Remove "header" instructions.

PHPExcel class to add the required information in the header



18.7k

can you provide me some example, how to add required info in header using PHPExcel, because i deal with this for the first time. Thanks in advace.

public function createExcelAction() { global $config;

    $loader = new \Phalcon\Loader();

    $loader->registerDirs(
        array(
            $config->application->libraryDir."PHPExcel/"
        )
    );

    // register autoloader
    $loader->register();

    $excel = new PHPExcel();
    $excel->setActiveSheetIndex(0);
    $excel->getActiveSheet()->setTitle('test worksheet');

    $excel->getActiveSheet()->setCellValue('A1', 'Rezultati pretrage');
    $excel->getActiveSheet()->setCellValue('A2', "Ime");
    $excel->getActiveSheet()->setCellValue('C2', "Prezime");
    $excel->getActiveSheet()->setCellValue('F2', "Adresa stanovanja");

    $br = rand(0,1000000);
    $naziv = $br.".xls";
    //$filename='pobjeda_rudet.xlsx'; //save our workbook as this file name
    //header('Content-Type: application/vnd.ms-excel'); //mime typež
    //header('Content-Disposition: attachment;filename="'.$naziv.'"');
    // header('Content-Disposition: attachment;filename="'.$filename.'"'); 
    //header('Cache-Control: max-age=0'); //no cache
    $objWriter = new PHPExcel_Writer_Excel2007($excel);
    $objWriter->save('../tmp/'.$naziv);

}

the tmp directory (in root folder project) needs to be writable

Create MyTags function name createExcelAction() Fatal error: Class 'PHPExcel' not found in /app/customhelpers/MyTags.php on line 1145