Including phpspreadsheet Without Composer
When you cannot install Composer on a server due to restrictive file permissions, outdated PHP versions, or legacy environment constraints, pulling in phpspreadsheet becomes a logistical hurdle. The library is substantial, relying on dozens of dependencies like maennchen/zipstream-php, markbaker/complex, and various XML parsers. Without the Composer autoloader, you cannot simply drop the main file into your project; you need to manually gather every class file and ensure their namespaces resolve correctly.
The standard approach involves downloading the library as a source archive rather than a pre-built package. You can fetch the latest stable version directly from GitHub releases or the official repository. Once downloaded, extract the src directory contents into your application's root. This grants you access to the core PhpOffice\PhpSpreadsheet namespace and its child classes. However, this is only half the battle. The library functions by loading these files dynamically via a specific autoloader class. If you simply include the source files without initializing that autoloader, your code will fail when attempting to instantiate objects like \PhpOffice\PhpSpreadsheet\Writer\Xlsx.
To make the library work in a vanilla PHP environment, you must manually register the autoloader. The most reliable method is to include the autoload.php file found within the extracted source directory. You would add this line at the top of your entry script:
require_once 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
$spreadsheet = new Spreadsheet();
If you have placed the library directly in your project root without a vendor folder, the path must be adjusted accordingly. Alternatively, you can utilize a custom autoloader function that maps the specific namespaces to their file locations within the extracted src/PhpOffice/PhpSpreadsheet directory. This requires defining an array of class-to-file mappings or iterating through the directory structure to build the map dynamically before registering it with spl_autoload_register.
One critical trade-off in this manual setup is version management. Unlike Composer, which pins exact versions and manages conflict resolution across dependencies, a manually extracted tarball represents a single point in time. If the library releases a patch that fixes a security vulnerability or addresses a memory leak, you must manually re-download the archive and replace your current files. There is no mechanism for automatic updates, forcing you to monitor release notes closely if you are running this in a production environment where stability is paramount.
Another consideration is the size of the download. The source archive for phpspreadsheet can be several megabytes. While not unmanageable, it adds significant load during deployment compared to a single file inclusion. If storage space is extremely limited on the target server, you might consider stripping out unused writers or readers before uploading, though this requires careful auditing of the codebase to ensure no critical functionality is inadvertently removed.
For Laravel projects specifically, integrating the library without Composer can be tricky because Laravel's composer.json and autoloading mechanisms expect certain vendor entries. However, if the project structure allows it, you can still register a custom autoloader in config/app.php or manually require the file in your bootstrap sequence. This approach bypasses the need for a vendor/ folder but requires ensuring that no other packages in your project depend on specific versions of these libraries that might conflict with the manually included files.
In environments where Composer is strictly prohibited, this manual extraction and autoloading strategy provides a viable path forward. It restores functionality to applications like Laravel or Symfony that rely on phpspreadsheet for handling Excel files, provided you are diligent about version control and file integrity.