phpexcel - Reading from one excel sheet, writing to another

Hello!

I was wondering if anyone knew how to extract data from one excel sheet to be able to write it to a separate excel sheet.

For example; taking the value of the cell C4 in the excel Spreadsheet “FirstSheet.xls”. And to write it to a new excel sheet, “NewSheet.xls” in the cell “D2”.

Opps, i realized that i posted this in the wrong section.

I’m not sure that this specifically suits your needs, but it looks as though it might.

awl: That’s the library. They do not have an online doc, so I was forced to fully download it.

Accessing cells is easy.

[php]$objPHPExcel->getActiveSheet()->getCell(‘B1’)->getValue();[/php]

Setting cells is equally easy:

[php]$objPHPExcel->getActiveSheet()->setCellValue(‘B1’, ‘My valuevalue’); [/php]

What are you struggling with?

I’m struggling with putting:

[php]$objPHPExcel->getActiveSheet()->getCell(‘B1’)->getValue();[/php]

Directly into a completely different excel sheet
To something like this:

[php]$objPHPExcel2->getActiveSheet()->setCellValue(‘B1’, ‘GetCell(‘b1’)’); [/php]

^ So that I essentially take the value from one excel sheet to be put in a completely different excel sheet.

Like my example taking the cell value of “b1” from the excel sheet “FirstSheet.xls”
To put the cell “b1” (Or any other cell) of the excel sheet “SecondSheet.xls”

Not just moving the data around in the same excel sheet

[php]$objPHPExcel2->getActiveSheet()->setCellValue(
‘B1’,
$objPHPExcel->getActiveSheet()->GetCell(‘b1’)->getValue()); [/php]

Simple as that :slight_smile:

Alright, So;

[php]

<?php require_once "Classes/PHPExcel/IOFactory.php"; $objPHPExcel = PHPExcel_IOFactory::load("SheetOne.xls"); //Loads SheetOne $objPHPExcel2 = PHPExcel_IOFactory::load("SheetTwo.xls"); // Loads SheetTwo $objPHPExcel->setActiveSheetIndex(); //set first sheet as active $objPHPExcel->setActiveSheetIndex(1); //set second sheet active $objPHPExcel2->getActiveSheet(1)->setCellValue('C1', date('Y-m-d')); //set C1 to current date $objTpl2->getActiveSheet(1)->setCellValue('B1',$objTpl->getActiveSheet()->GetCell('B1')->getValue()); //set the value of B1 in sheet two = value of B1 in sheet one exit; ?>

[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service