<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\Event\Event;
use Zend\Diactoros\Stream;
use Cake\Http\CallbackStream;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
/**
* Excels Controller
*
*
* @method \App\Model\Entity\Excel[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
*/
class ExcelsController extends AppController
{
/**
* Index method
*
* @return \Cake\Http\Response|void
*/
public function print()
{
// ファイル読込
$inputFileName = ROOT . DS . 'uploadfiles' . DS . 'sheet' . DS . 'hoge.xlsx';
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($inputFileName);
// 選択されているシートを取得
$sheet = $spreadsheet->getActiveSheet();
// 書き込み
$sheet->setCellValue('A1', 'A1に書き込み');
// コールバックをストリーム化
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$stream = new CallbackStream(function () use ($writer) {
$writer->save('php://output');
});
// ファイルを出力
$response = $this->response
->withHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
->withHeader('Content-Disposition', 'attachment;filename="hoge.xlsx"')
->withHeader('Cache-Control', 'max-age=0')
->withBody($stream);
return $response;
}
}