|
| 1 | +<?php |
| 2 | + |
| 3 | +use PhpOffice\PhpSpreadsheet\Spreadsheet; |
| 4 | +use PhpOffice\PhpSpreadsheet\Worksheet\Table; |
| 5 | + |
| 6 | +require __DIR__ . '/../Header.php'; |
| 7 | + |
| 8 | +// Create new Spreadsheet object |
| 9 | +$helper->log('Create new Spreadsheet object'); |
| 10 | +$spreadsheet = new Spreadsheet(); |
| 11 | + |
| 12 | +// Set document properties |
| 13 | +$helper->log('Set document properties'); |
| 14 | +$spreadsheet->getProperties()->setCreator('aswinkumar863') |
| 15 | + ->setLastModifiedBy('aswinkumar863') |
| 16 | + ->setTitle('PhpSpreadsheet Table Test Document') |
| 17 | + ->setSubject('PhpSpreadsheet Table Test Document') |
| 18 | + ->setDescription('Test document for PhpSpreadsheet, generated using PHP classes.') |
| 19 | + ->setKeywords('office PhpSpreadsheet php') |
| 20 | + ->setCategory('Table'); |
| 21 | + |
| 22 | +// Create the worksheet |
| 23 | +$helper->log('Add data'); |
| 24 | + |
| 25 | +$spreadsheet->setActiveSheetIndex(0); |
| 26 | + |
| 27 | +$columnFormula = '=SUM(Sales_Data[[#This Row],[Q1]:[Q4]])'; |
| 28 | + |
| 29 | +$dataArray = [ |
| 30 | + ['Year', 'Country', 'Q1', 'Q2', 'Q3', 'Q4', 'Sales'], |
| 31 | + [2010, 'Belgium', 380, 390, 420, 460, $columnFormula], |
| 32 | + [2010, 'France', 510, 490, 460, 590, $columnFormula], |
| 33 | + [2010, 'Germany', 720, 680, 640, 660, $columnFormula], |
| 34 | + [2010, 'Italy', 440, 410, 420, 450, $columnFormula], |
| 35 | + [2010, 'Spain', 510, 490, 470, 420, $columnFormula], |
| 36 | + [2010, 'UK', 690, 610, 620, 600, $columnFormula], |
| 37 | + [2010, 'United States', 790, 730, 860, 850, $columnFormula], |
| 38 | + [2011, 'Belgium', 400, 350, 450, 500, $columnFormula], |
| 39 | + [2011, 'France', 620, 650, 415, 570, $columnFormula], |
| 40 | + [2011, 'Germany', 680, 620, 710, 690, $columnFormula], |
| 41 | + [2011, 'Italy', 430, 370, 350, 335, $columnFormula], |
| 42 | + [2011, 'Spain', 460, 390, 430, 415, $columnFormula], |
| 43 | + [2011, 'UK', 720, 650, 580, 510, $columnFormula], |
| 44 | + [2011, 'United States', 800, 700, 900, 950, $columnFormula], |
| 45 | +]; |
| 46 | + |
| 47 | +$spreadsheet->getActiveSheet()->fromArray($dataArray, null, 'A1'); |
| 48 | + |
| 49 | +// Create Table |
| 50 | +$helper->log('Create Table'); |
| 51 | +$table = new Table('A1:G16', 'Sales_Data'); // +1 row for totalsRow |
| 52 | +$table->setShowTotalsRow(true); |
| 53 | +$table->setRange('A1:G16'); // +1 row for totalsRow |
| 54 | + |
| 55 | +// Set Column Formula |
| 56 | +$table->getColumn('G')->setColumnFormula($columnFormula); |
| 57 | + |
| 58 | +$helper->log('Add Totals Row'); |
| 59 | +// Table column label not implemented yet, |
| 60 | +$table->getColumn('A')->setTotalsRowLabel('Total'); |
| 61 | +// So set the label directly to the cell |
| 62 | +$spreadsheet->getActiveSheet()->getCell('A16')->setValue('Total'); |
| 63 | + |
| 64 | +// Table column function not implemented yet, |
| 65 | +$table->getColumn('G')->setTotalsRowFunction('sum'); |
| 66 | +// So set the formula directly to the cell |
| 67 | +$spreadsheet->getActiveSheet()->getCell('G16')->setValue('=SUBTOTAL(109,Sales_Data[Sales])'); |
| 68 | + |
| 69 | +// Table column function not implemented yet, |
| 70 | +$table->getColumn('C')->setTotalsRowFunction('sum'); |
| 71 | +$table->getColumn('D')->setTotalsRowFunction('sum'); |
| 72 | +$table->getColumn('E')->setTotalsRowFunction('sum'); |
| 73 | +$table->getColumn('F')->setTotalsRowFunction('sum'); |
| 74 | +// So set the formulae directly to the cells |
| 75 | +$spreadsheet->getActiveSheet()->getCell('C16')->setValue('=SUBTOTAL(109,Sales_Data[Q1])'); |
| 76 | +$spreadsheet->getActiveSheet()->getCell('D16')->setValue('=SUBTOTAL(109,Sales_Data[Q2])'); |
| 77 | +$spreadsheet->getActiveSheet()->getCell('E16')->setValue('=SUBTOTAL(109,Sales_Data[Q3])'); |
| 78 | +$spreadsheet->getActiveSheet()->getCell('F16')->setValue('=SUBTOTAL(109,Sales_Data[Q4])'); |
| 79 | + |
| 80 | +// Add Table to Worksheet |
| 81 | +$helper->log('Add Table to Worksheet'); |
| 82 | +$spreadsheet->getActiveSheet()->addTable($table); |
| 83 | + |
| 84 | +$helper->displayGrid($spreadsheet->getActiveSheet()->toArray(null, false, true, true)); |
| 85 | + |
| 86 | +$helper->log('Calculate Structured References'); |
| 87 | + |
| 88 | +$helper->displayGrid($spreadsheet->getActiveSheet()->toArray(null, true, true, true)); |
| 89 | + |
| 90 | +// Save |
| 91 | +$helper->write($spreadsheet, __FILE__, ['Xlsx']); |
0 commit comments