このバンドルは、Symfony 4/5/6/7 アプリを PHPOffice PhpSpreadsheet 生産性ライブラリと統合します。
このバンドルには、各 PHPOffice ライブラリの前提条件に加えて、以下が必要です。
* PHP 7.2 or higher
* Symfony 4 or higher
注: v1.0.0 より古いタグ (v0.2.0 など) は、PHP <= 7.1 と Symfony <= 4.4 の両方で非推奨ステータスになっているため、サポートされなくなりました。
最新の安定バージョンを必要とする場合は、composer を使用してください。
composer require yectep/phpspreadsheet-bundle Flex を使用していない場合は、 AppKernel.phpまたはbundles.phpファイルでバンドルを有効にします。
$ bundles = array (
[...]
new Yectep PhpSpreadsheetBundle PhpSpreadsheetBundle (),
);このバンドルにより、 phpoffice.spreadsheetサービスが有効になります。
PHPOffice PhpSpreadsheet の公式ドキュメントも参照してください。
空のPhpOfficePhpSpreadsheetSpreadsheetオブジェクトを作成するか、オプションの$filenameが渡された場合は、 PhpOfficePhpSpreadsheetIOFactoryインスタンス化して、ファイルを読み取るための適切なIWriterクラスを自動的に検出して使用します。
// In your controller
$ newSpreadsheet = $ this -> get ( ' phpoffice.spreadsheet ' )-> createSpreadsheet ();
$ existingXlsx = $ this -> get ( ' phpoffice.spreadsheet ' )-> createSpreadsheet ( ' /path/to/file.xlsx ' );string $type)指定された$typeのPhpOfficePhpSpreadsheetReaderクラスのインスタンスを返します。
型では大文字と小文字が区別されます。サポートされているタイプは次のとおりです。
Xlsx :エクセル2007Xls : Excel 5/BIFF (95)Xml : Excel 2003 XMLSlk : シンボリック リンク (SYLK)Ods : Open/Libre Office (ODS)Csv : CSVHtml : HTML $ readerXlsx = $ this -> get ( ' phpoffice.spreadsheet ' )-> createReader ( ' Xlsx ' );
$ spreadsheet = $ readerXlsx -> load ( ' /path/to/file.xlsx ' );Spreadsheet $spreadsheet, string $type) PhpOfficePhpSpreadsheetSpreadsheetオブジェクトと Writer $typeを指定すると、その型のPhpOfficePhpSpreadsheetWriterクラスのインスタンスを返します。
適切な PHP ライブラリがインストールされている場合、上記の読み取りタイプに加えて、これらのタイプは書き込み用にもサポートされます。
TcpdfMpdfDompdf $ spreadsheet = $ this -> get ( ' phpoffice.spreadsheet ' )-> createSpreadsheet ();
$ spreadsheet -> getActiveSheet ()-> setCellValue ( ' A1 ' , ' Hello world ' );
$ writerXlsx = $ this -> get ( ' phpoffice.spreadsheet ' )-> createWriter ( $ spreadsheet , ' Xlsx ' );
$ writerXlsx -> save ( ' /path/to/destination.xlsx ' );貢献は大歓迎です。プロジェクトをフォークし、完了したら PR を送信します。
残りの ToDo は次のとおりです。
Symfony Serializer コンポーネント + CSV エンコーダーから移行している場合は、次のようなコードを使用できます。
$ spreadsheet = $ this -> get ( ' phpoffice.spreadsheet ' )-> createSpreadsheet ();
$ sheet = $ spreadsheet -> getActiveSheet ();
$ sheet -> setTitle ( $ this -> filterVars [ ' wareCategory ' ]-> getTitle ());
$ columnsMap = [];
$ lineIndex = 2 ;
foreach ( $ data as $ line ) {
foreach ( $ line as $ columnName => $ columnValue ) {
if ( is_int ( $ columnIndex = array_search ( $ columnName , $ columnsMap ))) {
$ columnIndex ++;
} else {
$ columnsMap [] = $ columnName ;
$ columnIndex = count ( $ columnsMap );
}
$ sheet -> getCellByColumnAndRow ( $ columnIndex , $ lineIndex )-> setValue ( $ columnValue );
}
$ lineIndex ++;
}
foreach ( $ columnsMap as $ columnMapId => $ columnTitle ) {
$ sheet -> getCellByColumnAndRow ( $ columnMapId + 1 , 1 )-> setValue ( $ columnTitle );
}
$ writer = $ this -> get ( ' phpoffice.spreadsheet ' )-> createWriter ( $ spreadsheet , ' Xlsx ' );
ob_start ();
$ writer -> save ( ' php://output ' );
$ excelOutput = ob_get_clean ();
return new Response (
$ excelOutput ,
200 ,
[
' content-type ' => ' text/x-csv; charset=windows-1251 ' ,
' Content-Disposition ' => ' attachment; filename="price.xlsx" '
]
);