承接 harborcompliance/cakephp-xlsxview 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

harborcompliance/cakephp-xlsxview

Composer 安装命令:

composer require harborcompliance/cakephp-xlsxview

包简介

An XLSX View class for CakePHP

README 文档

README

Installation

You can install this plugin into your CakePHP application using composer.

The recommended way to install composer packages is:

composer require harborcompliance/cakephp-xlsxview

Enable plugin

Load the plugin by running command

bin/cake plugin load XlsxView

Usage

A basic example writing an array to the xlsx file.

public function export()
{
    $data = [
        ['id' => 1, 'name' => 'Alex', 'email' => 'alex@example.com'],
        ['id' => 2, 'name' => 'Jason', 'email' => 'jason@example.com'],
    ];

    $this->set(compact('data'));
    $this->viewBuilder()
        ->setClassName('XlsxView.Xlsx')
        ->setOption('serialize', 'data');
}

You may specify an optional header that will be the first row in your xlsx file.

public function export()
{
    $data = [
        ['id' => 1, 'name' => 'Alex', 'email' => 'alex@example.com'],
        ['id' => 2, 'name' => 'Jason', 'email' => 'jason@example.com'],
    ];

    $header = ['ID', 'Name', 'Email'];

    $this->set(compact('data'));
    $this->viewBuilder()
        ->setClassName('XlsxView.Xlsx')
        ->setOptions([
            'serialize' => 'data',
            'header' => $header,
        ]);
}

Automatic View Class Switching

You can use the router's extension parsing feature and the RequestHandlerComponent to automatically use the XlsxView class.

Enable xlsx extension parsing for all routes using Router::extensions('xlsx') in your app's routes.php or using $routes->addExtensions() within the required scope.

// UsersController.php

// In your controller's initialize() method:
$this->loadComponent('RequestHandler');

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

    if ($this->request->is('xlsx')) {
        $this->viewBuilder()->setOptions([
            'serialize' => 'users',
            'header' => $header,
        ]);
    }
}

With the above controller you can now access /users.xlsx or use the Accept header application/vnd.openxmlformats-officedocument.spreadsheetml.sheet to get the data as an xlsx file and use /users to get normal HTML page.

Setting the Downloaded File Name

By default, the downloaded file will be named after the final portion of the url. To explicitly set the filename use the Response::withDownload() method.

public function export()
{
    // ...

    $this->response = $this->response->withDownload('users.xlsx');
}

统计信息

  • 总下载量: 10.92k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 4
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-02-03