Example Method for the Yii Framework Framework to Use PHPExcel Components

  • 2021-12-13 16:29:01
  • OfStack

This article illustrates how the Yii Framework framework uses PHPExcel components. Share it for your reference, as follows:

PHPExcel Download Address http://www.yiiframework.com/extension/phpexcel

Unzip the downloaded PHPExcel compressed package to the Yii Framework directory under framework\ vendors

The code is as follows


/**
*  Export data to Excel
*/
public function actionExport()
{
    // To export to Excel Data of 
    $criteria = $this->_getCriteria();
    $data = Statistics::model()->findAll($criteria);
    //  Get PHPExcel Reference path 
    $phpExcelPath = Yii::getPathOfAlias('system.vendors');
    //  Shut down YII The automatic loading function of, use manual loading instead, otherwise an error will occur, PHPExcel Has its own auto-load function 
    // YII The framework requires class name and file name for automatic loading of components 1 To; 
    //  And PHPExcel The file name corresponding to the class contains the parent directory name, such as: IOFactory The file name corresponding to the class is PHPExcel_IOFactory.php
    spl_autoload_unregister(array('YiiBase','autoload'));
    include($phpExcelPath . DIRECTORY_SEPARATOR . 'PHPExcel.php');
    // Below is Excel Data export processing logic 
    $objPHPExcel = PHPExcel_IOFactory::load('./content/template/report.xlsx');
    $objPHPExcel->getProperties()->setCreator("Kalman")
    ->setTitle(" Statistical report ")
    ->setSubject(" Statistical report ")
    ->setDescription(" Statistical report ");
    $objPHPExcel->setActiveSheetIndex(0)
    ->setCellValue('A1', 'Hello')
    ->setCellValue('B2', 'world!')
    ->setCellValue('C1', 'Hello')
    ->setCellValue('D2', 'world!');
    $objPHPExcel->setActiveSheetIndex(0)
    ->setCellValue('A25', '123456');
    $objPHPExcel->getActiveSheet()->setTitle('report');
    // Excel Sheet displayed after opening 
    $objPHPExcel->setActiveSheetIndex(0);
    // Output through browser Excel Report 
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="report.xlsx"');
    header('Cache-Control: max-age=0');
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    $objWriter->save('php://output');
    Yii::app()->end();
    // Recovery Yii Automatic loading function 
    spl_autoload_register(array('YiiBase','autoload'));
}

For more readers interested in Yii related contents, please check the topics on this site: "Introduction to Yii Framework and Summary of Common Skills", "Summary of Excellent Development Framework of php", "Basic Tutorial for Introduction to smarty Template", "Introduction to php Object-Oriented Programming", "Summary of Usage of php String (string)", "Introduction to php+mysql Database Operation" and "Summary of Common Database Operation Skills of php"

I hope this article is helpful to the PHP programming based on Yii framework.


Related articles: