PHP code example of staffim / spreadsheet-builder

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

    

staffim / spreadsheet-builder example snippets




namespace Acme\Xlsx;

use Staffim\SpreadsheetBuilder\AbstractWorksheetBuilder;
use PhpOffice\PhpSpreadsheet\Style\Alignment;

class UsersListWorksheetBuilder extends AbstractWorksheetBuilder {

   public function getTableTitle(iterable $data): string
   {
       return sprintf('Users list at %s', (new \DateTime())->format('d.m.Y'));
   }

   public function getWorksheetTitle(iterable $data): string
   {
       return 'Users list';
   }

   protected function getColumnsSettings(iterable $data): array
   {
       return [
           [
               'title' => 'ID',
               'value' => static function (User $user) {
                   return $user->getId();
               },
               'width' => 5,
           ],
           [
               'title' => 'Email',
               'value' => static function (User $user) {
                   return $user->getEmail();
               },
               'width' => 20,
           ],
           [
              'title' => 'About',
              'value' => static function (User $user) {
                  return $user->getAbout();
              },
              'width' => 25,
              'style' => [// all available phpspreadsheet's styles
                'alignment' => [
                    'wrapText' => true,
                    'vertical' => Alignment::VERTICAL_TOP,
                ],
              ],
          ],
       ];
   }
}


use Staffim\SpreadsheetBuilder\Builder;

/// ....

$builder = new Builder([
    new \Acme\Xlsx\UsersListWorksheetBuilder(),
]);

$users = $usersManager->fetchUsers();

$spreadsheet = $builder->build([$users]);


$converter = new \Staffim\SpreadsheetBuilder\RichTextToHtmlConverter(
     [
         new BoldConverter(),
         new ItalicConverter(),
         new UnderlineConverter(),
         new ColorConverter(),
     ]
 );
};

$html = '<span style="color: brown; font-weight: bold">bold<br/></span>
<i style="color: #ffcc01">italic</i><br/>
<b style="text-decoration: underline">underline111</b>
<span style="font-weight: bold; color: #AA0000">bold red</span>';

$richText = $converter->convertFromHtml($html);