How to write in an existing xls file ?

Topics: Developer Forum
Aug 25, 2011 at 9:52 PM

Hi Folks,


I´d like to know how can I write in an existing Excel5 File.

the documention examples only contain codes that create and write new Files.




Aug 25, 2011 at 9:57 PM

$workbook = PHPExcel_IOFactory::load('your_file.xls');

Aug 25, 2011 at 10:22 PM

thanks Scott,


Ive tryed :

/** PHPExcel */require_once '../Classes/PHPExcel.php';

$excel2 = PHPExcel_IOFactory::load('test.xls');

$excel2->setActiveSheetIndex(0)            ->setCellValue('A1', 'Hellyyyyyyyyyyyyyyyyyyyyo')           

->setCellValue('B2', 'woryyyyyyyyyyyyyyyyld!')         

  ->setCellValue('C1', 'Heyyyyyyyyyyllo')       

    ->setCellValue('D2', 'woyyyyyyyyyyrld!');



but the file was not changed... what else have to be done...



Aug 26, 2011 at 1:11 PM

Instead of...


Try this...

$objWriter = PHPExcel_IOFactory::createWriter($excel2, 'Excel5');

Test/07reader.php basically does this.

Aug 26, 2011 at 1:29 PM

thanks scott...

that´s it !

Im trying to generate a new excel file based on a file with different formats by cell (in one cell there are bold and intalic word for example)... and filters...

after loading it I´ll write some blank cells and then write a new file.....

but based on the above code ... the generated file came with some failures like wrong format by cell and the filters are not working..

my xls files were made on Microsoft Excel 2003... is there any other specific contructor or other way to load and save this kind of files keeping all of its cell formats and values ?:


thanks again

Mar 15, 2013 at 11:40 AM
i am facing same problem to update existing excel file.........
Mar 15, 2013 at 12:15 PM
Edited Mar 15, 2013 at 12:16 PM
So what exactly is the problem? Do you have any code? Have you read the documentation or looked at the examples?

Can't you read the existing file? Can't you change values? Can't you save it again?
Mar 31, 2013 at 8:48 PM
Hi, I have an issue with the read, modify and save. I got Fatal error: Maximum execution time of 30 seconds exceeded in different functions.
Here is my code

$objPHPexcel = new PHPExcel_IOFactory::load('../Documentos/Formato_Pedido_Marco_Sosa.xls');

$objWorksheet = $objPHPexcel->getActiveSheet();

$objWriter = PHPExcel_IOFactory::createWriter($objPHPexcel, 'Excel5');

Mar 31, 2013 at 9:26 PM
I changed the php.ini to increase the Maximum execution time from 30 to 300, this worked, but the file took a long time be saved. Is there a way to improve it?
I also have enabled the display errors in the setting of WAMP to see what's going on, and it displays a lot of times the same notice.

Notice: Array to string conversion in C:\Documents and Settings\sosam1\My Documents\Dropbox\htdocs\Merdiz\Classes\PHPExcel\Calculation.php on line 3010
Call Stack

Time Memory Function Location

1 0.3626 144976 {main}( ) ..\leer_excel.php:0
2 12.4708 10028008 PHPExcel_Writer_Excel5->save( ) ..\leer_excel.php:37
3 13.7849 11412048 PHPExcel_Writer_Excel5_Worksheet->close( ) ..\Excel5.php:171
4 91.0215 49686024 PHPExcel_Cell->getCalculatedValue( ) ..\Worksheet.php:441
5 91.0215 49686056 PHPExcel_Calculation->calculateCellValue( ) ..\Cell.php:283
6 91.0216 49685984 PHPExcel_Calculation->_calculateFormulaValue( ) ..\Calculation.php:2162
7 91.0222 49688736 PHPExcel_Calculation->_processTokenStack( ) ..\Calculation.php:2325
8 91.6912 50775176 PHPExcel_Calculation->extractCellRange( ) ..\Calculation.php:3065
9 100.7377 64871000 PHPExcel_Cell->getCalculatedValue( ) ..\Calculation.php:3613
10 100.7377 64871000 PHPExcel_Calculation->calculateCellValue( ) ..\Cell.php:283
11 100.7377 64871048 PHPExcel_Calculation->_calculateFormulaValue( ) ..\Calculation.php:2162
12 100.7414 64883040 PHPExcel_Calculation->_processTokenStack( ) ..\Calculation.php:2325
13 100.8481 65117800 PHPExcel_Calculation->extractCellRange( ) ..\Calculation.php:3224
14 100.8483 65118184 PHPExcel_Cell->getCalculatedValue( ) ..\Calculation.php:3602
15 100.8483 65118184 PHPExcel_Calculation->calculateCellValue( ) ..\Cell.php:283
16 100.8483 65118232 PHPExcel_Calculation->_calculateFormulaValue( ) ..\Calculation.php:2162
17 100.8548 65144488 PHPExcel_Calculation->_processTokenStack( ) ..\Calculation.php:2325
18 100.8717 65236840 PHPExcel_Calculation->extractCellRange( ) ..\Calculation.php:3224
19 100.8718 65237240 PHPExcel_Cell->getCalculatedValue( ) ..\Calculation.php:3602
20 100.8719 65237240 PHPExcel_Calculation->calculateCellValue( ) ..\Cell.php:283
21 100.8719 65237296 PHPExcel_Calculation->_calculateFormulaValue( ) ..\Calculation.php:2162
22 100.8783 65263368 PHPExcel_Calculation->_processTokenStack( ) ..\Calculation.php:2325

Any idea to get this better?


Apr 1, 2013 at 12:21 AM

I made my code working, but its too slow... it takes about 5 minutes to complete, is there a way to make it faster?
This is my code, I hope someone could help me.
$objPHPexcel = PHPExcel_IOFactory::load('../Documentos/Formato_Pedido_Marco_Sosa.xls');
$objWorksheet = $objPHPexcel->setActiveSheetIndex(0);
$objWorksheet = $objPHPexcel->getActiveSheet(); 
$objWriter = PHPExcel_IOFactory::createWriter($objPHPexcel,'Excel5'); 

$objPHPexcel = PHPExcel_IOFactory::load('../Documentos/Formato_Pedido_Marco_Sosa.xls');
$objWorksheet = $objPHPexcel->setActiveSheetIndex(0);
$objWorksheet = $objPHPexcel->getActiveSheet();

echo '</br>Clave'.$clave = $objWorksheet->getCell('B22')->getValue();
Apr 1, 2013 at 10:44 AM
ocraw wrote:

I made my code working, but its too slow... it takes about 5 minutes to complete, is there a way to make it faster?
This is my code, I hope someone could help me.

If that's all you're actually doing, how big is your spreadsheet? Working with binary file formats such as Excel isn't faster, but 5 minutes seems excessive unless you're working with workbooks comprising billions of cells
Apr 1, 2013 at 4:49 PM
Hi Mark,

My spreadsheet is not too big, it has some formulas, but is a small file with only two sheets and about 50 rows and 30 cols each one, but I am not sure if it processing all cols and rows (65535) in the sheets. I just noticed it has 2 small images, could they be the problem?
I am also loosing 2 formulas from the document when saving again after modifying the file, it seems to that is because they have a VLOOKUP formula.

=IF(B22="","",IF(ISERROR(VLOOKUP(B22,Precios!$A$3:$C$470,2,FALSE)),"Producto no procesado",VLOOKUP(B22,Precios!$A$3:$C$470,2,FALSE)))

And the code I pasted is all I have.

Any idea?

Apr 2, 2013 at 6:09 AM
Hi Mark,

I am still with the issue of the VLOOKUP, I read that some years ago there were some issues with this. I am using PHPExcel_1.7.8. I think the issues with this were fixed back in 2010.

I hope you can help me.


Apr 2, 2013 at 6:29 AM
Mark, thanks for your support.
The issue was with Excel 5, I updated the spreadsheet I am using to Excel 2007 and it's working just perfect.