Monday, June 2, 2008

PHP 4.x to 5.2.6

I have one last client that was using PHP 4.x, upgrading has been in the works for a long time. The client uses PHP and COM to automate Excel and Word document generation. The classes that performed the automation did not work in PHP 5.x. There wasn't a compelling reason to goto the new version until PHP 4.x was end of lifed.

The upgrade went surprisingly well. A couple of PHP.ini settings and getting the appropriate PEAR packages installed was the majority of work. The one issue was with Excel and COM.

I'm posting below the change that I had to make to the code. From various Google searches I doubt the other 3 PHP developers on the planet using PHP and COM to talk to Microsoft Office will find the below helpful. Regardless here is the change:

  • I initially used the below to write values to a specific spreadsheet, the solution was found here:

    foreach($data as $key => $value ){
    $excel_cell = $Worksheet->Range($key);
    $excel_cell->activate;
    $excel_cell->value = $value;
    }


  • The cell->activate call was not working and I modified the code to the below, suggested here:

    foreach($data as $key => $value ){
    $excel_cell = $Worksheet->Range($key)->Value = $value;
    }


The refactored code is much cleaner and a positive step from a code perspective.

Finally, a stern warning from Microsoft that the process I'm using is a bad idea.

No comments: