Copying Worksheet to another book doesn't work correct.

Topics: Developer Forum
Jun 5, 2015 at 4:05 PM
Edited Jun 5, 2015 at 4:08 PM
Hi there.
When I copied a worksheet to same book, It's has no problem. Though when I copied the worksheet to another book, the copied sheet lost its styles.
The code to reproduce the error are below.

$objPHPExcel = new PHPExcel();

array('fill'    => array(
            'type'      => PHPExcel_Style_Fill::FILL_SOLID,
            'color'     => array('argb' => 'FFCCFFCC')
        'borders' => array(
            'bottom'    => array('style' => PHPExcel_Style_Border::BORDER_THIN),
            'right'     => array('style' => PHPExcel_Style_Border::BORDER_MEDIUM)
array('fill'    => array(
            'type'      => PHPExcel_Style_Fill::FILL_SOLID,
            'color'     => array('argb' => 'FFFFFF00')
//write some text
$objPHPExcel->getActiveSheet()->setCellValue("D6", "ABCDE");
//Set some styles
array('fill'    => array(
            'type'      => PHPExcel_Style_Fill::FILL_SOLID,
            'color'     => array('argb' => 'FF66FF00')
        'font'    => array(
            'bold'      => true,
            'size' => 16,
        'alignment' => array(
            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
            'vertical' => PHPExcel_Style_Alignment::VERTICAL_TOP,
        'borders' => array(
            'top'     => array('style' => PHPExcel_Style_Border::BORDER_MEDIUMDASHDOTDOT),
            'bottom'  => array('style' => PHPExcel_Style_Border::BORDER_SLANTDASHDOT)
//Make copy
$objClone = clone $objPHPExcel->getActiveSheet();
//Paste to same book

//Paste to another boook
$objNewExcel = new PHPExcel;

// Save both
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

$objNewWriter = PHPExcel_IOFactory::createWriter($objNewExcel, 'Excel2007');

PHPExcel Ver.1.8.0
PHP Ver.5.3.3

Any solution? Thanks!
Jun 5, 2015 at 4:25 PM
Copying a worksheet from one PHPExcel object to another requires a lot more work than simply copying the a worksheet within the same PHPExcel object, precisely because it does need to copy all the styles as well

It works if you use the correct method:
Jun 5, 2015 at 5:37 PM
Thank you for quick reply.
It worked correctly by using 'addExternalSheet'.

But when I commented out as

'Sheet does not exist' exception is occured.

I only need to move the sheet. I don't want to copy it to original book really.

Why the exception is occur?