How to set plain sheet title in phpexcel

23,271

Seems like you are executing your posted code twice. This creates two sets of identically-named worksheets (Week 1, Week 2, Week 1, Week 2) which Excel does not like. It automatically adds "1" to repeating titles.

It's not your code that adds 1, it's Excel.

So you need to either maintain global week iterator, so it becomes Week 1, Week 2, Week 3, Week 4:

// you may need to do global $week if this code runs in a function
if ( ! isset( $week ) )
  $week = 0;
for ($w=1; $w<3; $w++) :
  // ...
  $week += $w;
  $objPHPExcel->getActiveSheet()->setTitle("Week ".$week);
  // ...
endfor;

Or you need to implement some other sheet naming scheme for yourself.

Share:
23,271
RK.
Author by

RK.

Programmer.

Updated on October 31, 2020

Comments

  • RK.
    RK. over 3 years

    I am trying to print excel sheet with multiple sheets inside. Everything is working fine except the sheet title. I have set the sheet title and it is coming accordingly. However, with the given sheet title the extra number is also coming which creates confusion. I believe, this is the sheet index number or something but unable to remove it. Here is my code :

    $y=0;
    for ($w=1; $w<3; $w++) :
                $sheetId =$y;
                $objPHPExcel->createSheet(NULL, $sheetId);
                $objPHPExcel->setActiveSheetIndex($sheetId);
                $objPHPExcel->getActiveSheet()->setTitle("Week ".$w);
                $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(12);
                $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(12);
                $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(14);
                $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
                $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(16);
                $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(12);
                $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(10);
                $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(15);
                $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(10);
                $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(17.5);
    
                $objPHPExcel->setActiveSheetIndex($sheetId)->mergeCells('A1:J6');
                $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setWrapText(true);
                $objPHPExcel->getActiveSheet()->getStyle("A1")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                $objPHPExcel->getActiveSheet()->getStyle('A1:J5')->applyFromArray($styleArray);
                $objPHPExcel->getActiveSheet()->getStyle('A:E')->applyFromArray($centreAlign);
    endfor;
    

    Output:

    The first week 1 and week 2 is correct but in others extra number is coming which is undesirable.

    Any help will be highly appreciated.