Two identical sheet

Topics: User Forum
Apr 14, 2010 at 7:26 AM

Hello;

I am trying to update my tutorial on PHPExcel update

Howto French

I am facing a problem I would say pointer, in fact, in reading an Excel file if I want to create two identical sheet, I have to read twice the same file.

If I do this it works correctly

 

$objet1 = new PHPExcel_Reader_Excel5();

$excel1 = $objet1->load('Exemple14.xls');

$objet2 = new PHPExcel_Reader_Excel5();

$excel2 = $objet2->load('Exemple14.xls');

$writer = new PHPExcel();
$writer->addSheet($excel1->getSheet();

$writer->addSheet($excel2->getSheet();

$sheet2 = $writer->getSheet(2);
$sheet2->setCellValue('A50', 'MaitrePylos');

$save = new PHPExcel_Writer_Excel5($writer);
$save->save('Exemple_gg.xls');
By cons, if I did like this, the two sheet are changed

 

$objet1 = new PHPExcel_Reader_Excel5();

$excel1 = $objet1->load('Exemple14.xls');

$sheet = $excel1->getSheet();

$writer = new PHPExcel();


$writer->addSheet($sheet);
$writer->addSheet($sheet);

$sheet2 = $writer->getSheet(2);
$sheet2->setCellValue('A50', 'MaitrePylos');


$save = new PHPExcel_Writer_Excel5($writer);
$save->save('Exemple_gg.xls');
In fact I modify the same reference, how do the two new sheet is disinct.

thank you for your help.

MaitrePylos

Coordinator
Apr 14, 2010 at 10:45 AM

You can try using either the worksheet copy method or PHP clone for the sheet.

$objet1 = new PHPExcel_Reader_Excel5();
$excel1 = $objet1->load('bugTest.xls');
$sheet1 = $excel1->getSheet();
$sheet2 = $sheet1->copy();
$sheet2->setTitle('Cloned Worksheet');
$writer = new PHPExcel();
$writer->addSheet($sheet1);
$writer->addSheet($sheet2);
$sheet2 = $writer->getSheet(2);
$sheet2->setCellValue('A2', 2);
$save = new PHPExcel_Writer_Excel5($writer);
$save->save('bugResult.xls');

or

$objet1 = new PHPExcel_Reader_Excel5();
$excel1 = $objet1->load('bugTest.xls');
$sheet1 = $excel1->getSheet();
$sheet2 = clone $sheet1;
$sheet2->setTitle('Cloned Worksheet');
$writer = new PHPExcel();
$writer->addSheet($sheet1);
$writer->addSheet($sheet2);
$sheet2 = $writer->getSheet(2);
$sheet2->setCellValue('A2', 2);
$save = new PHPExcel_Writer_Excel5($writer);
$save->save('bugResult.xls');

 

Apr 14, 2010 at 10:56 AM

THX :)