How Yii uses Phpexcel to import Excel files

Source: Internet
Author: User
Tags autoload autoloader spl vars yii

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
    1. ' Import ' = =Array (
    2. ' Application.extensions.PHPExcel.PHPExcel ',
    3. ),

(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
    1. Phpexcel_autoloader::register ();
    2. Phpexcel_shared_zipstreamwrapper::register ();

Revision changed to [PHP]View Plaincopy
    1. 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
  1. Public static function Register () {
  2. /* 
  3. if (function_exists (' __autoload ')) {
  4. Register any existing autoloader function with SPL, so we don ' t get any clashes
  5. Spl_autoload_register (' __autoload ');
  6. }
  7. Register ourselves with SPL
  8. return Spl_autoload_register (Array (' Phpexcel_autoloader ', ' Load '));
  9. */
  10. $functions = Spl_autoload_functions ();
  11. foreach ( $functions as $function)
  12. Spl_autoload_unregister ($function);
  13. $functions = array_merge (Array (' phpexcel_autoloader ',' Load '),$functions);
  14. foreach ( $functions as $function)
  15. $x = Spl_autoload_register ($function);
  16. return $x;
  17. } //function Register ()

3.3. Use the following code when you need to use Phpexcel [PHP]View Plaincopy
  1. $filePath = '/home/public_html/sqt/protected/data/queuesql/company.xls ';
  2. Spl_autoload_unregister (Array (' yiibase ', ' autoload '));
  3. $phpExcelPath = Yii::getpathofalias (' application.extensions.PHPExcel.PHPExcel ');
  4. Include ($phpExcelPath. Directory_separator.  ' iofactory.php ');
  5. Spl_autoload_register (Array (' yiibase ', ' autoload '));
  6. $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
    1. Yii::$enableIncludePath = false;
    2. Yii::import (' Application.extensions.PHPExcel.PHPExcel ', 1);
-------------------------------------------------------------------------------
Import Excel File method [PHP]View Plaincopy
  1. Public function Actionload () {
  2. if (isset ($_post[' Submit ')) {
  3. $file = cuploadedfile::getinstancebyname (' file '); Get an uploaded file instance
  4. if ($file-getType () = = ' application/vnd.ms-excel ') {
  5. $excelFile = $file->gettempname (); Get file name
  6. //Here is the import Phpexcel package, to use when you add such two sentences, convenient bar
  7. Yii::$enableIncludePath = false;
  8. Yii::import (' Application.extensions.PHPExcel.PHPExcel ', 1);
  9. $phpexcel = new Phpexcel;
  10. $excelReader = Phpexcel_iofactory::createreader (' Excel5 ');
  11. $phpexcel = $excelReader->load ($excelFile)->getsheet (0); Load a file and get the first sheet
  12. $total _line = $phpexcel->gethighestrow ();
  13. $total _column = $phpexcel->gethighestcolumn ();
  14. For ($row = 2; $row <= $total _line; $row + +) {  
  15. $data = Array ();
  16. For ($column = ' A '; $column <= $total _column; $column + +) {  
  17. $data [] = Trim ($phpexcel->getcell ($column.   $row), GetValue ());
  18. }
  19. }
  20. }
  21. }
This article transferred from: http://blog.csdn.net/tinico/article/details/18033575

How Yii uses Phpexcel to import Excel files

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.