@poriko

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

use PhpOffice\PhpSpreadsheet\Reader\Xlsx で not foundが出る

解決したいこと

PHPでExcelのデータを取得したいのでPhpSpreadsheetを使いたいのですが、
"PhpOffice\PhpSpreadsheet\Reader\Xlsx" not found
になってしまします。

composerもインストール済みです。
~windows cmdのログ~


C:\Users\myname>composer require phpoffice/phpspreadsheet
Using version ^1.24 for phpoffice/phpspreadsheet
./composer.json has been updated
Running composer update phpoffice/phpspreadsheet
Loading composer repositories with package information
Updating dependencies
Lock file operations: 11 installs, 0 updates, 0 removals
  - Locking ezyang/htmlpurifier (v4.14.0)
  - Locking maennchen/zipstream-php (2.2.1)
  - Locking markbaker/complex (3.0.1)
  - Locking markbaker/matrix (3.0.0)
  - Locking myclabs/php-enum (1.8.3)
  - Locking phpoffice/phpspreadsheet (1.24.1)
  - Locking psr/http-client (1.0.1)
  - Locking psr/http-factory (1.0.1)
  - Locking psr/http-message (1.0.1)
  - Locking psr/simple-cache (2.0.0)
  - Locking symfony/polyfill-mbstring (v1.26.0)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 11 installs, 0 updates, 0 removals
  - Downloading myclabs/php-enum (1.8.3)
  - Downloading psr/simple-cache (2.0.0)
  - Downloading markbaker/matrix (3.0.0)
  - Downloading markbaker/complex (3.0.1)
  - Downloading symfony/polyfill-mbstring (v1.26.0)
  - Downloading maennchen/zipstream-php (2.2.1)
  - Downloading ezyang/htmlpurifier (v4.14.0)
  - Downloading phpoffice/phpspreadsheet (1.24.1)
  - Installing myclabs/php-enum (1.8.3): Extracting archive
  - Installing psr/simple-cache (2.0.0): Extracting archive
  - Installing psr/http-message (1.0.1): Extracting archive
  - Installing psr/http-factory (1.0.1): Extracting archive
  - Installing psr/http-client (1.0.1): Extracting archive
  - Installing markbaker/matrix (3.0.0): Extracting archive
  - Installing markbaker/complex (3.0.1): Extracting archive
  - Installing symfony/polyfill-mbstring (v1.26.0): Extracting archive
  - Installing maennchen/zipstream-php (2.2.1): Extracting archive
  - Installing ezyang/htmlpurifier (v4.14.0): Extracting archive
  - Installing phpoffice/phpspreadsheet (1.24.1): Extracting archive
4 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating autoload files
3 packages you are using are looking for funding.
Use the `composer fund` command to find out more!

発生している問題・エラー

$reader = new XlsxReader();
を実行すると以下のエラーメッセージが出てしまいます。

PHP Fatal error:  Uncaught Error: Class "PhpOffice\PhpSpreadsheet\Reader\Xlsx" not found in C:\xampp\htdocs\xxxx\index.php



### 該当するソースコード
```PHP
<?php
include('./vendor/autoload.php');
//require './vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
//use phpoffice\phpspreadsheet\src\PhpSpreadsheet\Reader\Xlsx as XlsxReader;

~略~

function XXX($aaa){
    
  $reader = new XlsxReader(); ←ここでエラー

自分で試したこと

①includeやrequireの部分を変える

include('./vendor/autoload.php');の部分を
require './vendor/autoload.php';に変えても同じエラーが出ました。

②use コマンドの引数を変える

PHP Fatal error: Uncaught Error: Class "PhpOffice\PhpSpreadsheet\Reader\Xlsx" not found in C:\xampp\htdocs\xxxx\index.php

というエラーなので、vendor以下のフルパスを確認しました。
フルパスは以下です。
C:\Users\myname\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Reader\Xlsx

そこで、
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
ではなく
use phpoffice\phpspreadsheet\src\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
でも試しましたが、
同様のエラーが出ました。

PHP Fatal error:  Uncaught Error: Class "phpoffice\phpspreadsheet\src\PhpSpreadsheet\Reader\Xlsx" not found in C:\xampp\htdocs\xxxx\index.php

その他

PHP 8.0.8

PHP初心者で、cmdのコマンドも至らない状況ですが、
ご教授いただければ幸いです。

0 likes

1Answer

フォルダが異なるのが原因だと思います。
C:\Users\myname
C:\xampp\htdocs\xxxx

なので./vendor/autoload.phpを読み込んでもロードされていないのだと思います。
phpoffice/phpspreadsheetは別のvendorフォルダにあるのですから。

もしかしたらC:\Users\myname\vendor\autoload.phpも読み込めば動くかもしれませんが、通常は1つのプロジェクトフォルダにまとめるので、どうなるかは何とも言えないところです。

特に理由が無いのであればC:\xampp\htdocs\xxxxフォルダでcomposer require phpoffice/phpspreadsheetコマンドを実行してインストールすれば良いと思います。

0Like

Comments

  1. @poriko

    Questioner

    ありがとうございます。解決いたしました。
    cmd上でC:\xampp\htdocs\xxxx\index.php のある
    C:\xampp\htdocs\xxxxに移動し、
    composer require phpoffice/phpspreadsheet
    を実行したらできました。

Your answer might help someone💌