sampleSpreadsheet.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <?php
  2. // Create new Spreadsheet object
  3. use PhpOffice\PhpSpreadsheet\RichText\RichText;
  4. use PhpOffice\PhpSpreadsheet\Shared\Date;
  5. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  6. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  7. use PhpOffice\PhpSpreadsheet\Style\Border;
  8. use PhpOffice\PhpSpreadsheet\Style\Color;
  9. use PhpOffice\PhpSpreadsheet\Style\Fill;
  10. use PhpOffice\PhpSpreadsheet\Style\Font;
  11. use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
  12. use PhpOffice\PhpSpreadsheet\Style\Protection;
  13. use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
  14. use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
  15. $helper->log('Create new Spreadsheet object');
  16. $spreadsheet = new Spreadsheet();
  17. // Set document properties
  18. $helper->log('Set document properties');
  19. $spreadsheet->getProperties()->setCreator('Maarten Balliauw')
  20. ->setLastModifiedBy('Maarten Balliauw')
  21. ->setTitle('Office 2007 XLSX Test Document')
  22. ->setSubject('Office 2007 XLSX Test Document')
  23. ->setDescription('Test document for Office 2007 XLSX, generated using PHP classes.')
  24. ->setKeywords('office 2007 openxml php')
  25. ->setCategory('Test result file');
  26. // Create a first sheet, representing sales data
  27. $helper->log('Add some data');
  28. $spreadsheet->setActiveSheetIndex(0);
  29. $spreadsheet->getActiveSheet()->setCellValue('B1', 'Invoice');
  30. $spreadsheet->getActiveSheet()->setCellValue('D1', Date::PHPToExcel(gmmktime(0, 0, 0, date('m'), date('d'), date('Y'))));
  31. $spreadsheet->getActiveSheet()->getStyle('D1')->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_DATE_XLSX15);
  32. $spreadsheet->getActiveSheet()->setCellValue('E1', '#12566');
  33. $spreadsheet->getActiveSheet()->setCellValue('A3', 'Product Id');
  34. $spreadsheet->getActiveSheet()->setCellValue('B3', 'Description');
  35. $spreadsheet->getActiveSheet()->setCellValue('C3', 'Price');
  36. $spreadsheet->getActiveSheet()->setCellValue('D3', 'Amount');
  37. $spreadsheet->getActiveSheet()->setCellValue('E3', 'Total');
  38. $spreadsheet->getActiveSheet()->setCellValue('A4', '1001');
  39. $spreadsheet->getActiveSheet()->setCellValue('B4', 'PHP for dummies');
  40. $spreadsheet->getActiveSheet()->setCellValue('C4', '20');
  41. $spreadsheet->getActiveSheet()->setCellValue('D4', '1');
  42. $spreadsheet->getActiveSheet()->setCellValue('E4', '=IF(D4<>"",C4*D4,"")');
  43. $spreadsheet->getActiveSheet()->setCellValue('A5', '1012');
  44. $spreadsheet->getActiveSheet()->setCellValue('B5', 'OpenXML for dummies');
  45. $spreadsheet->getActiveSheet()->setCellValue('C5', '22');
  46. $spreadsheet->getActiveSheet()->setCellValue('D5', '2');
  47. $spreadsheet->getActiveSheet()->setCellValue('E5', '=IF(D5<>"",C5*D5,"")');
  48. $spreadsheet->getActiveSheet()->setCellValue('E6', '=IF(D6<>"",C6*D6,"")');
  49. $spreadsheet->getActiveSheet()->setCellValue('E7', '=IF(D7<>"",C7*D7,"")');
  50. $spreadsheet->getActiveSheet()->setCellValue('E8', '=IF(D8<>"",C8*D8,"")');
  51. $spreadsheet->getActiveSheet()->setCellValue('E9', '=IF(D9<>"",C9*D9,"")');
  52. $spreadsheet->getActiveSheet()->setCellValue('D11', 'Total excl.:');
  53. $spreadsheet->getActiveSheet()->setCellValue('E11', '=SUM(E4:E9)');
  54. $spreadsheet->getActiveSheet()->setCellValue('D12', 'VAT:');
  55. $spreadsheet->getActiveSheet()->setCellValue('E12', '=E11*0.21');
  56. $spreadsheet->getActiveSheet()->setCellValue('D13', 'Total incl.:');
  57. $spreadsheet->getActiveSheet()->setCellValue('E13', '=E11+E12');
  58. // Add comment
  59. $helper->log('Add comments');
  60. $spreadsheet->getActiveSheet()->getComment('E11')->setAuthor('PhpSpreadsheet');
  61. $commentRichText = $spreadsheet->getActiveSheet()->getComment('E11')->getText()->createTextRun('PhpSpreadsheet:');
  62. $commentRichText->getFont()->setBold(true);
  63. $spreadsheet->getActiveSheet()->getComment('E11')->getText()->createTextRun("\r\n");
  64. $spreadsheet->getActiveSheet()->getComment('E11')->getText()->createTextRun('Total amount on the current invoice, excluding VAT.');
  65. $spreadsheet->getActiveSheet()->getComment('E12')->setAuthor('PhpSpreadsheet');
  66. $commentRichText = $spreadsheet->getActiveSheet()->getComment('E12')->getText()->createTextRun('PhpSpreadsheet:');
  67. $commentRichText->getFont()->setBold(true);
  68. $spreadsheet->getActiveSheet()->getComment('E12')->getText()->createTextRun("\r\n");
  69. $spreadsheet->getActiveSheet()->getComment('E12')->getText()->createTextRun('Total amount of VAT on the current invoice.');
  70. $spreadsheet->getActiveSheet()->getComment('E13')->setAuthor('PhpSpreadsheet');
  71. $commentRichText = $spreadsheet->getActiveSheet()->getComment('E13')->getText()->createTextRun('PhpSpreadsheet:');
  72. $commentRichText->getFont()->setBold(true);
  73. $spreadsheet->getActiveSheet()->getComment('E13')->getText()->createTextRun("\r\n");
  74. $spreadsheet->getActiveSheet()->getComment('E13')->getText()->createTextRun('Total amount on the current invoice, including VAT.');
  75. $spreadsheet->getActiveSheet()->getComment('E13')->setWidth('100pt');
  76. $spreadsheet->getActiveSheet()->getComment('E13')->setHeight('100pt');
  77. $spreadsheet->getActiveSheet()->getComment('E13')->setMarginLeft('150pt');
  78. $spreadsheet->getActiveSheet()->getComment('E13')->getFillColor()->setRGB('EEEEEE');
  79. // Add rich-text string
  80. $helper->log('Add rich-text string');
  81. $richText = new RichText();
  82. $richText->createText('This invoice is ');
  83. $payable = $richText->createTextRun('payable within thirty days after the end of the month');
  84. $payable->getFont()->setBold(true);
  85. $payable->getFont()->setItalic(true);
  86. $payable->getFont()->setColor(new Color(Color::COLOR_DARKGREEN));
  87. $richText->createText(', unless specified otherwise on the invoice.');
  88. $spreadsheet->getActiveSheet()->getCell('A18')->setValue($richText);
  89. // Merge cells
  90. $helper->log('Merge cells');
  91. $spreadsheet->getActiveSheet()->mergeCells('A18:E22');
  92. $spreadsheet->getActiveSheet()->mergeCells('A28:B28'); // Just to test...
  93. $spreadsheet->getActiveSheet()->unmergeCells('A28:B28'); // Just to test...
  94. // Protect cells
  95. $helper->log('Protect cells');
  96. $spreadsheet->getActiveSheet()->getProtection()->setSheet(true); // Needs to be set to true in order to enable any worksheet protection!
  97. $spreadsheet->getActiveSheet()->protectCells('A3:E13', 'PhpSpreadsheet');
  98. // Set cell number formats
  99. $helper->log('Set cell number formats');
  100. $spreadsheet->getActiveSheet()->getStyle('E4:E13')->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE);
  101. // Set column widths
  102. $helper->log('Set column widths');
  103. $spreadsheet->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
  104. $spreadsheet->getActiveSheet()->getColumnDimension('D')->setWidth(12);
  105. $spreadsheet->getActiveSheet()->getColumnDimension('E')->setWidth(12);
  106. // Set fonts
  107. $helper->log('Set fonts');
  108. $spreadsheet->getActiveSheet()->getStyle('B1')->getFont()->setName('Candara');
  109. $spreadsheet->getActiveSheet()->getStyle('B1')->getFont()->setSize(20);
  110. $spreadsheet->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
  111. $spreadsheet->getActiveSheet()->getStyle('B1')->getFont()->setUnderline(Font::UNDERLINE_SINGLE);
  112. $spreadsheet->getActiveSheet()->getStyle('B1')->getFont()->getColor()->setARGB(Color::COLOR_WHITE);
  113. $spreadsheet->getActiveSheet()->getStyle('D1')->getFont()->getColor()->setARGB(Color::COLOR_WHITE);
  114. $spreadsheet->getActiveSheet()->getStyle('E1')->getFont()->getColor()->setARGB(Color::COLOR_WHITE);
  115. $spreadsheet->getActiveSheet()->getStyle('D13')->getFont()->setBold(true);
  116. $spreadsheet->getActiveSheet()->getStyle('E13')->getFont()->setBold(true);
  117. // Set alignments
  118. $helper->log('Set alignments');
  119. $spreadsheet->getActiveSheet()->getStyle('D11')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_RIGHT);
  120. $spreadsheet->getActiveSheet()->getStyle('D12')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_RIGHT);
  121. $spreadsheet->getActiveSheet()->getStyle('D13')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_RIGHT);
  122. $spreadsheet->getActiveSheet()->getStyle('A18')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_JUSTIFY);
  123. $spreadsheet->getActiveSheet()->getStyle('A18')->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
  124. $spreadsheet->getActiveSheet()->getStyle('B5')->getAlignment()->setShrinkToFit(true);
  125. // Set thin black border outline around column
  126. $helper->log('Set thin black border outline around column');
  127. $styleThinBlackBorderOutline = [
  128. 'borders' => [
  129. 'outline' => [
  130. 'borderStyle' => Border::BORDER_THIN,
  131. 'color' => ['argb' => 'FF000000'],
  132. ],
  133. ],
  134. ];
  135. $spreadsheet->getActiveSheet()->getStyle('A4:E10')->applyFromArray($styleThinBlackBorderOutline);
  136. // Set thick brown border outline around "Total"
  137. $helper->log('Set thick brown border outline around Total');
  138. $styleThickBrownBorderOutline = [
  139. 'borders' => [
  140. 'outline' => [
  141. 'borderStyle' => Border::BORDER_THICK,
  142. 'color' => ['argb' => 'FF993300'],
  143. ],
  144. ],
  145. ];
  146. $spreadsheet->getActiveSheet()->getStyle('D13:E13')->applyFromArray($styleThickBrownBorderOutline);
  147. // Set fills
  148. $helper->log('Set fills');
  149. $spreadsheet->getActiveSheet()->getStyle('A1:E1')->getFill()->setFillType(Fill::FILL_SOLID);
  150. $spreadsheet->getActiveSheet()->getStyle('A1:E1')->getFill()->getStartColor()->setARGB('FF808080');
  151. // Set style for header row using alternative method
  152. $helper->log('Set style for header row using alternative method');
  153. $spreadsheet->getActiveSheet()->getStyle('A3:E3')->applyFromArray(
  154. [
  155. 'font' => [
  156. 'bold' => true,
  157. ],
  158. 'alignment' => [
  159. 'horizontal' => Alignment::HORIZONTAL_RIGHT,
  160. ],
  161. 'borders' => [
  162. 'top' => [
  163. 'borderStyle' => Border::BORDER_THIN,
  164. ],
  165. ],
  166. 'fill' => [
  167. 'fillType' => Fill::FILL_GRADIENT_LINEAR,
  168. 'rotation' => 90,
  169. 'startColor' => [
  170. 'argb' => 'FFA0A0A0',
  171. ],
  172. 'endColor' => [
  173. 'argb' => 'FFFFFFFF',
  174. ],
  175. ],
  176. ]
  177. );
  178. $spreadsheet->getActiveSheet()->getStyle('A3')->applyFromArray(
  179. [
  180. 'alignment' => [
  181. 'horizontal' => Alignment::HORIZONTAL_LEFT,
  182. ],
  183. 'borders' => [
  184. 'left' => [
  185. 'borderStyle' => Border::BORDER_THIN,
  186. ],
  187. ],
  188. ]
  189. );
  190. $spreadsheet->getActiveSheet()->getStyle('B3')->applyFromArray(
  191. [
  192. 'alignment' => [
  193. 'horizontal' => Alignment::HORIZONTAL_LEFT,
  194. ],
  195. ]
  196. );
  197. $spreadsheet->getActiveSheet()->getStyle('E3')->applyFromArray(
  198. [
  199. 'borders' => [
  200. 'right' => [
  201. 'borderStyle' => Border::BORDER_THIN,
  202. ],
  203. ],
  204. ]
  205. );
  206. // Unprotect a cell
  207. $helper->log('Unprotect a cell');
  208. $spreadsheet->getActiveSheet()->getStyle('B1')->getProtection()->setLocked(Protection::PROTECTION_UNPROTECTED);
  209. // Add a hyperlink to the sheet
  210. $helper->log('Add a hyperlink to an external website');
  211. $spreadsheet->getActiveSheet()->setCellValue('E26', 'www.phpexcel.net');
  212. $spreadsheet->getActiveSheet()->getCell('E26')->getHyperlink()->setUrl('https://www.example.com');
  213. $spreadsheet->getActiveSheet()->getCell('E26')->getHyperlink()->setTooltip('Navigate to website');
  214. $spreadsheet->getActiveSheet()->getStyle('E26')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_RIGHT);
  215. $helper->log('Add a hyperlink to another cell on a different worksheet within the workbook');
  216. $spreadsheet->getActiveSheet()->setCellValue('E27', 'Terms and conditions');
  217. $spreadsheet->getActiveSheet()->getCell('E27')->getHyperlink()->setUrl("sheet://'Terms and conditions'!A1");
  218. $spreadsheet->getActiveSheet()->getCell('E27')->getHyperlink()->setTooltip('Review terms and conditions');
  219. $spreadsheet->getActiveSheet()->getStyle('E27')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_RIGHT);
  220. // Add a drawing to the worksheet
  221. $helper->log('Add a drawing to the worksheet');
  222. $drawing = new Drawing();
  223. $drawing->setName('Logo');
  224. $drawing->setDescription('Logo');
  225. $drawing->setPath(__DIR__ . '/../images/officelogo.jpg');
  226. $drawing->setHeight(36);
  227. $drawing->setWorksheet($spreadsheet->getActiveSheet());
  228. // Add a drawing to the worksheet
  229. $helper->log('Add a drawing to the worksheet');
  230. $drawing = new Drawing();
  231. $drawing->setName('Paid');
  232. $drawing->setDescription('Paid');
  233. $drawing->setPath(__DIR__ . '/../images/paid.png');
  234. $drawing->setCoordinates('B15');
  235. $drawing->setOffsetX(110);
  236. $drawing->setRotation(25);
  237. $drawing->getShadow()->setVisible(true);
  238. $drawing->getShadow()->setDirection(45);
  239. $drawing->setWorksheet($spreadsheet->getActiveSheet());
  240. // Add a drawing to the worksheet
  241. $helper->log('Add a drawing to the worksheet');
  242. $drawing = new Drawing();
  243. $drawing->setName('PhpSpreadsheet logo');
  244. $drawing->setDescription('PhpSpreadsheet logo');
  245. $drawing->setPath(__DIR__ . '/../images/PhpSpreadsheet_logo.png');
  246. $drawing->setHeight(36);
  247. $drawing->setCoordinates('D24');
  248. $drawing->setOffsetX(10);
  249. $drawing->setWorksheet($spreadsheet->getActiveSheet());
  250. // Play around with inserting and removing rows and columns
  251. $helper->log('Play around with inserting and removing rows and columns');
  252. $spreadsheet->getActiveSheet()->insertNewRowBefore(6, 10);
  253. $spreadsheet->getActiveSheet()->removeRow(6, 10);
  254. $spreadsheet->getActiveSheet()->insertNewColumnBefore('E', 5);
  255. $spreadsheet->getActiveSheet()->removeColumn('E', 5);
  256. // Set header and footer. When no different headers for odd/even are used, odd header is assumed.
  257. $helper->log('Set header/footer');
  258. $spreadsheet->getActiveSheet()->getHeaderFooter()->setOddHeader('&L&BInvoice&RPrinted on &D');
  259. $spreadsheet->getActiveSheet()->getHeaderFooter()->setOddFooter('&L&B' . $spreadsheet->getProperties()->getTitle() . '&RPage &P of &N');
  260. // Set page orientation and size
  261. $helper->log('Set page orientation and size');
  262. $spreadsheet->getActiveSheet()->getPageSetup()->setOrientation(PageSetup::ORIENTATION_PORTRAIT);
  263. $spreadsheet->getActiveSheet()->getPageSetup()->setPaperSize(PageSetup::PAPERSIZE_A4);
  264. // Rename first worksheet
  265. $helper->log('Rename first worksheet');
  266. $spreadsheet->getActiveSheet()->setTitle('Invoice');
  267. // Create a new worksheet, after the default sheet
  268. $helper->log('Create a second Worksheet object');
  269. $spreadsheet->createSheet();
  270. // Llorem ipsum...
  271. $sLloremIpsum = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vivamus eget ante. Sed cursus nunc semper tortor. Aliquam luctus purus non elit. Fusce vel elit commodo sapien dignissim dignissim. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Curabitur accumsan magna sed massa. Nullam bibendum quam ac ipsum. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Proin augue. Praesent malesuada justo sed orci. Pellentesque lacus ligula, sodales quis, ultricies a, ultricies vitae, elit. Sed luctus consectetuer dolor. Vivamus vel sem ut nisi sodales accumsan. Nunc et felis. Suspendisse semper viverra odio. Morbi at odio. Integer a orci a purus venenatis molestie. Nam mattis. Praesent rhoncus, nisi vel mattis auctor, neque nisi faucibus sem, non dapibus elit pede ac nisl. Cras turpis.';
  272. // Add some data to the second sheet, resembling some different data types
  273. $helper->log('Add some data');
  274. $spreadsheet->setActiveSheetIndex(1);
  275. $spreadsheet->getActiveSheet()->setCellValue('A1', 'Terms and conditions');
  276. $spreadsheet->getActiveSheet()->setCellValue('A3', $sLloremIpsum);
  277. $spreadsheet->getActiveSheet()->setCellValue('A4', $sLloremIpsum);
  278. $spreadsheet->getActiveSheet()->setCellValue('A5', $sLloremIpsum);
  279. $spreadsheet->getActiveSheet()->setCellValue('A6', $sLloremIpsum);
  280. // Set the worksheet tab color
  281. $helper->log('Set the worksheet tab color');
  282. $spreadsheet->getActiveSheet()->getTabColor()->setARGB('FF0094FF');
  283. // Set alignments
  284. $helper->log('Set alignments');
  285. $spreadsheet->getActiveSheet()->getStyle('A3:A6')->getAlignment()->setWrapText(true);
  286. // Set column widths
  287. $helper->log('Set column widths');
  288. $spreadsheet->getActiveSheet()->getColumnDimension('A')->setWidth(80);
  289. // Set fonts
  290. $helper->log('Set fonts');
  291. $spreadsheet->getActiveSheet()->getStyle('A1')->getFont()->setName('Candara');
  292. $spreadsheet->getActiveSheet()->getStyle('A1')->getFont()->setSize(20);
  293. $spreadsheet->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
  294. $spreadsheet->getActiveSheet()->getStyle('A1')->getFont()->setUnderline(Font::UNDERLINE_SINGLE);
  295. $spreadsheet->getActiveSheet()->getStyle('A3:A6')->getFont()->setSize(8);
  296. // Add a drawing to the worksheet
  297. $helper->log('Add a drawing to the worksheet');
  298. $drawing = new Drawing();
  299. $drawing->setName('Terms and conditions');
  300. $drawing->setDescription('Terms and conditions');
  301. $drawing->setPath(__DIR__ . '/../images/termsconditions.jpg');
  302. $drawing->setCoordinates('B14');
  303. $drawing->setWorksheet($spreadsheet->getActiveSheet());
  304. // Set page orientation and size
  305. $helper->log('Set page orientation and size');
  306. $spreadsheet->getActiveSheet()->getPageSetup()->setOrientation(PageSetup::ORIENTATION_LANDSCAPE);
  307. $spreadsheet->getActiveSheet()->getPageSetup()->setPaperSize(PageSetup::PAPERSIZE_A4);
  308. // Rename second worksheet
  309. $helper->log('Rename second worksheet');
  310. $spreadsheet->getActiveSheet()->setTitle('Terms and conditions');
  311. // Set active sheet index to the first sheet, so Excel opens this as the first sheet
  312. $spreadsheet->setActiveSheetIndex(0);
  313. return $spreadsheet;