Commits

yybalam committed a32f081

Initial Commit of YiiExcel extension by K'iin Balam.

Universidad Pedagógica Nacional Fracisco Morazán
Dirección de Tecnologías de Información

  • Participants

Comments (0)

Files changed (5)

+syntax: glob
+*.project
+# YiiExcel
+
+Extensión para Yii framework que envuelve la autocarga de clases de [PHPExcel](https://github.com/PHPOffice/PHPExcel), perminiendo un uso transparente dentro de aplicaciones Yii.
+
+## Installación
+
+1. Copie el directorio yiiexcel dentro de protected/extensions.
+2. Descargue [PHPExcel](http://phpexcel.codeplex.com/releases/view/96183).
+3. Cree un diectorio dentro de protected/vendors y llámelo phpexcel.
+4. Decomprima PHPExcel y copie el contenido del directorio Classes destro de del directorio phpexcel recién creado.
+5. Abra el archivo PHPExcel.php y comente la inlución del autocargador:
+
+<pre>
+/** PHPExcel root directory */
+/*if (!defined('PHPEXCEL_ROOT')) {
+	define('PHPEXCEL_ROOT', dirname(__FILE__) . '/');
+	require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
+}*/
+</pre>
+
+6. Edite el archivo index.php y registre el autocargador de YiiExcel:
+
+<pre>
+require_once($yii);
+//Ne ejecutar la aplicación antes de registrar el autocargador
+$app = Yii::createWebApplication($config);
+
+Yii::import('ext.yiiexcel.YiiExcel', true);
+Yii::registerAutoloader(array('YiiExcel', 'autoload'), true);
+
+// Optional:
+//	As we always try to run the autoloader before anything else, we can use it to do a few
+//		simple checks and initialisations
+PHPExcel_Shared_ZipStreamWrapper::register();
+
+if (ini_get('mbstring.func_overload') & 2) {
+    throw new Exception('Multibyte function overloading in PHP must be disabled for string functions (2).');
+}
+PHPExcel_Shared_String::buildCharacterSets();
+
+//Ahora si se puede correr la aplicación
+$app->run();
+</pre>
+
+## Uso
+
+Simplemente crea una instancia de PHPExcel
+
+<pre>
+$objPHPExcel = new PHPExcel();
+</pre>
+
+Consulta el archivo SiteController.php de ejemplo que está en el directorio example.
+# YiiExcel
+
+Yii extension wrapper for [PHPExcel](https://github.com/PHPOffice/PHPExcel) class autoload on Yii applications.
+
+## Installation
+
+1. Copy yiiexcel directory to protected/extensions.
+2. Download [PHPExcel](http://phpexcel.codeplex.com/releases/view/96183).
+3. Create a phpexcel directory on protected/vendors.
+4. Unzip PHPExcel and copy Classes directory content to protected/extensions/phpexcel.
+5. Edit PHPExcel.php file and comment the autoloadei inclusion:
+
+<pre>
+/** PHPExcel root directory */
+/*if (!defined('PHPEXCEL_ROOT')) {
+	define('PHPEXCEL_ROOT', dirname(__FILE__) . '/');
+	require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
+}*/
+</pre>
+
+6. Edit index.php file and register the YiiExcel autoloader:
+
+<pre>
+require_once($yii);
+//do not run app before register YiiExcel autoload
+$app = Yii::createWebApplication($config);
+
+Yii::import('ext.yiiexcel.YiiExcel', true);
+Yii::registerAutoloader(array('YiiExcel', 'autoload'), true);
+
+// Optional:
+//	As we always try to run the autoloader before anything else, we can use it to do a few
+//		simple checks and initialisations
+PHPExcel_Shared_ZipStreamWrapper::register();
+
+if (ini_get('mbstring.func_overload') & 2) {
+    throw new Exception('Multibyte function overloading in PHP must be disabled for string functions (2).');
+}
+PHPExcel_Shared_String::buildCharacterSets();
+
+//Now you can run application
+$app->run();
+</pre>
+
+## Usage
+
+Just create a PHPExcel instance:
+
+<pre>
+$objPHPExcel = new PHPExcel();
+</pre>
+
+Read the SiteController.php example file located inside example directory.
+<?php
+/**
+ * 
+ * Universidad Pedagógica Nacional Fracisco Morazán
+ * Dirección de Tecnologías de Información
+ * 
+ * @author K'iin Balam <kbalam@upnfm.edu.hn>
+ * 
+ */
+
+/**
+ * YiiExcel class - wrapper for PHPExcel
+ * 
+ * This class provide a wrapper for PHPExcel Autoload. 
+ * Please read the README file
+ * 
+ * This extension is an autoloader for PHPExcel on Yii framework 
+ * 
+ * @package YiiExcel
+ * @author K'iin Balam <kbalam@upnfm.edu.hn>
+ * @copyright Copyright (c) 2013 UPNFM
+ * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
+ * @version 1.0, 2013-01-18
+ */
+ 
+class YiiExcel {
+    
+    public static $_pathAlias = 'application.vendors.phpexcel';
+    
+    static function autoload($pClassName){
+        if ((class_exists($pClassName, false)) || (strpos($pClassName, 'PHPExcel') !== 0)) {
+            //  Either already loaded, or not a PHPExcel class request
+            return false;
+        }
+
+        //get the path
+        //$pClassFilePath = Yii::getPathOfAlias('application.vendors.phpexcel').'/'
+        $pClassFilePath = Yii::getPathOfAlias(self::$_pathAlias).'/'
+            .str_replace('_', DIRECTORY_SEPARATOR, $pClassName).'.php';
+
+        if ((file_exists($pClassFilePath) === false) || (is_readable($pClassFilePath) === false)) {
+            //  Can't load
+            return false;
+        }
+
+        require($pClassFilePath);
+    }//loadClass end
+}//EPHPExcelAutoloader end

example/SiteController.php

+<?php
+
+class SiteController extends Controller{
+
+    public function actionExcel(){
+        $objPHPExcel = new PHPExcel();
+
+        // Set document properties
+        $objPHPExcel->getProperties()->setCreator("K'iin Balam")
+             ->setLastModifiedBy("K'iin Balam")
+             ->setTitle("YiiExcel Test Document")
+             ->setSubject("YiiExcel Test Document")
+             ->setDescription("Test document for YiiExcel, generated using PHP classes.")
+             ->setKeywords("office PHPExcel php YiiExcel UPNFM")
+             ->setCategory("Test result file");        
+        
+        // Add some data
+        $objPHPExcel->setActiveSheetIndex(0)
+            ->setCellValue('A1', 'Hello')
+            ->setCellValue('B2', 'world!')
+            ->setCellValue('C1', 'Hello')
+            ->setCellValue('D2', 'world!');
+        
+        // Miscellaneous glyphs, UTF-8
+        $objPHPExcel->setActiveSheetIndex(0)
+            ->setCellValue('A4', 'Miscellaneous glyphs')
+            ->setCellValue('A5', 'éàèùâêîôûëïüÿäöüç');
+        
+        // Rename worksheet
+        $objPHPExcel->getActiveSheet()->setTitle('YiiExcel');
+        
+        // Set active sheet index to the first sheet, so Excel opens this as the first sheet
+        $objPHPExcel->setActiveSheetIndex(0);
+        
+        // Save a xls file
+        $filename = 'YiiExcel';
+        header('Content-Type: application/vnd.ms-excel');
+        header('Content-Disposition: attachment;filename="'.$filename.'.xls"');
+        header('Cache-Control: max-age=0');
+        
+        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
+
+        $objWriter->save('php://output');
+        unset($this->objWriter);
+        unset($this->objWorksheet);
+        unset($this->objReader);
+        unset($this->objPHPExcel);
+        exit();
+    }//fin del método actionExcel
+}