Formula examples
git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@88518 2327b42d-5241-43d6-9e2a-de5ac946f064
This commit is contained in:
parent
4347e0635a
commit
1725c622f3
|
@ -11,13 +11,13 @@ date_default_timezone_set('Europe/London');
|
||||||
<head>
|
<head>
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||||
|
|
||||||
<title>PHPExcel Reader Example #01</title>
|
<title>PHPExcel Calculation Examples</title>
|
||||||
|
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
<h1>PHPExcel Calculation Example </h1>
|
<h1>PHPExcel Calculation Example</h1>
|
||||||
<h2>Database Formulae</h2>
|
<h2>Database Formulae</h2>
|
||||||
<h3>DAVERAGE</h3>
|
<h3>DAVERAGE</h3>
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
@ -28,10 +28,11 @@ set_include_path(get_include_path() . PATH_SEPARATOR . '../../../../Classes/');
|
||||||
include 'PHPExcel.php';
|
include 'PHPExcel.php';
|
||||||
|
|
||||||
|
|
||||||
|
// Create new PHPExcel object
|
||||||
$objPHPExcel = new PHPExcel();
|
$objPHPExcel = new PHPExcel();
|
||||||
|
|
||||||
$worksheet = $objPHPExcel->getActiveSheet();
|
$worksheet = $objPHPExcel->getActiveSheet();
|
||||||
|
|
||||||
|
// Add some data
|
||||||
$database = array( array( 'Tree', 'Height', 'Age', 'Yield', 'Profit' ),
|
$database = array( array( 'Tree', 'Height', 'Age', 'Yield', 'Profit' ),
|
||||||
array( 'Apple', 18, 20, 14, 105.00 ),
|
array( 'Apple', 18, 20, 14, 105.00 ),
|
||||||
array( 'Pear', 12, 12, 10, 96.00 ),
|
array( 'Pear', 12, 12, 10, 96.00 ),
|
||||||
|
@ -40,7 +41,7 @@ $database = array( array( 'Tree', 'Height', 'Age', 'Yield', 'Profit' ),
|
||||||
array( 'Pear', 9, 8, 8, 76.80 ),
|
array( 'Pear', 9, 8, 8, 76.80 ),
|
||||||
array( 'Apple', 8, 9, 6, 45.00 ),
|
array( 'Apple', 8, 9, 6, 45.00 ),
|
||||||
);
|
);
|
||||||
$criteria = array( array( 'Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height' ),
|
$criteria = array( array( 'Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height' ),
|
||||||
array( '="=Apple"', '>10', NULL, NULL, NULL, '<16' ),
|
array( '="=Apple"', '>10', NULL, NULL, NULL, '<16' ),
|
||||||
array( '="=Pear"', NULL, NULL, NULL, NULL, NULL )
|
array( '="=Pear"', NULL, NULL, NULL, NULL, NULL )
|
||||||
);
|
);
|
||||||
|
@ -48,7 +49,7 @@ $criteria = array( array( 'Tree', 'Height', 'Age', 'Yield', 'Profit', '
|
||||||
$worksheet->fromArray( $criteria, NULL, 'A1' );
|
$worksheet->fromArray( $criteria, NULL, 'A1' );
|
||||||
$worksheet->fromArray( $database, NULL, 'A4' );
|
$worksheet->fromArray( $database, NULL, 'A4' );
|
||||||
|
|
||||||
$worksheet->setCellValue('A12', 'The Average yield of apple trees over 10\' in height');
|
$worksheet->setCellValue('A12', 'The Average yield of Apple trees over 10\' in height');
|
||||||
$worksheet->setCellValue('B12', '=DAVERAGE(A4:E10,"Yield",A1:B2)');
|
$worksheet->setCellValue('B12', '=DAVERAGE(A4:E10,"Yield",A1:B2)');
|
||||||
|
|
||||||
$worksheet->setCellValue('A13', 'The Average age of all Apple and Pear trees in the orchard');
|
$worksheet->setCellValue('A13', 'The Average age of all Apple and Pear trees in the orchard');
|
||||||
|
@ -66,6 +67,8 @@ var_dump($databaseData);
|
||||||
|
|
||||||
echo '<hr />';
|
echo '<hr />';
|
||||||
|
|
||||||
|
|
||||||
|
// Test the formulae
|
||||||
echo '<h4>Criteria</h4>';
|
echo '<h4>Criteria</h4>';
|
||||||
|
|
||||||
$criteriaData = $worksheet->rangeToArray('A1:B2',null,true,true,true);
|
$criteriaData = $worksheet->rangeToArray('A1:B2',null,true,true,true);
|
||||||
|
|
|
@ -0,0 +1,91 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
error_reporting(E_ALL);
|
||||||
|
set_time_limit(0);
|
||||||
|
|
||||||
|
date_default_timezone_set('Europe/London');
|
||||||
|
|
||||||
|
|
||||||
|
?>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||||
|
|
||||||
|
<title>PHPExcel Calculation Examples</title>
|
||||||
|
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<h1>PHPExcel Calculation Example </h1>
|
||||||
|
<h2>Database Formulae</h2>
|
||||||
|
<h3>DCOUNT</h3>
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/** Include path **/
|
||||||
|
set_include_path(get_include_path() . PATH_SEPARATOR . '../../../../Classes/');
|
||||||
|
|
||||||
|
/** PHPExcel_IOFactory */
|
||||||
|
include 'PHPExcel.php';
|
||||||
|
|
||||||
|
|
||||||
|
// Create new PHPExcel object
|
||||||
|
$objPHPExcel = new PHPExcel();
|
||||||
|
$worksheet = $objPHPExcel->getActiveSheet();
|
||||||
|
|
||||||
|
// Add some data
|
||||||
|
$database = array( array( 'Tree', 'Height', 'Age', 'Yield', 'Profit' ),
|
||||||
|
array( 'Apple', 18, 20, 14, 105.00 ),
|
||||||
|
array( 'Pear', 12, 12, 10, 96.00 ),
|
||||||
|
array( 'Cherry', 13, 14, 9, 105.00 ),
|
||||||
|
array( 'Apple', 14, 15, 10, 75.00 ),
|
||||||
|
array( 'Pear', 9, 8, 8, 76.80 ),
|
||||||
|
array( 'Apple', 8, 9, 6, 45.00 ),
|
||||||
|
);
|
||||||
|
$criteria = array( array( 'Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height' ),
|
||||||
|
array( '="=Apple"', '>10', NULL, NULL, NULL, '<16' ),
|
||||||
|
array( '="=Pear"', NULL, NULL, NULL, NULL, NULL )
|
||||||
|
);
|
||||||
|
|
||||||
|
$worksheet->fromArray( $criteria, NULL, 'A1' );
|
||||||
|
$worksheet->fromArray( $database, NULL, 'A4' );
|
||||||
|
|
||||||
|
$worksheet->setCellValue('A12', 'The Number of Apple trees over 10\' in height');
|
||||||
|
$worksheet->setCellValue('B12', '=DCOUNT(A4:E10,"Yield",A1:B2)');
|
||||||
|
|
||||||
|
$worksheet->setCellValue('A13', 'The Number of Apple and Pear trees in the orchard');
|
||||||
|
$worksheet->setCellValue('B13', '=DCOUNT(A4:E10,3,A1:A3)');
|
||||||
|
|
||||||
|
|
||||||
|
echo '<hr />';
|
||||||
|
|
||||||
|
|
||||||
|
echo '<h4>Database</h4>';
|
||||||
|
|
||||||
|
$databaseData = $worksheet->rangeToArray('A4:E10',null,true,true,true);
|
||||||
|
var_dump($databaseData);
|
||||||
|
|
||||||
|
|
||||||
|
echo '<hr />';
|
||||||
|
|
||||||
|
|
||||||
|
// Test the formulae
|
||||||
|
echo '<h4>Criteria</h4>';
|
||||||
|
|
||||||
|
$criteriaData = $worksheet->rangeToArray('A1:B2',null,true,true,true);
|
||||||
|
var_dump($criteriaData);
|
||||||
|
|
||||||
|
echo $worksheet->getCell("A12")->getValue() .'<br />';
|
||||||
|
echo 'DCOUNT() Result is ' . $worksheet->getCell("B12")->getCalculatedValue() .'<br /><br />';
|
||||||
|
|
||||||
|
echo '<h4>Criteria</h4>';
|
||||||
|
|
||||||
|
$criteriaData = $worksheet->rangeToArray('A1:A3',null,true,true,true);
|
||||||
|
var_dump($criteriaData);
|
||||||
|
|
||||||
|
echo $worksheet->getCell("A13")->getValue() .'<br />';
|
||||||
|
echo 'DCOUNT() Result is ' . $worksheet->getCell("B13")->getCalculatedValue();
|
||||||
|
|
||||||
|
|
||||||
|
?>
|
||||||
|
<body>
|
||||||
|
</html>
|
|
@ -0,0 +1,84 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
error_reporting(E_ALL);
|
||||||
|
set_time_limit(0);
|
||||||
|
|
||||||
|
date_default_timezone_set('Europe/London');
|
||||||
|
|
||||||
|
|
||||||
|
?>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||||
|
|
||||||
|
<title>PHPExcel Calculation Examples</title>
|
||||||
|
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<h1>PHPExcel Calculation Example</h1>
|
||||||
|
<h2>Data/Time Formulae</h2>
|
||||||
|
<h3>DATE</h3>
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/** Include path **/
|
||||||
|
set_include_path(get_include_path() . PATH_SEPARATOR . '../../../../Classes/');
|
||||||
|
|
||||||
|
/** PHPExcel_IOFactory */
|
||||||
|
include 'PHPExcel.php';
|
||||||
|
|
||||||
|
|
||||||
|
// Create new PHPExcel object
|
||||||
|
$objPHPExcel = new PHPExcel();
|
||||||
|
$worksheet = $objPHPExcel->getActiveSheet();
|
||||||
|
|
||||||
|
// Add some data
|
||||||
|
$testDates = array( array(2012,3,26), array(2012,2,29), array(2012,4,1), array(2012,12,25),
|
||||||
|
array(2012,10,31), array(2012,11,5), array(2012,1,1), array(2012,3,17),
|
||||||
|
array(2011,2,29), array(7,5,3), array(2012,13,1), array(2012,11,45),
|
||||||
|
array(2012,0,0), array(2012,1,0), array(2012,0,1),
|
||||||
|
array(2012,-2,2), array(2012,2,-2), array(2012,-2,-2),
|
||||||
|
);
|
||||||
|
$testDateCount = count($testDates);
|
||||||
|
|
||||||
|
$worksheet->fromArray($testDates,NULL,'A1',true);
|
||||||
|
|
||||||
|
for ($row = 1; $row <= $testDateCount; ++$row) {
|
||||||
|
$worksheet->setCellValue('D'.$row, '=DATE(A'.$row.',B'.$row.',C'.$row.')');
|
||||||
|
$worksheet->setCellValue('E'.$row, '=D'.$row);
|
||||||
|
}
|
||||||
|
$worksheet->getStyle('E1:E'.$testDateCount)
|
||||||
|
->getNumberFormat()
|
||||||
|
->setFormatCode('yyyy-mmm-dd');
|
||||||
|
|
||||||
|
|
||||||
|
echo '<hr />';
|
||||||
|
|
||||||
|
|
||||||
|
// Test the formulae
|
||||||
|
?>
|
||||||
|
<table border="1" cellspacing="0">
|
||||||
|
<tr>
|
||||||
|
<th colspan="3">Date Value</th>
|
||||||
|
<th rowspan="2" valign="bottom">Formula</th>
|
||||||
|
<th rowspan="2" valign="bottom">Excel DateStamp</th>
|
||||||
|
<th rowspan="2" valign="bottom">Formatted DateStamp</th>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th>Year</th>
|
||||||
|
<th>Month</th>
|
||||||
|
<th>Day</th>
|
||||||
|
<tr>
|
||||||
|
<?php
|
||||||
|
for ($row = 1; $row <= $testDateCount; ++$row) {
|
||||||
|
echo '<tr>';
|
||||||
|
echo '<td>' , $worksheet->getCell('A'.$row)->getFormattedValue() , '</td>';
|
||||||
|
echo '<td>' , $worksheet->getCell('B'.$row)->getFormattedValue() , '</td>';
|
||||||
|
echo '<td>' , $worksheet->getCell('C'.$row)->getFormattedValue() , '</td>';
|
||||||
|
echo '<td>' , $worksheet->getCell('D'.$row)->getValue() , '</td>';
|
||||||
|
echo '<td>' , $worksheet->getCell('D'.$row)->getFormattedValue() , '</td>';
|
||||||
|
echo '<td>' , $worksheet->getCell('E'.$row)->getFormattedValue() , '</td>';
|
||||||
|
echo '</tr>';
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
</table>
|
|
@ -0,0 +1,77 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
error_reporting(E_ALL);
|
||||||
|
set_time_limit(0);
|
||||||
|
|
||||||
|
date_default_timezone_set('Europe/London');
|
||||||
|
|
||||||
|
|
||||||
|
?>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||||
|
|
||||||
|
<title>PHPExcel Calculation Examples</title>
|
||||||
|
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<h1>PHPExcel Calculation Example</h1>
|
||||||
|
<h2>Data/Time Formulae</h2>
|
||||||
|
<h3>DATEVALUE</h3>
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/** Include path **/
|
||||||
|
set_include_path(get_include_path() . PATH_SEPARATOR . '../../../../Classes/');
|
||||||
|
|
||||||
|
/** PHPExcel_IOFactory */
|
||||||
|
include 'PHPExcel.php';
|
||||||
|
|
||||||
|
|
||||||
|
// Create new PHPExcel object
|
||||||
|
$objPHPExcel = new PHPExcel();
|
||||||
|
$worksheet = $objPHPExcel->getActiveSheet();
|
||||||
|
|
||||||
|
// Add some data
|
||||||
|
$testDates = array( '26 March 2012', '29 Feb 2012', 'April 1, 2012', '25/12/2012',
|
||||||
|
'2012-Oct-31', '5th November', 'January 1st', 'April 2012',
|
||||||
|
'17-03', '03-2012', '29 Feb 2011', '03-05-07',
|
||||||
|
'03-MAY-07', '03-13-07',
|
||||||
|
);
|
||||||
|
$testDateCount = count($testDates);
|
||||||
|
|
||||||
|
for($row = 1; $row <= $testDateCount; ++$row) {
|
||||||
|
$worksheet->setCellValue('A'.$row, $testDates[$row-1]);
|
||||||
|
$worksheet->setCellValue('B'.$row, '=DATEVALUE(A'.$row.')');
|
||||||
|
$worksheet->setCellValue('C'.$row, '=B'.$row);
|
||||||
|
}
|
||||||
|
|
||||||
|
$worksheet->getStyle('C1:C'.$testDateCount)
|
||||||
|
->getNumberFormat()
|
||||||
|
->setFormatCode('yyyy-mmm-dd');
|
||||||
|
|
||||||
|
|
||||||
|
echo '<hr />';
|
||||||
|
|
||||||
|
|
||||||
|
// Test the formulae
|
||||||
|
?>
|
||||||
|
<p><strong>Warning: </strong>The PHPExcel DATEVALUE() function accepts a wider range of date formats than MS Excel's DATEFORMAT() function.</p>
|
||||||
|
<table border="1" cellspacing="0">
|
||||||
|
<tr>
|
||||||
|
<th>Date String</th>
|
||||||
|
<th>Formula</th>
|
||||||
|
<th>Excel DateStamp</th>
|
||||||
|
<th>Formatted DateStamp</th>
|
||||||
|
</tr>
|
||||||
|
<?php
|
||||||
|
for ($row = 1; $row <= $testDateCount; ++$row) {
|
||||||
|
echo '<tr>';
|
||||||
|
echo '<td>' , $worksheet->getCell('A'.$row)->getFormattedValue() , '</td>';
|
||||||
|
echo '<td>' , $worksheet->getCell('B'.$row)->getValue() , '</td>';
|
||||||
|
echo '<td>' , $worksheet->getCell('B'.$row)->getFormattedValue() , '</td>';
|
||||||
|
echo '<td>' , $worksheet->getCell('C'.$row)->getFormattedValue() , '</td>';
|
||||||
|
echo '</tr>';
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
</table>
|
|
@ -0,0 +1,73 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
error_reporting(E_ALL);
|
||||||
|
set_time_limit(0);
|
||||||
|
|
||||||
|
date_default_timezone_set('Europe/London');
|
||||||
|
|
||||||
|
|
||||||
|
?>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||||
|
|
||||||
|
<title>PHPExcel Calculation Examples</title>
|
||||||
|
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<h1>PHPExcel Calculation Example</h1>
|
||||||
|
<h2>Data/Time Formulae</h2>
|
||||||
|
<h3>TIMEVALUE</h3>
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/** Include path **/
|
||||||
|
set_include_path(get_include_path() . PATH_SEPARATOR . '../../../../Classes/');
|
||||||
|
|
||||||
|
/** PHPExcel_IOFactory */
|
||||||
|
include 'PHPExcel.php';
|
||||||
|
|
||||||
|
|
||||||
|
// Create new PHPExcel object
|
||||||
|
$objPHPExcel = new PHPExcel();
|
||||||
|
$worksheet = $objPHPExcel->getActiveSheet();
|
||||||
|
|
||||||
|
// Add some data
|
||||||
|
$testDates = array( '3:15', '13:15', '15:15:15', '3:15 AM', '3:15 PM', '5PM', '9:15AM', '13:15AM'
|
||||||
|
);
|
||||||
|
$testDateCount = count($testDates);
|
||||||
|
|
||||||
|
for($row = 1; $row <= $testDateCount; ++$row) {
|
||||||
|
$worksheet->setCellValue('A'.$row, $testDates[$row-1]);
|
||||||
|
$worksheet->setCellValue('B'.$row, '=TIMEVALUE(A'.$row.')');
|
||||||
|
$worksheet->setCellValue('C'.$row, '=B'.$row);
|
||||||
|
}
|
||||||
|
|
||||||
|
$worksheet->getStyle('C1:C'.$testDateCount)
|
||||||
|
->getNumberFormat()
|
||||||
|
->setFormatCode('hh:mm:ss');
|
||||||
|
|
||||||
|
|
||||||
|
echo '<hr />';
|
||||||
|
|
||||||
|
|
||||||
|
// Test the formulae
|
||||||
|
?>
|
||||||
|
<table border="1" cellspacing="0">
|
||||||
|
<tr>
|
||||||
|
<th>Time String</th>
|
||||||
|
<th>Formula</th>
|
||||||
|
<th>Excel TimeStamp</th>
|
||||||
|
<th>Formatted TimeStamp</th>
|
||||||
|
</tr>
|
||||||
|
<?php
|
||||||
|
for ($row = 1; $row <= $testDateCount; ++$row) {
|
||||||
|
echo '<tr>';
|
||||||
|
echo '<td>' , $worksheet->getCell('A'.$row)->getFormattedValue() , '</td>';
|
||||||
|
echo '<td>' , $worksheet->getCell('B'.$row)->getValue() , '</td>';
|
||||||
|
echo '<td>' , $worksheet->getCell('B'.$row)->getFormattedValue() , '</td>';
|
||||||
|
echo '<td>' , $worksheet->getCell('C'.$row)->getFormattedValue() , '</td>';
|
||||||
|
echo '</tr>';
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
</table>
|
Loading…
Reference in New Issue