PHP code example of anklimsk / cakephp-spreadsheet

1. Go to this page and download the library: Download anklimsk/cakephp-spreadsheet library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

anklimsk / cakephp-spreadsheet example snippets


   CakePlugin::load('CakeSpreadsheet', ['bootstrap' => true, 'routes' => true]);
   

      public function getExportConfig() {
         $header = [__('Field label 1'), __('Field label 2'), __('Field label 3'), __('Field label 4')];
         $width = [35, 20, 10, 15];
         $align = ['L', 'L', 'C', 'R'];
         $fileName = __('Export file');

         return compact('header', 'width', 'align', 'fileName');
      }

      public function getExportData($conditions = []) {
          ...
          $result = [
              'Group header (List name)' => [
                  'Sub header' => [
                      [
                          'Field value 1',
                          'Field value 2',
                          'Field value 3',
                          'Field value 4',
                      ]
                  ]
              ]
          ];

          return $result;
      }
      

      public $components = [
          ...,
          'RequestHandler' => [
              'viewClassMap' => [
                  'xlsx' => 'CakeSpreadsheet.Spreadsheet'
              ]
          ]
      ];
      

      public export($id = null) {
         if (!$this->RequestHandler->prefers('xlsx')) {
            throw new BadRequestException(__('Invalid export type');
         }

         $conditions = [];
         if (!empty($id)) {
            $conditions['Model.id'] = $id;
         }
         $exportConfig = $this->Model->getExportConfig();
         $exportData = $this->Model->getExportData();

         $this->set(compact('exportConfig', 'exportData'));
      }
      

      $this->Html->link('Excel file', ['ext' => 'xlsx']);
      

      if (isset($fileName)) {
         $this->setFileName($fileName);
      }

      $this->Spreadsheet->getDefaultStyle()->applyFromArray([
         'font' => [
            'name' => 'Arial Cyr',
            'size' => 10,
          ],
      ]);
      

      echo $this->element('CakeSpreadsheet.exportExcelTable', compact('exportConfig', 'exportData'));