Blog

Use PhpSpreadsheet to read XLSX file in Drupal

jcisio
01/10/2019
PhpSpreadsheet cannot open stream wrapper which is widely used in Drupal.

We have been using PhpSpreadsheet since several years (when it was still named PhpExcel) with Drupal to read XLSX file. The chance is that, we never use it with a stream wrapper, which is particularly widely used in Drupal. Turn out that it does not work with stream wrapper because it uses ZipArchive to uncompress XLSX file and ZipArchive::open() does not support stream wrapper.

While I think it is reasonnable that ZipArchive does not support stream wrapper because of some limit in the ZIP format and it would be nice that PhpSpreadsheet support it, I just need a way to work around this (then still unknown) problem. Get the real path:

$filepath = \Drupal::service('file_system')->realpath($uri);
$speadsheet = IOFactory::load($filepath);

That's it. I omitted all debugging details by the way.

PS: the server should have zip extension installed. ZipArchive is used by core, but only in the web installer and thus there is no mention about this extension in the requirement.