Read from one sheet and write to other

Topics: Developer Forum, User Forum
Oct 28, 2010 at 11:12 AM
Edited Oct 30, 2010 at 6:57 AM

Hi all,

     Is it possible in PHP Excel to read from a separate sheet and write to another.Let me make things clear.I want to read from a sheet which contain only one sheet and I want to write it in to another sheet which have multiple sheets.

Is this possible???

If yes, can you plz. illustrate with an example.

 

Thanks in advance,

MaK

Coordinator
Nov 1, 2010 at 11:48 PM
Edited Nov 1, 2010 at 11:51 PM

Perfectly possible: but how do you decide which worksheet in the book with multiple sheets you want to write to?

$firstBook = PHPExcel_IOFactory::load("workbook1.xls");
$secondBook = PHPExcel_IOFactory::load("workbook2.xls");
$cellValue = $firstBook->getActiveSheet()->getCell('A1')->getValue();
$secondBook->getSheet(0)->setCellValue('A1',$cellValue);
$cellValue = $firstBook->getActiveSheet()->getCell('A2')->getValue();
$secondBook->getSheet(1)->setCellValue('A1',$cellValue);
$cellValue = $firstBook->getActiveSheet()->getCell('A3')->getValue();
$secondBook->getSheetByName('Worksheet1')->setCellValue('B2',$cellValue);
$cellValue = $firstBook->getActiveSheet()->getCell('A4')->getValue();
$secondBook->getSheetByName('Worksheet2')->setCellValue('B2',$cellValue);