Home >Backend Development >PHP Tutorial >Using PHPExcel to import and export in YII - CSDN Blog
1. Unzip phpexcel into the protected/vendor directory. The directory structure is vendor/PHPExcel/PHPExcel.php
2. Modify the index.php file
require_once($yii); $app=Yii::createWebApplication($config);//->run(); // adding PHPExcel autoloader Yii::import('application.vendor.*'); require_once "PHPExcel/PHPExcel.php"; require_once "PHPExcel/PHPExcel/Autoloader.php"; Yii::registerAutoloader(array('PHPExcel_Autoloader','Load'), true); $app->run();
3. Export the Excel file
public function actionExcel(){ $objPHPExcel = new PHPExcel(); $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', 'Hello') ->setCellValue('B2', 'world!') ->setCellValue('C1', 'Hello') ->setCellValue('D2', 'world!'); $objPHPExcel->getActiveSheet()->setTitle('Simple'); $objPHPExcel->setActiveSheetIndex(0); ob_end_clean(); ob_start(); header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="test.xls"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save('php://output'); }
4. Import the Excel file of.
$file = CUploadedFile::getInstance($model,'brand_model_file'); var_dump($file->getType()); if(in_array($file->getType(),array('application/vnd.ms-excel','application/excel','application/msexcel','application/kset')) ) { $excelFile = $file->getTempName(); //$phpexcel = new PHPExcel; $excelReader = PHPExcel_IOFactory::createReader('Excel5'); $phpexcel = $excelReader->load($excelFile)->getSheet(0); $total_line = $phpexcel->getHighestRow(); $total_column = $phpexcel->getHighestColumn(); $allData = array(); for ($row = 1; $row <= $total_line; $row++) { $data = array(); for ($column = 'A'; $column <= $total_column; $column++) { $data[] = trim($phpexcel->getCell($column.$row) -> getValue()); } array_push($allData, $data); } print_r($allData);
The above is the entire content of this article, thank you for reading.
Related recommendations:
PHP uses PHPExcel to implement batch upload to the database
The above is the detailed content of Using PHPExcel to import and export in YII - CSDN Blog. For more information, please follow other related articles on the PHP Chinese website!