jacques /
php-reports
| 1 | <?php |
||||||
| 2 | /** |
||||||
| 3 | * @author Jacques Marneweck <[email protected]> |
||||||
| 4 | * @copyright 2018-2019 Jacques Marneweck. All rights strictly reserved. |
||||||
| 5 | */ |
||||||
| 6 | |||||||
| 7 | namespace Jacques\Reports; |
||||||
| 8 | |||||||
| 9 | use PhpOffice\PhpSpreadsheet\IOFactory; |
||||||
| 10 | use PhpOffice\PhpSpreadsheet\Spreadsheet; |
||||||
| 11 | use PhpOffice\PhpSpreadsheet\Writer\Xlsx; |
||||||
| 12 | |||||||
| 13 | class Excel |
||||||
| 14 | { |
||||||
| 15 | /** |
||||||
| 16 | * @var \PhpOffice\PhpSpreadsheet\Spreadsheet|null |
||||||
| 17 | */ |
||||||
| 18 | private $spreadsheet = null; |
||||||
| 19 | |||||||
| 20 | /** |
||||||
| 21 | * @var \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet|null |
||||||
| 22 | */ |
||||||
| 23 | private $sheet = null; |
||||||
| 24 | |||||||
| 25 | /** |
||||||
| 26 | * Active sheet we are working on. |
||||||
| 27 | */ |
||||||
| 28 | private $activesheet = 0; |
||||||
| 29 | |||||||
| 30 | /** |
||||||
| 31 | * Creates a new spreadsheet. |
||||||
| 32 | * |
||||||
| 33 | * @param string $title Title for the Worksheet |
||||||
| 34 | */ |
||||||
| 35 | public function __construct($title) |
||||||
| 36 | { |
||||||
| 37 | $this->spreadsheet = new Spreadsheet(); |
||||||
| 38 | $this->sheet = $this->spreadsheet->getActiveSheet(); |
||||||
| 39 | |||||||
| 40 | $this->sheet->setShowGridlines(true); |
||||||
| 41 | $this->sheet->setTitle($title); |
||||||
| 42 | } |
||||||
| 43 | |||||||
| 44 | /** |
||||||
| 45 | * Creates a new sheet and makes the new sheet the active sheet. |
||||||
| 46 | * |
||||||
| 47 | * @param string $title Title for the Worksheet |
||||||
| 48 | */ |
||||||
| 49 | public function createsheet($title) |
||||||
| 50 | { |
||||||
| 51 | $this->activesheet++; |
||||||
| 52 | |||||||
| 53 | $this->spreadsheet->createsheet(); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 54 | $this->spreadsheet->setActiveSheetIndex($this->activesheet); |
||||||
| 55 | $this->sheet = $this->spreadsheet->getActiveSheet(); |
||||||
| 56 | |||||||
| 57 | $this->sheet->setShowGridlines(true); |
||||||
| 58 | $this->sheet->setTitle($title); |
||||||
| 59 | } |
||||||
| 60 | |||||||
| 61 | /** |
||||||
| 62 | * Retreive the active sheet. |
||||||
| 63 | */ |
||||||
| 64 | public function getActiveSheet() |
||||||
| 65 | { |
||||||
| 66 | return $this->spreadsheet->getActiveSheet(); |
||||||
| 67 | } |
||||||
| 68 | |||||||
| 69 | /** |
||||||
| 70 | * Get properties for the spreadsheet. |
||||||
| 71 | */ |
||||||
| 72 | public function getProperties() |
||||||
| 73 | { |
||||||
| 74 | return $this->spreadsheet->getProperties(); |
||||||
| 75 | } |
||||||
| 76 | |||||||
| 77 | /** |
||||||
| 78 | * Set the active sheet index by the name of the sheet. |
||||||
| 79 | * |
||||||
| 80 | * @param string $name |
||||||
| 81 | */ |
||||||
| 82 | public function setActiveSheetIndexByName($name) |
||||||
| 83 | { |
||||||
| 84 | $this->spreadsheet->setActiveSheetIndexByName($name); |
||||||
| 85 | $this->sheet = $this->spreadsheet->getActiveSheet(); |
||||||
| 86 | } |
||||||
| 87 | |||||||
| 88 | /** |
||||||
| 89 | * Apply header styles over multiple rows making up the header. |
||||||
| 90 | * |
||||||
| 91 | * @param string $cells Columns for the first row (i.e. A1:Z1). |
||||||
| 92 | */ |
||||||
| 93 | public function applyHeaderStyleSingleRow($cells) |
||||||
| 94 | { |
||||||
| 95 | $styleArray = [ |
||||||
| 96 | 'font' => [ |
||||||
| 97 | 'bold' => true, |
||||||
| 98 | ], |
||||||
| 99 | 'borders' => [ |
||||||
| 100 | 'top' => [ |
||||||
| 101 | 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK, |
||||||
| 102 | ], |
||||||
| 103 | 'bottom' => [ |
||||||
| 104 | 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN, |
||||||
| 105 | ], |
||||||
| 106 | ], |
||||||
| 107 | ]; |
||||||
| 108 | |||||||
| 109 | $this->sheet->getStyle($cells)->applyFromArray($styleArray); |
||||||
|
0 ignored issues
–
show
The method
getStyle() does not exist on null.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 110 | } |
||||||
| 111 | |||||||
| 112 | /** |
||||||
| 113 | * Apply header styles over multiple rows making up the header. |
||||||
| 114 | * |
||||||
| 115 | * @param string $firstRowCells Columns for the first row (i.e. A1:Z1). |
||||||
| 116 | * @param string $lastRowCells Columns for the last row of the header (i.e. A2:Z2). |
||||||
| 117 | */ |
||||||
| 118 | public function applyHeaderStylesMultipleRows($firstRowCells, $lastRowCells) |
||||||
| 119 | { |
||||||
| 120 | $styleArray = [ |
||||||
| 121 | 'font' => [ |
||||||
| 122 | 'bold' => true, |
||||||
| 123 | ], |
||||||
| 124 | 'borders' => [ |
||||||
| 125 | 'top' => [ |
||||||
| 126 | 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK, |
||||||
| 127 | ], |
||||||
| 128 | ], |
||||||
| 129 | ]; |
||||||
| 130 | $this->sheet->getStyle($firstRowCells)->applyFromArray($styleArray); |
||||||
| 131 | |||||||
| 132 | $styleArray = [ |
||||||
| 133 | 'font' => [ |
||||||
| 134 | 'bold' => true, |
||||||
| 135 | ], |
||||||
| 136 | 'borders' => [ |
||||||
| 137 | 'bottom' => [ |
||||||
| 138 | 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN, |
||||||
| 139 | ], |
||||||
| 140 | ], |
||||||
| 141 | ]; |
||||||
| 142 | $this->sheet->getStyle($lastRowCells)->applyFromArray($styleArray); |
||||||
| 143 | } |
||||||
| 144 | |||||||
| 145 | /** |
||||||
| 146 | * Set the auto size property on a column for the column width. |
||||||
| 147 | * |
||||||
| 148 | * @param string $firstCell First column (i.e. A) |
||||||
| 149 | * @param string $lastcell Last column (i.e. Z) |
||||||
| 150 | */ |
||||||
| 151 | public function applyAutoSize($firstCell, $lastCell) |
||||||
| 152 | { |
||||||
| 153 | $col = $firstCell; |
||||||
| 154 | $lastCell++; |
||||||
| 155 | |||||||
| 156 | while ($col != $lastCell) { |
||||||
| 157 | $this->sheet->getColumnDimension($col)->setAutoSize(true); |
||||||
| 158 | $col++; |
||||||
| 159 | } |
||||||
| 160 | } |
||||||
| 161 | |||||||
| 162 | /** |
||||||
| 163 | * Save the spreadsheet. |
||||||
| 164 | * |
||||||
| 165 | * @param string $filename |
||||||
| 166 | */ |
||||||
| 167 | public function save($filename) |
||||||
| 168 | { |
||||||
| 169 | $writer = IOFactory::createWriter($this->spreadsheet, 'Xlsx'); |
||||||
|
0 ignored issues
–
show
It seems like
$this->spreadsheet can also be of type null; however, parameter $spreadsheet of PhpOffice\PhpSpreadsheet\IOFactory::createWriter() does only seem to accept PhpOffice\PhpSpreadsheet\Spreadsheet, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 170 | $writer->save($filename); |
||||||
| 171 | } |
||||||
| 172 | |||||||
| 173 | public function __call($name, $args) |
||||||
| 174 | { |
||||||
| 175 | return (call_user_func_array(array($this->sheet, $name), $args)); |
||||||
| 176 | } |
||||||
| 177 | } |
||||||
| 178 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.