29_Advanced_value_binder.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\Cell\AdvancedValueBinder;
  3. use PhpOffice\PhpSpreadsheet\Cell\Cell;
  4. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  5. require __DIR__ . '/../Header.php';
  6. // Set timezone
  7. $helper->log('Set timezone');
  8. date_default_timezone_set('UTC');
  9. // Set value binder
  10. $helper->log('Set value binder');
  11. Cell::setValueBinder(new AdvancedValueBinder());
  12. // Create new Spreadsheet object
  13. $helper->log('Create new Spreadsheet object');
  14. $spreadsheet = new Spreadsheet();
  15. // Set document properties
  16. $helper->log('Set document properties');
  17. $spreadsheet->getProperties()->setCreator('Maarten Balliauw')
  18. ->setLastModifiedBy('Maarten Balliauw')
  19. ->setTitle('Office 2007 XLSX Test Document')
  20. ->setSubject('Office 2007 XLSX Test Document')
  21. ->setDescription('Test document for Office 2007 XLSX, generated using PHP classes.')
  22. ->setKeywords('office 2007 openxml php')
  23. ->setCategory('Test result file');
  24. // Set default font
  25. $helper->log('Set default font');
  26. $spreadsheet->getDefaultStyle()->getFont()->setName('Arial');
  27. $spreadsheet->getDefaultStyle()->getFont()->setSize(10);
  28. // Set column widths
  29. $helper->log('Set column widths');
  30. $spreadsheet->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
  31. $spreadsheet->getActiveSheet()->getColumnDimension('B')->setWidth(14);
  32. // Add some data, resembling some different data types
  33. $helper->log('Add some data');
  34. $spreadsheet->getActiveSheet()->setCellValue('A1', 'String value:')
  35. ->setCellValue('B1', 'Mark Baker');
  36. $spreadsheet->getActiveSheet()->setCellValue('A2', 'Numeric value #1:')
  37. ->setCellValue('B2', 12345);
  38. $spreadsheet->getActiveSheet()->setCellValue('A3', 'Numeric value #2:')
  39. ->setCellValue('B3', -12.345);
  40. $spreadsheet->getActiveSheet()->setCellValue('A4', 'Numeric value #3:')
  41. ->setCellValue('B4', .12345);
  42. $spreadsheet->getActiveSheet()->setCellValue('A5', 'Numeric value #4:')
  43. ->setCellValue('B5', '12345');
  44. $spreadsheet->getActiveSheet()->setCellValue('A6', 'Numeric value #5:')
  45. ->setCellValue('B6', '1.2345');
  46. $spreadsheet->getActiveSheet()->setCellValue('A7', 'Numeric value #6:')
  47. ->setCellValue('B7', '.12345');
  48. $spreadsheet->getActiveSheet()->setCellValue('A8', 'Numeric value #7:')
  49. ->setCellValue('B8', '1.234e-5');
  50. $spreadsheet->getActiveSheet()->setCellValue('A9', 'Numeric value #8:')
  51. ->setCellValue('B9', '-1.234e+5');
  52. $spreadsheet->getActiveSheet()->setCellValue('A10', 'Boolean value:')
  53. ->setCellValue('B10', 'TRUE');
  54. $spreadsheet->getActiveSheet()->setCellValue('A11', 'Percentage value #1:')
  55. ->setCellValue('B11', '10%');
  56. $spreadsheet->getActiveSheet()->setCellValue('A12', 'Percentage value #2:')
  57. ->setCellValue('B12', '12.5%');
  58. $spreadsheet->getActiveSheet()->setCellValue('A13', 'Fraction value #1:')
  59. ->setCellValue('B13', '-1/2');
  60. $spreadsheet->getActiveSheet()->setCellValue('A14', 'Fraction value #2:')
  61. ->setCellValue('B14', '3 1/2');
  62. $spreadsheet->getActiveSheet()->setCellValue('A15', 'Fraction value #3:')
  63. ->setCellValue('B15', '-12 3/4');
  64. $spreadsheet->getActiveSheet()->setCellValue('A16', 'Fraction value #4:')
  65. ->setCellValue('B16', '13/4');
  66. $spreadsheet->getActiveSheet()->setCellValue('A17', 'Currency value #1:')
  67. ->setCellValue('B17', '$12345');
  68. $spreadsheet->getActiveSheet()->setCellValue('A18', 'Currency value #2:')
  69. ->setCellValue('B18', '$12345.67');
  70. $spreadsheet->getActiveSheet()->setCellValue('A19', 'Currency value #3:')
  71. ->setCellValue('B19', '$12,345.67');
  72. $spreadsheet->getActiveSheet()->setCellValue('A20', 'Date value #1:')
  73. ->setCellValue('B20', '21 December 1983');
  74. $spreadsheet->getActiveSheet()->setCellValue('A21', 'Date value #2:')
  75. ->setCellValue('B21', '19-Dec-1960');
  76. $spreadsheet->getActiveSheet()->setCellValue('A22', 'Date value #3:')
  77. ->setCellValue('B22', '07/12/1982');
  78. $spreadsheet->getActiveSheet()->setCellValue('A23', 'Date value #4:')
  79. ->setCellValue('B23', '24-11-1950');
  80. $spreadsheet->getActiveSheet()->setCellValue('A24', 'Date value #5:')
  81. ->setCellValue('B24', '17-Mar');
  82. $spreadsheet->getActiveSheet()->setCellValue('A25', 'Time value #1:')
  83. ->setCellValue('B25', '01:30');
  84. $spreadsheet->getActiveSheet()->setCellValue('A26', 'Time value #2:')
  85. ->setCellValue('B26', '01:30:15');
  86. $spreadsheet->getActiveSheet()->setCellValue('A27', 'Date/Time value:')
  87. ->setCellValue('B27', '19-Dec-1960 01:30');
  88. $spreadsheet->getActiveSheet()->setCellValue('A28', 'Formula:')
  89. ->setCellValue('B28', '=SUM(B2:B9)');
  90. // Rename worksheet
  91. $helper->log('Rename worksheet');
  92. $spreadsheet->getActiveSheet()->setTitle('Advanced value binder');
  93. // Save
  94. $helper->write($spreadsheet, __FILE__);