0

I create some worksheets in PHPExcel:

$objWorkSheet = $objPHPExcel->createSheet("A");
$objPHPExcel->addSheet($objWorkSheet);
$objWorkSheet = $objPHPExcel->createSheet("B");
$objPHPExcel->addSheet($objWorkSheet);
$objWorkSheet = $objPHPExcel->createSheet("C");
$objPHPExcel->addSheet($objWorkSheet);

Is it possible to change the order of the worksheets afterwards?

2
  • as per on PHPExcel documentation, they don't have any method to do this. Jul 3, 2016 at 21:00
  • should be doable by manipulating the object
    – user557846
    Jul 3, 2016 at 21:01

1 Answer 1

1

I created this function that moves a tab to the last position of the spreadsheet. Parameter: Name of the tab to be moved. If the tab does not exist, it does nothing. It was added to PHPExcel.php after the removeSheetByIndex function (line 255). I hope it helps.

/**
* Move sheet by name to the end
*
* @param string $pName Sheet name
* if sheet $pName not found, do nothing!!
*/
public function moveSheetByName($pName = '')
{
    if (($worksheet = $this->getSheetByName($pName)) instanceof PHPExcel_Worksheet) {
        $pIndex = $this->getIndex($this->getSheetByName($pName));
        $pSheettoMove = array_splice($this->_workSheetCollection, $pIndex, 1);
        $this->addSheet($pSheettoMove[0]);              
    }
}   

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.