Home » Php » How to open an Excel file with PHPExcel for both reading and writing?

How to open an Excel file with PHPExcel for both reading and writing?

Posted by: admin November 30, 2017 Leave a comment


I’m using the PHPExcel library, and I’m creating xls objects either for writing or for reading:


How can I open an XLSX file for reading and writing?


You load a file into PHPExcel using a reader and the load() method, then save that file using a writer and the save() method… but PHPExcel itself is unaware of the source of the PHPExcel object… it doesn’t care whether you have loaded it from a file (or what type of file) or created it by hand.

As such, there is no concept of “opening for read/write”. You simply read the file by name, and save to the same filename. That will overwrite the original file with any changes that you have made in your script.




set_include_path(get_include_path() . PATH_SEPARATOR . './Classes/');

include 'PHPExcel/IOFactory.php';

$fileType = 'Excel5';
$fileName = 'testFile.xls';

// Read the file
$objReader = PHPExcel_IOFactory::createReader($fileType);
$objPHPExcel = $objReader->load($fileName);

// Change the file
            ->setCellValue('A1', 'Hello')
            ->setCellValue('B1', 'World!');

// Write the file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $fileType);

And can I suggest that you read the documentation, and look at the sample code in /Tests

Leave a Reply

Your email address will not be published. Required fields are marked *