Create a New PHPExcel Worksheet

I am trying to create another worksheet and everything is working fine. But now I need to create 1 depending on the variable. For instance:

I have two options for checking and one for results. Everything is driven by a boolean called $resultado .

I have a 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'); } 

If this-> validations is the second worksheet. Now I need this worksheet to have a different name, and therefore I need other data encapsulated in the function. Therefore, my function generates the desired condition as follows:

 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 generate 1 sheet with a title depending on the variable, this is not what I want.

+5
source share
1 answer

Create a New PHPExcel Worksheet

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

Answer:
Just try the following for 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'); } } 

See the following SO Q & Thread for more information .

+6
source

Source: https://habr.com/ru/post/1236716/


All Articles