1. Download Phpexcel, copy the classes in the compressed package to protected/extensions and modify it to phpexcel.
2. Modify YII configuration file config/main.php
[PHP]View Plaincopy
- ' Import ' = =Array (
- ' Application.extensions.PHPExcel.PHPExcel ',
- ),
(The following methods to deal with Phpexcel AutoLoad and yii autoload conflict of choice, recommended 4th, most consistent with the YII standard)
3.1, modify the Phpexcel in the autoloader.php
[PHP]View Plaincopy
- Phpexcel_autoloader::register ();
- Phpexcel_shared_zipstreamwrapper::register ();
Revision changed to
[PHP]View Plaincopy
- Yii::registerautoloader (Array (' Phpexcel_autoloader ',' Register '), true);
3.2. Modify the autoloader.php file in the Phpexcel code directory according to the following code
[PHP]View Plaincopy
- Public static function Register () {
- /*
- if (function_exists (' __autoload ')) {
- Register any existing autoloader function with SPL, so we don ' t get any clashes
- Spl_autoload_register (' __autoload ');
- }
- Register ourselves with SPL
- return Spl_autoload_register (Array (' Phpexcel_autoloader ', ' Load '));
- */
- $functions = Spl_autoload_functions ();
- foreach ( $functions as $function)
- Spl_autoload_unregister ($function);
- $functions = array_merge (Array (' phpexcel_autoloader ',' Load '),$functions);
- foreach ( $functions as $function)
- $x = Spl_autoload_register ($function);
- return $x;
- } //function Register ()
3.3. Use the following code when you need to use Phpexcel
[PHP]View Plaincopy
- $filePath = '/home/public_html/sqt/protected/data/queuesql/company.xls ';
- Spl_autoload_unregister (Array (' yiibase ', ' autoload '));
- $phpExcelPath = Yii::getpathofalias (' application.extensions.PHPExcel.PHPExcel ');
- Include ($phpExcelPath. Directory_separator. ' iofactory.php ');
- Spl_autoload_register (Array (' yiibase ', ' autoload '));
- $PHPExcel = Phpexcel_iofactory::load ( $filePath);
3.4, as long as the set Yii:: $enableIncludePath to False, the third-party class library has the opportunity to execute its own AutoLoad method, and do not need to configure config/main.php, very convenient and flexible
[PHP]View Plaincopy
- Yii::$enableIncludePath = false;
- Yii::import (' Application.extensions.PHPExcel.PHPExcel ', 1);
-------------------------------------------------------------------------------
Import Excel File method
[PHP]View Plaincopy
- Public function Actionload () {
- if (isset ($_post[' Submit ')) {
- $file = cuploadedfile::getinstancebyname (' file '); Get an uploaded file instance
- if ($file-getType () = = ' application/vnd.ms-excel ') {
- $excelFile = $file->gettempname (); Get file name
- //Here is the import Phpexcel package, to use when you add such two sentences, convenient bar
- Yii::$enableIncludePath = false;
- Yii::import (' Application.extensions.PHPExcel.PHPExcel ', 1);
- $phpexcel = new Phpexcel;
- $excelReader = Phpexcel_iofactory::createreader (' Excel5 ');
- $phpexcel = $excelReader->load ($excelFile)->getsheet (0); Load a file and get the first sheet
- $total _line = $phpexcel->gethighestrow ();
- $total _column = $phpexcel->gethighestcolumn ();
- For ($row = 2; $row <= $total _line; $row + +) {
- $data = Array ();
- For ($column = ' A '; $column <= $total _column; $column + +) {
- $data [] = Trim ($phpexcel->getcell ($column. $row), GetValue ());
- }
- }
- }
- }
This article transferred from: http://blog.csdn.net/tinico/article/details/18033575
How Yii uses Phpexcel to import Excel files