DMAX.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. error_reporting(E_ALL);
  3. set_time_limit(0);
  4. date_default_timezone_set('Europe/London');
  5. ?>
  6. <html>
  7. <head>
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  9. <title>PHPExcel Calculation Examples</title>
  10. </head>
  11. <body>
  12. <h1>DMAX</h1>
  13. <h2>Returns the maximum value from selected database entries.</h2>
  14. <?php
  15. /** Include path **/
  16. set_include_path(get_include_path() . PATH_SEPARATOR . '../../../../Classes/');
  17. /** Include PHPExcel */
  18. include 'PHPExcel.php';
  19. // Create new PHPExcel object
  20. $objPHPExcel = new PHPExcel();
  21. $worksheet = $objPHPExcel->getActiveSheet();
  22. // Add some data
  23. $database = array( array( 'Tree', 'Height', 'Age', 'Yield', 'Profit' ),
  24. array( 'Apple', 18, 20, 14, 105.00 ),
  25. array( 'Pear', 12, 12, 10, 96.00 ),
  26. array( 'Cherry', 13, 14, 9, 105.00 ),
  27. array( 'Apple', 14, 15, 10, 75.00 ),
  28. array( 'Pear', 9, 8, 8, 76.80 ),
  29. array( 'Apple', 8, 9, 6, 45.00 ),
  30. );
  31. $criteria = array( array( 'Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height' ),
  32. array( '="=Apple"', '>10', NULL, NULL, NULL, '<16' ),
  33. array( '="=Pear"', NULL, NULL, NULL, NULL, NULL )
  34. );
  35. $worksheet->fromArray( $criteria, NULL, 'A1' );
  36. $worksheet->fromArray( $database, NULL, 'A4' );
  37. $worksheet->setCellValue('A12', 'The tallest tree in the orchard');
  38. $worksheet->setCellValue('B12', '=DMAX(A4:E10,"Height",A4:E10)');
  39. $worksheet->setCellValue('A13', 'The Oldest apple tree in the orchard');
  40. $worksheet->setCellValue('B13', '=DMAX(A4:E10,3,A1:A2)');
  41. echo '<hr />';
  42. echo '<h4>Database</h4>';
  43. $databaseData = $worksheet->rangeToArray('A4:E10',null,true,true,true);
  44. var_dump($databaseData);
  45. echo '<hr />';
  46. // Test the formulae
  47. echo '<h4>Criteria</h4>';
  48. echo 'ALL' . '<br /><br />';
  49. echo $worksheet->getCell("A12")->getValue() .'<br />';
  50. echo 'DMAX() Result is ' . $worksheet->getCell("B12")->getCalculatedValue() .'<br /><br />';
  51. echo '<h4>Criteria</h4>';
  52. $criteriaData = $worksheet->rangeToArray('A1:A2',null,true,true,true);
  53. var_dump($criteriaData);
  54. echo $worksheet->getCell("A13")->getValue() .'<br />';
  55. echo 'DMAX() Result is ' . $worksheet->getCell("B13")->getCalculatedValue();
  56. ?>
  57. <body>
  58. </html>