1. Go to this page and download the library: Download fastbolt/excel-writer 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/ */
fastbolt / excel-writer example snippets
$ composer
$columns = [
new ColumnSetting('Name', ColumnSetting::FORMAT_STRING, 'getName'),
new ColumnSetting('ID', ColumnSetting::FORMAT_INTEGER, 'getId'),
];
$generator = new ExcelGenerator();
$file = $generator
->setContent($data)
->setColumns($columns)
->generateSpreadsheet('../var/temp/excelwriter');
$data = [
[
$users[0], //instance of a user entity
'Italy',
new DateTime('NOW')
],
[
$users[1], //instance of a user entity
'France',
new DateTime('NOW')
]
];
//define columns matching the order of the data
$columns = [
new ColumnSetting('Login', ColumnSetting::FORMAT_INTEGER, static function($user) {
return $user->getLoginname();
}),
new ColumnSetting('Country', ColumnSetting::FORMAT_STRING),
new ColumnSetting('Date', ColumnSetting::FORMAT_DATE)
];
//generate
$generator = new ExcelGenerator();
$file = $generator
->setContent($data)
->setColumns($columns)
->generateSpreadsheet('../var/temp/filename');
$repo = $this->getDoctrine()->getRepository(User::class);
$users = $repo->findBy(['client' => 5]);
//define columns matching the order of the data
$columns = [
new ColumnSetting('Login', ColumnSetting::FORMAT_INTEGER, 'getLoginName'),
new ColumnSetting('Country', ColumnSetting::FORMAT_STRING, static function($user) {
return $user->getCountry()->getName();
}),
new ColumnSetting('Created', ColumnSetting::FORMAT_DATE, 'getCreated')
];
//generate
$generator = new ExcelGenerator();
$file = $generator
->setContent($users)
->setColumns($columns)
->generateSpreadsheet('../var/temp/filename');