1.将phpexcel解压到protected/vendor目录当中。目录结构为vendor/PHPExcel/PHPExcel.php
2.修改index.php文件
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.导出Excel文件
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.导入Excel文件的。
$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);
以上就是本篇文章的全部内容了,感谢大家阅读。
相关推荐:
如何解决IE浏览器使用PHPExcel导出文件时文件名中文乱码的问题
以上是YII中使用PHPExcel导入导出 - CSDN博客的详细内容。更多信息请关注PHP中文网其他相关文章!