5

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.

NHTorres
  • 1,528
  • 2
  • 21
  • 39

1 Answers1

7

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.

Community
  • 1
  • 1
Randika Vishman
  • 7,983
  • 3
  • 57
  • 80
  • 1
    Thank you! this works well for me. As a last question can be done to open the first page of excel? I used to create these two, you will always have a fixed. I would like you to create the excel open the main sheet – NHTorres Nov 24 '15 at 14:59
  • 1
    If my answer worked for you, what else don't you get clarified? :-) I can't understand if you found the solution or not. You can always refer to the first page(first sheet) by following code line, `getSheet($indexOfSheet)` but remember it may give an exception if you gave an index of non existing sheet. I recommend you to visit the following link for further clarification. – Randika Vishman Nov 24 '15 at 15:06
  • Yes! you're right only was my mistake, everything works fine for me with your solution, thank you very much (: – NHTorres Nov 24 '15 at 15:19