Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setActiveSheetIndex not working properly

Tags:

php

phpexcel

I'm using PHPExcel to make export xls with 2 worksheets. When I open the generated xls file, it opens directly to the second worksheet, even though I've declared in the code that generates the first worksheet:

$objPHPExcel->setActiveSheetIndex(0) 

This happened after I'd added the following lines in the code that generates the second worksheet.

$objPHPExcel->getSheet(1)->getStyle('A1:E1')->getFont()->setBold(true);

    $objPHPExcel->getSheet(1)->getColumnDimension('A')->setWidth(15);
    $objPHPExcel->getSheet(1)->getRowDimension(1)->setRowHeight(40);
    $objPHPExcel->getSheet(1)->getStyle('A:F')->getAlignment()->setWrapText(true); 
    $objPHPExcel->getSheet(1)->getColumnDimension('B')->setWidth(40);
    $objPHPExcel->getSheet(1)->getStyle('B')->getAlignment()->setWrapText(true); 
    $objPHPExcel->getSheet(1)->getColumnDimension('C')->setWidth(15);
    $objPHPExcel->getSheet(1)->getColumnDimension('D')->setWidth(15);
    $objPHPExcel->getSheet(1)->getColumnDimension('E')->setWidth(15);
    $objPHPExcel->getSheet(1)->getColumnDimension('E')->setAutoSize(true);

    $objPHPExcel->getSheet(1)->getPageMargins(1)->setTop(1);
    $objPHPExcel->getSheet(1)->getPageMargins(1)->setRight(0.75);
    $objPHPExcel->getSheet(1)->getPageMargins(1)->setLeft(0.75);
    $objPHPExcel->getSheet(1)->getPageMargins(1)->setBottom(1);

    $objPHPExcel->getSheet(1)->getStyle('A1:E1')->getFill()
                    ->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
                    ->getStartColor()->setRGB('4EE900');

If I comment some of the code lines, (as shown in the picture) the active sheet index works fine.

enter image description here

So, if I remove these lines, it works fine. Any Idea how to fix this to keep first worksheet as the active worksheet?

like image 930
Miril Terolli Avatar asked Oct 15 '25 03:10

Miril Terolli


1 Answers

Creating a new worksheet automatically sets that as the active sheet.

Unless you use $objPHPExcel->setActiveSheetIndex(0); after creating your second worksheet, then the second sheet will be your active sheet.

And you can always set the active sheet manually just before your save using

$objPHPExcel->setActiveSheetIndex(0);
like image 192
Mark Baker Avatar answered Oct 18 '25 03:10

Mark Baker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!