Search code examples
phpmysqlphpexcelspreadsheet

An extra worksheet present when creating multiple sheets using phpexcel


I am creating an excel file with different sheets. The different sheets are created by iteration. But my iteration results in an extra sheet named worksheet. My code is:

        $result = fetch results from database;
        $count = COUNT($result);
        foreach ( $result as $key=>$value){ 
            $objPHPExcel->createSheet($key);   
            $objPHPExcel->getActiveSheet()
                   ->setTitle($value['title']);  
        }

My database has got 3 results and it generates three worksheets along with a fourth one which is named as 'Worksheet'.

If I am using a checking condition with

if ($key > 0) {
     execute above code
}
else {
     $objPHPExcel->setActiveSheetIndex(0)->setTitle($value['title']);
}

it works fine. why is it so? Where is the mistake?


Solution

  • There is nothing wrong with your code. When you instantiate a new PHPExcel object using $objPHPExcel = new PHPExcel(), it is created with a single sheet called "worksheet"; delete that if you want to create only your own sheets

    $objPHPExcel->removeSheetByIndex(0);