Beispiel #1
0
 /** 
  * Uploads the xls file on system.
  * The browser displays the result of this operation.
  */
 public function actionUpload()
 {
     if (!Yii::app()->user->checkAccess('loadExcel')) {
         throw new CHttpException(403, Yii::t('http_status', '403'));
     }
     $upload = false;
     $model = new FileUpload();
     if (isset($_POST['FileUpload'])) {
         $model->attributes = $_POST['FileUpload'];
         if ($model->validate()) {
             $upload = true;
             $model->uploaded_file = CUploadedFile::getInstance($model, 'uploaded_file');
             $model->uploaded_file->saveAs(Yii::app()->basePath . '/../excel_uploads/' . $model->uploaded_file);
             $this->render('upload', array('model' => $model, 'upload' => $upload, 'filename' => $model->uploaded_file));
             Yii::app()->end();
         }
     }
     $this->render('upload', array('model' => $model, 'upload' => $upload));
 }
Beispiel #2
0
 public function actionFileupload()
 {
     $model = new FileUpload();
     // uncomment the following code to enable ajax-based validation
     /*
     if(isset($_POST['ajax']) && $_POST['ajax']==='file-upload-file_upload-form')
     {
     		echo CActiveForm::validate($model);
     		Yii::app()->end();
     }
     */
     $rows = array();
     $columns = array();
     $slides = array();
     if (isset($_POST['FileUpload'])) {
         $model->attributes = $_POST['FileUpload'];
         if ($model->validate()) {
             //  $slides=$this->pptx_to_text(Yii::app()->baseUrl."/files/samplePowerPoint.pptx");
             $file = CUploadedFile::getInstance($model, 'file');
             //$ext="pptx";
             $ext = pathinfo($file->getName(), PATHINFO_EXTENSION);
             if ($ext == "pptx") {
                 $slides = $this->pptx_to_text($file->getTempName());
                 //$slides=$this->pptx_to_text("files/samplePowerPoint.pptx");
             } else {
                 if ($ext == "xls" || $ext == "xlsx") {
                     Yii::import('ext.phpexcelreader.JPhpExcelReader');
                     try {
                         $data = new JPhpExcelReader($file->getTempName());
                     } catch (Exception $e) {
                         $model->addError("file", $e->getMessage());
                         //echo "Error:".$e->getMessage();
                     }
                     $i = 0;
                     /* 
                     							  echo "Data:<pre>";
                     print_r($data);
                     echo "</pre>";
                     */
                     //exit;
                     foreach ($data->sheets as $k1 => $s) {
                         /*
                         echo "Cells:<pre>";
                         							print_r($s);
                         echo "</pre>";
                         */
                         if (isset($s['cells'])) {
                             for ($j = 0; $j < count($s['cells']); $j++) {
                                 if ($j == 0) {
                                     $columns = $s['cells'][$j + 1];
                                 } else {
                                     for ($k = 0; $k < count($columns); $k++) {
                                         $rows[$k1][$i][$columns[$k + 1]] = $s['cells'][$j + 1][$k + 1];
                                         /*                                    
                                         												$rows[$k1][$i][$columns[$k+1]]=array(
                                         																      'value'=>$s['cells'][$j+1][$k+1],
                                         																      'style'=>''
                                         																    );  
                                         */
                                     }
                                     $i++;
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     $this->render('file_upload', array('model' => $model, 'sheets' => $rows, 'columns' => $columns, 'slides' => $slides));
 }