Problem with Read/Write

Topics: Developer Forum, User Forum
Jan 5, 2012 at 5:53 PM

I want to read a xls/xlsx file that is on my server. Then i want to add content to a cell and save this file with the new content.

My problem is that when i run my code nothing happend to my file.
The examples work all.

Here is my test code built from the sample file:




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

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

if (!file_exists("test.xlsx")) {
	exit("Please run 05featuredemo.php first.\n");

echo date('H:i:s') . " Load from Excel2007 file\n";
$objPHPExcel = PHPExcel_IOFactory::load("test.xlsx");

// Add some data
echo date('H:i:s') . " Add some data\n";
            ->setCellValue('A1', 'Hello')
            ->setCellValue('B2', 'world!')
            ->setCellValue('C1', 'Hello')
            ->setCellValue('D2', 'world!');

echo date('H:i:s') . " Write to Excel2007 format\n";
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));

// Echo memory peak usage
echo date('H:i:s') . " Peak memory usage: " . (memory_get_peak_usage(true) / 1024 / 1024) . " MB\r\n";

// Echo done
echo date('H:i:s') . " Done writing files.\r\n";

Jan 5, 2012 at 9:08 PM

The Writer's save() method accepts a filename as its argument.

You're reading test.xlsx but saving as str_replace('.php', '.xlsx', __FILE__) ie an xlsx file sharing the same name as your script but with an xlsx extension.