How to use PHPExcel to export an Excel file in the Yii Framework-PHP Tutorial

Source: Internet
Author: User
Tags php excel
Use PHPExcel to export an Excel file in the Yii Framework. I recently studied the Yii Framework of PHP and liked it. when I encountered the problem of exporting Excel, I studied it and found the following method: 1. first in configmain. I recently studied the Yii Framework of php and liked to add a reference to PHPExcel. when I encountered the problem of exporting Excel, I studied it and found the following method:

1. first add a reference to PHPExcel in config \ main. php. my method is as follows:

12345678 // autoloading model and component classes 'import'=>array( 'application.models.*', 'application.components.*', 'application.extensions.phpexcel.*', ),

In addition, some people use the components configuration, but I have some problems, so I will use the above method.

2. follow the following code to modify the Autoloader. php file in the PHP Excel code directory:

12345678910111213141516 public static function Register() { $functions = spl_autoload_functions(); foreach ( $functions as $function) spl_autoload_unregister($function); $functions = array_merge(array(array('PHPExcel_Autoloader','Load')),$functions); foreach ( $functions as $function) $x = spl_autoload_register($function); return $x; } // function Register()

In the above function, the original code is commented out.

3. the following code outputs Excel and some common attribute settings in your controller:

public function actionExport() { $objectPHPExcel = new PHPExcel(); $objectPHPExcel ->setActiveSheetIndex(0); $page_size = 52; // Data retrieval $model = Yii::app()->session[ 'printdata' ]; $dataProvider = $model ->search(); $dataProvider ->setPagination(false); $data = $dataProvider ->getData(); $count = $dataProvider ->getTotalItemCount(); // Calculate the total number of pages $page_count = (int)( $count / $page_size ) +1; $current_page = 0; $n = 0; foreach ( $data as $product ) { if ( $n % $page_size === 0 ) { $current_page = $current_page +1; // Report header output $objectPHPExcel ->getActiveSheet()->mergeCells( 'B1:G1' ); $objectPHPExcel ->getActiveSheet()->setCellValue( 'B1' , 'Product info table' ); $objectPHPExcel ->setActiveSheetIndex(0)->setCellValue( 'B2' , 'Product info table' ); $objectPHPExcel ->setActiveSheetIndex(0)->setCellValue( 'B2' , 'Product info table' ); $objectPHPExcel ->setActiveSheetIndex(0)->getStyle( 'B1' )->getFont()->setSize(24); $objectPHPExcel ->setActiveSheetIndex(0)->getStyle( 'B1' ) ->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objectPHPExcel ->setActiveSheetIndex(0)->setCellValue( 'B2' , 'Date :' . date ( "Y-m-j" )); $objectPHPExcel ->setActiveSheetIndex(0)->setCellValue( 'G2' , 'Die' . $current_page . '/' . $page_count . 'Page' ); $objectPHPExcel ->setActiveSheetIndex(0)->getStyle( 'G2' ) ->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); // Output of the table header $objectPHPExcel ->getActiveSheet()->getColumnDimension( 'A' )->setWidth(5); $objectPHPExcel ->setActiveSheetIndex(0)->setCellValue( 'B3' , 'Number' ); $objectPHPExcel ->getActiveSheet()->getColumnDimension( 'B' )->setWidth(6.5); $objectPHPExcel ->setActiveSheetIndex(0)->setCellValue( 'C3' , 'Name' ); $objectPHPExcel ->getActiveSheet()->getColumnDimension( 'C' )->setWidth(17); $objectPHPExcel ->setActiveSheetIndex(0)->setCellValue( 'D3' , 'Manufacturer' ); $objectPHPExcel ->getActiveSheet()->getColumnDimension( 'D' )->setWidth(22); $objectPHPExcel ->setActiveSheetIndex(0)->setCellValue( 'E3' , 'Unit' ); $objectPHPExcel ->getActiveSheet()->getColumnDimension( 'E' )->setWidth(15); $objectPHPExcel ->setActiveSheetIndex(0)->setCellValue( 'F3' , 'Unit price' ); $objectPHPExcel ->getActiveSheet()->getColumnDimension( 'F' )->setWidth(15); $objectPHPExcel ->setActiveSheetIndex(0)->setCellValue( 'G3' , 'Number of databases' ); $objectPHPExcel ->getActiveSheet()->getColumnDimension( 'G' )->setWidth(15); // Set Center $objectPHPExcel ->getActiveSheet()->getStyle( 'B3:G3' ) ->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); // Set the border $objectPHPExcel ->getActiveSheet()->getStyle( 'B3:G3' ) ->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); $objectPHPExcel ->getActiveSheet()->getStyle( 'B3:G3' ) ->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); $objectPHPExcel ->getActiveSheet()->getStyle( 'B3:G3' ) ->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); $objectPHPExcel ->getActiveSheet()->getStyle( 'B3:G3' ) ->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); $objectPHPExcel ->getActiveSheet()->getStyle( 'B3:G3' ) ->getBorders()->getVertical()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); // Set the color $objectPHPExcel ->getActiveSheet()->getStyle( 'B3:G3' )->getFill() ->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB( 'FF66CCCC' ); } // Detailed output $objectPHPExcel ->getActiveSheet()->setCellValue( 'B' .( $n +4) , $product ->id); $objectPHPExcel ->getActiveSheet()->setCellValue( 'C' .( $n +4) , $product ->product_name); $objectPHPExcel ->getActiveSheet()->setCellValue( 'D' .( $n +4) , $product ->product_agent->name); $objectPHPExcel ->getActiveSheet()->setCellValue( 'E' .( $n +4) , $product ->unit); $objectPHPExcel ->getActiveSheet()->setCellValue( 'F' .( $n +4) , $product ->unit_price); $objectPHPExcel ->getActiveSheet()->setCellValue( 'G' .( $n +4) , $product ->library_count); // Set the border $currentRowNum = $n +4; $objectPHPExcel ->getActiveSheet()->getStyle( 'B' .( $n +4). ':G' . $currentRowNum ) ->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); $objectPHPExcel ->getActiveSheet()->getStyle( 'B' .( $n +4). ':G' . $currentRowNum ) ->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); $objectPHPExcel ->getActiveSheet()->getStyle( 'B' .( $n +4). ':G' . $currentRowNum ) ->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); $objectPHPExcel ->getActiveSheet()->getStyle( 'B' .( $n +4). ':G' . $currentRowNum ) ->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); $objectPHPExcel ->getActiveSheet()->getStyle( 'B' .( $n +4). ':G' . $currentRowNum ) ->getBorders()->getVertical()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); $n = $n +1; } // Set pagination //$objectPHPExcel->getActiveSheet()->setBreak( 'I55' , PHPExcel_Worksheet::BREAK_ROW ); //$objectPHPExcel->getActiveSheet()->setBreak( 'I10' , PHPExcel_Worksheet::BREAK_COLUMN ); $objectPHPE

Example 1: Add a reference to PHPExcel in config \ main. php...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.