Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create new worksheet PHPExcel

Tags:

php

excel

cakephp

I'm trying to create another worksheet and everything works fine. But what I need now is to create 1 depending on a variable. For instance :

I have two options one for validation and one for results. Everything is conditioned by a boolean variable called $resultado.

I have my component in CakePHP

function ExcelCargaMasivaComponent() {
    $this->xls = new PHPExcel();
    $this->sheet = $this->xls->getActiveSheet();
    $this->sheet->setTitle("Worksheet");
    $this->sheet->getDefaultStyle()->getFont()->setName('Verdana');
    $this->xls->createSheet();
    $this->xls->setActiveSheetIndex(1);
    $this->validations = $this->xls->getActiveSheet();
    $this->validations->setTitle('Validations');
}

Where this-> validations is the second worksheet. Now, I need this worksheet has a different name, and therefore I want other data encapsulated in a function. So my function generate wanted condition this way:

function ExcelCargaMasivaComponent() {
    $this->xls = new PHPExcel();
    $this->sheet = $this->xls->getActiveSheet();
    $this->sheet->setTitle("Worksheet");
    $this->sheet->getDefaultStyle()->getFont()->setName('Verdana');
    $this->xls->createSheet();
    $this->xls->setActiveSheetIndex(1);
}

function generate($title = 'Report', $headers = array(), $data = array(), $uid = false, $resultados = false){
    if($resultados){
       $this->validations = $this->xls->getActiveSheet();
       $this->validations->setTitle('Resultados');
    }else{
       $this->validations = $this->xls->getActiveSheet();
       $this->validations->setTitle('Validations');
    }
}

I do this so that the second sheet has a different name and different data depending on the variable, but I could not get it to work. I only generates 1 sheet with the title depending on the variable, it's not what I want.

like image 719
NHTorres Avatar asked Nov 24 '15 14:11

NHTorres


1 Answers

Create new worksheet PHPExcel

Hi I really don't know if my answer may really do the magic to your question. However it seems fascinating to me.

Answer:
Just try doing the following to your generate method as I have provided the following code snippet:

function ExcelCargaMasivaComponent() {
    $this->xls = new PHPExcel();
    $this->sheet = $this->xls->getActiveSheet();
    $this->sheet->setTitle("Worksheet");
    $this->sheet->getDefaultStyle()->getFont()->setName('Verdana');
//  $this->xls->createSheet(); // comment out this lines as we keep
//  $this->xls->setActiveSheetIndex(1);  // them in our generate method
}

function generate($title = 'Report', $headers = array(), $data = array(), $uid = false, $resultados = false) {
    if ($resultados) {
       $this->xls->createSheet(0);
       $this->xls->setActiveSheetIndex(0); // This is the first required line
       $this->validations = $this->xls->getActiveSheet();
       $this->validations->setTitle('Resultados');
    } else {
       $this->xls->createSheet(1);           
       $this->xls->setActiveSheetIndex(1); // This is the second required line
       $this->validations = $this->xls->getActiveSheet();
       $this->validations->setTitle('Validations');
    }
}

For your further reference please see the following SO Q&A Thread too.

like image 67
Randika Vishman Avatar answered Sep 26 '22 21:09

Randika Vishman