PHPackages                             impronta48/cakephp-xlsview - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. impronta48/cakephp-xlsview

ActiveCakephp-plugin[Templating &amp; Views](/categories/templating)

impronta48/cakephp-xlsview
==========================

A Xls View class for CakePHP

1.0.0(1y ago)1702[1 issues](https://github.com/impronta48/cakephp-xlsview/issues)[1 PRs](https://github.com/impronta48/cakephp-xlsview/pulls)MITPHPPHP &gt;=8.1

Since Nov 10Pushed 1y ago1 watchersCompare

[ Source](https://github.com/impronta48/cakephp-xlsview)[ Packagist](https://packagist.org/packages/impronta48/cakephp-xlsview)[ Docs](https://github.com/impronta48/cakephp-xlsview)[ RSS](/packages/impronta48-cakephp-xlsview/feed)WikiDiscussions master Synced today

READMEChangelog (2)Dependencies (4)Versions (2)Used By (0)

XlsView Plugin
==============

[](#xlsview-plugin)

Quickly enable XLS output of your model data.

This branch is for CakePHP **5.x**.

Background
----------

[](#background)

I needed to quickly export XLS of stuff in the database. Using a view class to iterate manually would be a chore to replicate for each export method, so I figured it would be much easier to do this with a custom view class, like JsonView or XmlView. This Repo is Based on the work of

Installation
------------

[](#installation)

```
composer require impronta48/cakephp-xlsview

```

### Enable plugin

[](#enable-plugin)

Load the plugin by running command

```
bin/cake plugin load XlsView

```

Usage
-----

[](#usage)

To export a flat array as a Xls, one could write the following code:

```
public function export()
{
    $data = [
        ['a', 'b', 'c'],
        [1, 2, 3],
        ['you', 'and', 'me'],
    ];

    $this->set(compact('data'));
    $this->viewBuilder()
        ->setClassName('XlsView.xls')
        ->setOption('serialize', 'data');
}
```

All variables that are to be included in the Xls must be specified in the `serialize` view option, exactly how `JsonView` or `XmlView` work.

It is possible to have multiple variables in the Xls output:

```
public function export()
{
    $data = [['a', 'b', 'c']];
    $data_two = [[1, 2, 3]];
    $data_three = [['you', 'and', 'me']];

    $serialize = ['data', 'data_two', 'data_three'];

    $this->set(compact('data', 'data_two', 'data_three'));
    $this->viewBuilder()
        ->setClassName('XlsView.Xls')
        ->setOption('serialize', $serialize);
}
```

If you want headers or footers in your Xls output, you can specify either a `header` or `footer` view option. Both are completely optional:

```
public function export()
{
    $data = [
        ['a', 'b', 'c'],
        [1, 2, 3],
        ['you', 'and', 'me'],
    ];

    $header = ['Column 1', 'Column 2', 'Column 3'];
    $footer = ['Totals', '400', '$3000'];

    $this->set(compact('data'));
    $this->viewBuilder()
        ->setClassName('XlsView.Xls')
        ->setOptions([
            'serialize' => 'data',
            'header' => $header,
            'footer' => $footer,
        ]);
}
```

If you have complex model data, you can use the `extract` view option to specify the individual [`Hash::extract()`-compatible](http://book.cakephp.org/4/en/core-libraries/hash.html) paths or a callable for each record:

```
public function export()
{
    $posts = $this->Posts->find();
    $header = ['Post ID', 'Title', 'Created'];
    $extract = [
        'id',
        function (array $row) {
            return $row['title'];
        },
        'created'
    ];

    $this->set(compact('posts'));
    $this->viewBuilder()
        ->setClassName('XlsView.Xls')
        ->setOptions([
            'serialize' => 'posts',
            'header' => $header,
            'extract' => $extract,
        ]);
}
```

#### Automatic view class switching

[](#automatic-view-class-switching)

You can use the controller's content negotiation feature to automatically have the XlsView class switched in as follows.

Enable `Xls` extension parsing using `$routes->addExtensions(['xls'])` within required scope in your app's `routes.php`.

```
// PostsController.php

// Add the XlsView class for content type negotiation
public function initialize(): void
{
    parent::initialize();

    $this->addViewClasses(['xls' => 'XlsView.Xls']);
}

// Controller action
public function index()
{
    $posts = $this->Posts->find();
    $this->set(compact('posts'));

    if ($this->request->is('xls')) {
        $serialize = 'posts';
        $header = array('Post ID', 'Title', 'Created');
        $extract = array('id', 'title', 'created');

        $this->viewBuilder()->setOptions(compact('serialize', 'header', 'extract'));
    }
}
```

With the above controller you can now access `/posts.xls` or use `Accept` header `text/xls` to get the data as xls and use `/posts` to get normal HTML page.

For really complex Xlss, you can also use your own view files. To do so, either leave `serialize` unspecified or set it to null. The view files will be located in the `xls` subdirectory of your current controller:

```
// View used will be in templates/Posts/xls/export.php
public function export()
{
    $posts = $this->Posts->find();
    $this->set(compact('posts'));
    $this->viewBuilder()
        ->setClassName('XlsView.xls')
        ->setOption('serialize', null);
}
```

#### Setting the downloaded file name

[](#setting-the-downloaded-file-name)

By default, the downloaded file will be named after the last segment of the URL used to generate it. Eg: `example.com/my-controller/my-action` would download `my-action.xls`, while `example.com/my-controller/my-action/first-param` would download `first-param.xls`.

> In IE you are required to set the filename, otherwise it will download as a text file.

To set a custom file name, use the `Response::withDownload()` method. The following snippet can be used to change the downloaded file from `export.xls` to `my-file.xls`:

```
public function export()
{
    $data = [
        ['a', 'b', 'c'],
        [1, 2, 3],
        ['you', 'and', 'me'],
    ];

    $this->setResponse($this->getResponse()->withDownload('my-file.xls'));
    $this->set(compact('data'));
    $this->viewBuilder()
        ->setClassName('XlsView.xls')
        ->setOption('serialize', 'data');
}
```

#### Using a specific View Builder

[](#using-a-specific-view-builder)

In some cases, it is better not to use the current controller's View Builder `$this->viewBuilder()` as any call to `$this->render()` will compromise any subsequent rendering.

For example, in the course of your current controller's action, if you need to render some data as Xls in order to simply save it into a file on the server.

Do not forget to add to your controller:

```
use Cake\View\ViewBuilder;
```

So you can create a specific View Builder:

```
// Your data array
$data = [];

// Create the builder
$builder = new ViewBuilder();
$builder
    ->setLayout(false)
    ->setClassName('XlsView.Xls')
    ->setOptions(compact('serialize'));

// Then the view
$view = $builder->build($data);
$view->set(compact('data'));

// And Save the file
file_put_contents('/full/path/to/file.xls', $view->render());
```

###  Health Score

24

—

LowBetter than 31% of packages

Maintenance17

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

600d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/5368022?v=4)[iMpronta48](/maintainers/impronta48)[@impronta48](https://github.com/impronta48)

---

Top Contributors

[![impronta48](https://avatars.githubusercontent.com/u/5368022?v=4)](https://github.com/impronta48 "impronta48 (5 commits)")

---

Tags

exportcakephpxlsview

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/impronta48-cakephp-xlsview/health.svg)

```
[![Health](https://phpackages.com/badges/impronta48-cakephp-xlsview/health.svg)](https://phpackages.com/packages/impronta48-cakephp-xlsview)
```

###  Alternatives

[craftcms/cms

Craft CMS

3.6k3.6M3.1k](/packages/craftcms-cms)[friendsofcake/cakephp-csvview

A CSV View class for CakePHP

1782.6M5](/packages/friendsofcake-cakephp-csvview)[anourvalar/office

Generate documents from existing Excel &amp; Word templates | Export tables to Excel (Grids)

24095.2k](/packages/anourvalar-office)[cakephp/bake

Bake plugin for CakePHP

11212.0M202](/packages/cakephp-bake)[dereuromark/cakephp-queue

The Queue plugin for CakePHP provides deferred task execution.

308954.9k25](/packages/dereuromark-cakephp-queue)[dereuromark/cakephp-ide-helper

CakePHP IdeHelper Plugin to improve auto-completion

1882.3M44](/packages/dereuromark-cakephp-ide-helper)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
