Created
October 10, 2018 15:08
-
-
Save jefersondaniel/41cb56f5370b40e253e2855b8546a780 to your computer and use it in GitHub Desktop.
Php Spreadsheet Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* composer require phpoffice/phpspreadsheet "^1.3" | |
*/ | |
require __DIR__ . '/vendor/autoload.php'; | |
use PhpOffice\PhpSpreadsheet\Spreadsheet; | |
use PhpOffice\PhpSpreadsheet\Style\Alignment; | |
use PhpOffice\PhpSpreadsheet\Style\Fill; | |
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; | |
use PhpOffice\PhpSpreadsheet\Writer\Xlsx; | |
$spreadsheet = new Spreadsheet(); | |
$spreadsheet->disconnectWorksheets(); | |
$worksheet = new Worksheet(null, 'Title'); | |
$worksheet->fromArray([ | |
['Title'], | |
['Column X', 'Column Y'], | |
['Value X1', 'Value Y1'], | |
]); | |
$spreadsheet->addSheet($worksheet); | |
$columns = 2; | |
for ($column = 0; $column < $columns; $column++) { | |
$letter = chr(ord('A') + $column); | |
$worksheet->getColumnDimension($letter)->setAutoSize(true); | |
} | |
$lastColumn = chr(ord('A') + ($columns - 1)); | |
$worksheet->mergeCells('A1:' . $lastColumn . '1'); | |
$worksheet->getStyle('A1')->applyFromArray([ | |
'font' => [ | |
'size' => 12, | |
'bold' => true, | |
], | |
'fill' => [ | |
'fillType' => Fill::FILL_SOLID, | |
'color' => array('argb' => 'FFB2B2B2') | |
], | |
'alignment' => [ | |
'vertical' => Alignment::VERTICAL_CENTER, | |
'horizontal' => Alignment::HORIZONTAL_CENTER | |
] | |
]); | |
$worksheet->getStyle('A2:' . $lastColumn . '2')->applyFromArray([ | |
'font' => [ | |
'size' => 11, | |
'bold' => true, | |
], | |
'fill' => [ | |
'fillType' => Fill::FILL_SOLID, | |
'color' => array('argb' => 'FFDDDDDD') | |
], | |
'alignment' => [ | |
'vertical' => Alignment::VERTICAL_CENTER, | |
'horizontal' => Alignment::HORIZONTAL_CENTER | |
] | |
]); | |
$writer = new Xlsx($spreadsheet); | |
$writer->save('lala.xlsx'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment