{"id":20108453,"url":"https://github.com/impronta48/cakephp-xlsview","last_synced_at":"2025-05-06T09:33:08.847Z","repository":{"id":262056992,"uuid":"886088129","full_name":"impronta48/cakephp-xlsview","owner":"impronta48","description":"CakePHP Plugin that generates XLS files","archived":false,"fork":false,"pushed_at":"2025-01-11T14:22:26.000Z","size":18,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-14T19:06:19.573Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/impronta48.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-10T06:44:10.000Z","updated_at":"2025-01-11T14:20:58.000Z","dependencies_parsed_at":"2024-11-10T08:27:41.227Z","dependency_job_id":"e36f192b-f496-4ade-b19a-7d62dd665357","html_url":"https://github.com/impronta48/cakephp-xlsview","commit_stats":null,"previous_names":["impronta48/cakephp-xlsview"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/impronta48%2Fcakephp-xlsview","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/impronta48%2Fcakephp-xlsview/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/impronta48%2Fcakephp-xlsview/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/impronta48%2Fcakephp-xlsview/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/impronta48","download_url":"https://codeload.github.com/impronta48/cakephp-xlsview/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252657485,"owners_count":21783831,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-13T18:01:06.903Z","updated_at":"2025-05-06T09:33:08.840Z","avatar_url":"https://github.com/impronta48.png","language":"PHP","funding_links":[],"categories":["Templating","Plugins"],"sub_categories":["Templating"],"readme":"# XlsView Plugin\n\nQuickly enable XLS output of your model data.\n\nThis branch is for CakePHP **5.x**. \n\n## Background\n\nI needed to quickly export XLS of stuff in the database. Using a view class to\niterate manually would be a chore to replicate for each export method, so I\nfigured it would be much easier to do this with a custom view class,\nlike JsonView or XmlView.\nThis Repo is Based on the work of https://github.com/FriendsOfCake/cakephp-Csvview/\n\n## Installation\n\n```\ncomposer require impronta48/cakephp-xlsview\n```\n\n### Enable plugin\n\nLoad the plugin by running command\n\n    bin/cake plugin load XlsView\n\n## Usage\n\nTo export a flat array as a Xls, one could write the following code:\n\n```php\npublic function export()\n{\n    $data = [\n        ['a', 'b', 'c'],\n        [1, 2, 3],\n        ['you', 'and', 'me'],\n    ];\n\n    $this-\u003eset(compact('data'));\n    $this-\u003eviewBuilder()\n        -\u003esetClassName('XlsView.xls')\n        -\u003esetOption('serialize', 'data');\n}\n```\n\nAll variables that are to be included in the Xls must be specified in the\n`serialize` view option, exactly how `JsonView` or `XmlView` work.\n\nIt is possible to have multiple variables in the Xls output:\n\n```php\npublic function export()\n{\n    $data = [['a', 'b', 'c']];\n    $data_two = [[1, 2, 3]];\n    $data_three = [['you', 'and', 'me']];\n\n    $serialize = ['data', 'data_two', 'data_three'];\n\n    $this-\u003eset(compact('data', 'data_two', 'data_three'));\n    $this-\u003eviewBuilder()\n        -\u003esetClassName('XlsView.Xls')\n        -\u003esetOption('serialize', $serialize);\n}\n```\n\nIf you want headers or footers in your Xls output, you can specify either a\n`header` or `footer` view option. Both are completely optional:\n\n```php\npublic function export()\n{\n    $data = [\n        ['a', 'b', 'c'],\n        [1, 2, 3],\n        ['you', 'and', 'me'],\n    ];\n\n    $header = ['Column 1', 'Column 2', 'Column 3'];\n    $footer = ['Totals', '400', '$3000'];\n\n    $this-\u003eset(compact('data'));\n    $this-\u003eviewBuilder()\n        -\u003esetClassName('XlsView.Xls')\n        -\u003esetOptions([\n            'serialize' =\u003e 'data',\n            'header' =\u003e $header,\n            'footer' =\u003e $footer,\n        ]);\n}\n```\n\n\nIf you have complex model data, you can use the `extract` view option to\nspecify the individual [`Hash::extract()`-compatible](http://book.cakephp.org/4/en/core-libraries/hash.html) paths\nor a callable for each record:\n\n```php\npublic function export()\n{\n    $posts = $this-\u003ePosts-\u003efind();\n    $header = ['Post ID', 'Title', 'Created'];\n    $extract = [\n        'id',\n        function (array $row) {\n            return $row['title'];\n        },\n        'created'\n    ];\n\n    $this-\u003eset(compact('posts'));\n    $this-\u003eviewBuilder()\n        -\u003esetClassName('XlsView.Xls')\n        -\u003esetOptions([\n            'serialize' =\u003e 'posts',\n            'header' =\u003e $header,\n            'extract' =\u003e $extract,\n        ]);\n}\n```\n\n#### Automatic view class switching\n\nYou can use the controller's content negotiation feature to automatically have\nthe XlsView class switched in as follows.\n\nEnable `Xls` extension parsing using `$routes-\u003eaddExtensions(['xls'])` within required\nscope in your app's `routes.php`.\n\n```php\n// PostsController.php\n\n// Add the XlsView class for content type negotiation\npublic function initialize(): void\n{\n    parent::initialize();\n\n    $this-\u003eaddViewClasses(['xls' =\u003e 'XlsView.Xls']);\n}\n\n// Controller action\npublic function index()\n{\n    $posts = $this-\u003ePosts-\u003efind();\n    $this-\u003eset(compact('posts'));\n\n    if ($this-\u003erequest-\u003eis('xls')) {\n        $serialize = 'posts';\n        $header = array('Post ID', 'Title', 'Created');\n        $extract = array('id', 'title', 'created');\n\n        $this-\u003eviewBuilder()-\u003esetOptions(compact('serialize', 'header', 'extract'));\n    }\n}\n```\n\nWith the above controller you can now access `/posts.xls` or use `Accept` header\n`text/xls` to get the data as xls and use `/posts` to get normal HTML page.\n\nFor really complex Xlss, you can also use your own view files. To do so, either\nleave `serialize` unspecified or set it to null. The view files will be located\nin the `xls` subdirectory of your current controller:\n\n```php\n// View used will be in templates/Posts/xls/export.php\npublic function export()\n{\n    $posts = $this-\u003ePosts-\u003efind();\n    $this-\u003eset(compact('posts'));\n    $this-\u003eviewBuilder()\n        -\u003esetClassName('XlsView.xls')\n        -\u003esetOption('serialize', null);\n}\n```\n\n#### Setting the downloaded file name\n\nBy default, the downloaded file will be named after the last segment of the URL\nused to generate it. Eg: `example.com/my-controller/my-action` would download\n`my-action.xls`, while `example.com/my-controller/my-action/first-param` would\ndownload `first-param.xls`.\n\n\u003e In IE you are required to set the filename, otherwise it will download as a text file.\n\nTo set a custom file name, use the `Response::withDownload()` method. The following\nsnippet can be used to change the downloaded file from `export.xls` to `my-file.xls`:\n\n```php\npublic function export()\n{\n    $data = [\n        ['a', 'b', 'c'],\n        [1, 2, 3],\n        ['you', 'and', 'me'],\n    ];\n\n    $this-\u003esetResponse($this-\u003egetResponse()-\u003ewithDownload('my-file.xls'));\n    $this-\u003eset(compact('data'));\n    $this-\u003eviewBuilder()\n        -\u003esetClassName('XlsView.xls')\n        -\u003esetOption('serialize', 'data');\n}\n```\n\n#### Using a specific View Builder\n\nIn some cases, it is better not to use the current controller's View Builder\n`$this-\u003eviewBuilder()` as any call to `$this-\u003erender()` will compromise any\nsubsequent rendering.\n\nFor example, in the course of your current controller's action, if you need to\nrender some data as Xls in order to simply save it into a file on the server.\n\nDo not forget to add to your controller:\n\n```php\nuse Cake\\View\\ViewBuilder;\n```\nSo you can create a specific View Builder:\n\n```php\n// Your data array\n$data = [];\n\n// Create the builder\n$builder = new ViewBuilder();\n$builder\n    -\u003esetLayout(false)\n    -\u003esetClassName('XlsView.Xls')\n    -\u003esetOptions(compact('serialize'));\n\n// Then the view\n$view = $builder-\u003ebuild($data);\n$view-\u003eset(compact('data'));\n\n// And Save the file\nfile_put_contents('/full/path/to/file.xls', $view-\u003erender());\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimpronta48%2Fcakephp-xlsview","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fimpronta48%2Fcakephp-xlsview","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimpronta48%2Fcakephp-xlsview/lists"}