Store a PHPExcel() object for late use

Topics: Developer Forum, User Forum
Feb 23, 2011 at 10:32 AM

Hi all,

I need to "store" a PHPExcel() object ready to use for write (eg: PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); ) in a PHP variable (maybe a $_SESSION() variable) to recall later in another script.

I'm a newbie on PHPExcel so I had'nt  a clear vision of the objects: I try to set $_SESSION('my_memo')=$objPHPExcel and then use PHPExcel_IOFactory::createWriter($_SESSION('my_memo'), 'Excel5') but it fails.

Thanks in advance

 

Stefano

Feb 24, 2011 at 2:27 PM

Not sure it works but unless there is a function within PHPExcel to serialize an object I think you could do something like

// To store you object
$myPhpExcelObj = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

$_SESSION['myPhpExcelObj'] = serialize($myPhpExcelObj);

//and then to retrive the object you do
$myPhpExcelObj = unserialize($_SESSION['myPhpExcelObj']);

You could/should probably look into the caching mechanics of PHPExcel first to see how you can use that in conjunction and prehaps save to a file when you serialize instead of the Session.

Coordinator
Feb 24, 2011 at 9:56 PM

There is PHPExcel_Reader_Serialized and PHPExcel_Writer_Serialized, but these have been broken for some time. These basically used serialize() and unserialize() on the PHPExcel object; but as the relationships between the different components of the PHPExcel object have grown more complex, and with the addition of cell caching, saved objects can't be restored.

I'm dropping them from the next release until such time as I can rewrite them.

The only working alternative at the moment is to actually write to a temporary file using the Excel5 or Excel2007 Writer, then subsequently reread it again.