composerインストール
composerが入っていない場合入れる。
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
rm composer-setup.php
sudo mv composer.phar /usr/local/bin/composer
composer -V
php composer-setup.php
rm composer-setup.php
sudo mv composer.phar /usr/local/bin/composer
composer -V
the requested PHP extension zip is missing from your system.
と表示される場合は php-zip が必要。
sudo yum install --enablerepo=remi,remi-php73 php-zip
systemctl restart httpd.service
systemctl restart httpd.service
PhpSpreadsheetインストール
composer require phpoffice/phpspreadsheet
PhpSpreadsheetの動作確認
<?php require 'vendor/autoload.php'; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue('A1', 'Hello World !'); $writer = new Xlsx($spreadsheet); $writer->save('hello world.xlsx');
書き込み許可が必要
PhpSpreadsheetでExcel読み込み
<?php // PHPExcel // include_once ( '/**/PHPExcel/Classes/PHPExcel.php'); // include_once ( '/**/PHPExcel/Classes/PHPExcel/IOFactory.php'); // ↓ // phpspreadsheet require $_SERVER['DOCUMENT_ROOT'] . '/vendor/autoload.php'; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter; use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader; // xlsファイルの場合は下記 // use PhpOffice\PhpSpreadsheet\Writer\Xls as XlsWriter; // use PhpOffice\PhpSpreadsheet\Reader\Xls as XlsReader; // PHPExcel // $reader = PHPExcel_IOFactory::createReader('Excel5'); // ↓ // phpspreadsheet $reader = new XlsReader(); $excel = $reader->load('base.xls'); $sheet = $excel->getSheet(0); $sheet->copy(); $sheet->setTitle($title); $excel->addSheet($sheet, null); $excel->removeSheetByIndex(0);
操作関数自体は余り変わっていないようだ。
エクセルファイル作成 or ダウンロード
<?php // PHPExcel // $writer=PHPExcel_IOFactory::createWriter($excel,'Excel5'); // $writer->save('php://output'); // $writer->save($path); // ↓ // phpspreadsheet use PhpOffice\PhpSpreadsheet\IOFactory; $reader = IOFactory::createWriter(excel, 'Xls'); $writer->save('php://output'); // $writer->save($path);
参考
https://knooto.info/php-phpexcel-vs-phpspreadsheet/