Created
October 12, 2021 12:06
-
-
Save qant/31704a63b0fea4bde4174fb5fd7a9bbd to your computer and use it in GitHub Desktop.
Excel and pdf example using php
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
| { | |
| "require": { | |
| "phpoffice/phpspreadsheet": "^1.18", | |
| "mpdf/mpdf": "^8.0" | |
| } | |
| } |
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 | |
| //first composer require phpoffice/phpspreadsheet mpdf/mpdf | |
| //or use my composer.json and composer install | |
| require 'vendor/autoload.php'; | |
| use PhpOffice\PhpSpreadsheet\Spreadsheet; | |
| use PhpOffice\PhpSpreadsheet\Writer\Xlsx; | |
| use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf; | |
| $spreadsheet = new Spreadsheet(); | |
| $sheet = $spreadsheet->getActiveSheet(); | |
| $spreadsheet->getDefaultStyle()->getFont()->setName('Arial'); | |
| $spreadsheet->getDefaultStyle()->getFont()->setSize(8); | |
| //headers | |
| $sheet->setCellValue('A1', 'date'); | |
| $sheet->setCellValue('B1', 'name'); | |
| $sheet->setCellValue('C1', 'address'); | |
| //cells | |
| $sheet->setCellValue('A2', date('Y-m-d',time())); | |
| $sheet->setCellValue('A3', date('Y-m-d',time())); | |
| $sheet->setCellValue('A4', date('Y-m-d',time())); | |
| $sheet->getStyle('A2:A4')->getNumberFormat() | |
| ->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_YYYYMMDDSLASH); | |
| $sheet->setCellValue('B2', 'AN sem1'); | |
| $sheet->setCellValue('B3', 'ann aaa'); | |
| $sheet->setCellValue('B4', 'asasa yyy cxc'); | |
| $sheet->setCellValue('C2', 'calle blabla 12'); | |
| $sheet->setCellValue('C3', 'calle blabla 122'); | |
| $sheet->setCellValue('C4', 'calle blabla 122'); | |
| //cell filter | |
| $sheet->setAutoFilter('A1:C1'); | |
| //cell sizes | |
| $sheet->getRowDimension('1')->setRowHeight(20, 'px'); | |
| $sheet->getColumnDimension('A')->setWidth(20, 'px'); | |
| $sheet->getColumnDimension('B')->setWidth(50, 'px'); | |
| $sheet->getColumnDimension('C')->setWidth(50, 'px'); | |
| $styleArray = [ | |
| 'font' => [ | |
| 'bold' => false, | |
| ], | |
| 'alignment' => [ | |
| 'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT, | |
| ], | |
| /*'borders' => [ | |
| 'top' => [ | |
| 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN, | |
| ], | |
| ],*/ | |
| 'borders' => [ | |
| //https://phpspreadsheet.readthedocs.io/en/latest/topics/images/08-styling-border-options.png | |
| 'allBorders' => [ | |
| 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN, | |
| 'color' => ['argb' => '00000000'], | |
| ], | |
| ], | |
| ]; | |
| $sheet->getStyle('A2:C4')->applyFromArray($styleArray); | |
| $writer = new Xlsx($spreadsheet); | |
| $writer->save('demo.xlsx'); | |
| $writer_pdf = new Mpdf($spreadsheet); | |
| $writer_pdf->save("demo.pdf"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment