Writing to Worksheets with existing Data

Topics: Developer Forum
Sep 22, 2011 at 8:28 AM

Hi

I am having problem on how to add data to an existing worksheets with existing data.

Hoping to hear from anyone here.

thanks.

Sep 22, 2011 at 12:18 PM

What problem are you having? At its simplest, you load an existing workbook...

$workbook = PHPExcel_IOFactory::load('workbook.xls');
...add your data like you would otherwise, and output your results somehow.

Sep 23, 2011 at 2:39 AM
Edited Sep 23, 2011 at 2:42 AM

Thanks Scott,

here is actually my problem.

$objReader = PHPExcel_IOFactory::createReader('Excel2007');     
$sheetnames = array('W Dat','M Dat', 'Q Dat');             
$objReader->setLoadSheetsOnly($sheetnames);      
$objPHPExcel = $objReader->load($input_file);             
$objPHPExcel->setActiveSheetIndex($objPHPExcel->getIndex($objPHPExcel->getSheetByName('W Dat')));      
$objWorksheet = $objPHPExcel->getActiveSheet();             
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
foreach($bank_credit as $line){  
	if($column_counter >= 12){                   
		if($proceed){                     
			$details = explode(' ', trim($line));                     
			$bc[$details[0]] = $details[count($details)-1];                     
			$objWorksheet->setCellValueByColumnAndRow(0, $lineStart, $details[0]);                      
			$objWorksheet->setCellValueByColumnAndRow(1, $lineStart, $details[count($details)-1]);                      
			$lineStart++;                   
		}                   
		if(strpos($line, stringDate($lastDate)) !== false){                     
			$proceed = true;                   
		}                 
	}                 
	$column_counter++;               
}

$objReader->save($input_file);
When I run the code I get this error :
Fatal error: Call to undefined method PHPExcel_Reader_Excel2007::save() in C:\xampp\htdocs\accounting\banks_update.php on line 176
Please Help.



 

Sep 23, 2011 at 7:27 AM
Edited Sep 23, 2011 at 7:30 AM

wouldn't you need a writer, and not a reader to save the excelsheet? ;)

 

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save($filename);

Sep 23, 2011 at 7:32 AM

What i will be doing is reading and writing into a file in one full swing.

The logic is i am going to read the worksheet and check for updates and insert the updates on the same worksheet.

Thanks.

Sep 23, 2011 at 8:11 AM
Edited Sep 23, 2011 at 8:12 AM

That doesn't change anything. you need a reader to read, and a writer to write ;)

 

Architecture of PHPExcel is fairly simple, the reader convert the input format to an internal representation. The writers do the opposite, ie. they convert the internal representation to one of the supported file formats. In your case, if you want to output an excel2007 file, you need the excel2007 writer.

Sep 23, 2011 at 9:49 AM

do u have any sample code that writes on a worksheet with existing data.

Sep 23, 2011 at 12:12 PM

Just replace your one save line with code like what borft posted depending on what output you want.