PHP code example of kang-babi / spreadsheet

1. Go to this page and download the library: Download kang-babi/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/ */

    

kang-babi / spreadsheet example snippets


use KangBabi\Spreadsheet\Sheet;

$sheet = new Sheet();

$sheet->getSpreadsheetInstance(); # returns \PhpOffice\PhpSpreadsheet\Spreadsheet instance
$sheet->getActiveSheet(); # returns \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet instance

use KangBabi\Spreadsheet\Wrappers\Config;

$sheet->config(function (Config $config): void {
  ->orientation('portrait')
  ->pageFit('page') # fits to page
  ->repeatRows(1, 5) # repeats row 1 to 5
  ->paperSize('a4')
  ->columnWidth('A', 13)
});

use KangBabi\Spreadsheet\Wrappers\Builder;
use KangBabi\Spreadsheet\Wrappers\Row;

$sheet->header(function (Builder $header): void {
  $header
    ->row(function (Row $row): void { # rows start at 1 and increments per row chain
      $row
        ->merge('A', 'H') # merges columns A to H at the current row
        ->value('A', 'THIS IS MY HEADER')
        ->style('A:H', function (Style $style) { # targets A to H at the current row
          $style
            ->fontName('Times New Roman')
            ->alignment('horizontal', 'center') # or use ->horizontal('center')
            ->alignment('vertical', 'top') # or use ->vertical('top')
            ->border('all')
            ->border('bottom', 'none')
            ->strikethrough()
            ->italic();
        });
  })
  ->jump(2) # skips 2 rows and do nothing
  ->then(1, function (Row $row): void {...}); # jumps 1 row and builds row
});

use KangBabi\Spreadsheet\Misc\RichText;

$richText = RichText::make()
  ->text("Initial text ")
  ->bold()
  ->italic()
  ->size(11)
  ->fontName('Times New Roman')
  ->text('additional text ')
  ->size(8)
  ->fontName('Georgia')
  ->strikethrough()
  ->underline();

...
$row
  ->value('A', $richText)
...

use KangBabi\Spreadsheet\Misc\Image;

$image = Image::make()
  ->source('path/to/img/')
  ->from('A1')
  ->to('B1') # stretch image to B2
  ->padX(10, false) # pads left as default, set true to pad right
  ->padY(10, false) # pads top as default, set true to pad bottom
  ->height(100);


Row::macro('row', function (Row $row): void {
  $row
    ->value('A', 'Has')
    ->value('B', 'Macros');
});

$row = new Row();

# call macro
$row->call('row', $row);

# or

# call macro
Row::staticCall('row', $row);

Builder::macro('skip', function (Builder $builder): void {
  $builder
    ->jump()
    ->row(function (Row $row) {
      ...
    });
})

$builder = new Builder();

#call macro
$builder->call('skip', $builder);

# or

# call macro
Builder::staticCall('skip', $builder);

use KangBabi\Misc\Color;

$colors = Color::make()
  ->set('primary', '696cff') # results to 'FF696cff'
  ->set('secondary', '8592a3')
  ->set('success', '71dd37')

# access color values
$colors->get('primary'); # returns 'FF696cff'

# or
$colors->success; # returns 'FF71dd37'

# or static
Color::color('secondary'); # returns 'FF8592a3'

# get all colors
$colors->all();
/**
 * returns [
 *  'primary' => 'FF696cff',
 *  'secondary' => 'FF8592a3',
 *  'success' => 'FF71dd37',
 * ]
 */

# or static
Color::colors();
/**
 * returns [
 *  'primary' => 'FF696cff',
 *  'secondary' => 'FF8592a3',
 *  'success' => 'FF71dd37',
 * ]
 */

# Setting default color
Color::default('primary');

# trigger default
$colors->doesNotExist; # returns 'FF696cff'
$colors->get('doesNotExist'); # returns 'FF696cff'

# or static
Color::color('doesNotExist'); # returns 'FF696cff'

# from step 1

...
  $style
    ->horizontal('center')
    ->fill(Color::color('primary')) # default solid
    ->fill($colors->info, 'none') # fills none
...

$wrapText = true;

$sheet->save('my-sheet', $wrapText); # saves sheet as my-sheet.xlsx, wrap text is enabled by default