| 1 |
<?php |
| 2 |
/** |
| 3 |
* PHPExcel |
| 4 |
* |
| 5 |
* Copyright (C) 2006 - 2014 PHPExcel |
| 6 |
* |
| 7 |
* This library is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU Lesser General Public |
| 9 |
* License as published by the Free Software Foundation; either |
| 10 |
* version 2.1 of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This library is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
* Lesser General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Lesser General Public |
| 18 |
* License along with this library; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 |
* |
| 21 |
* @category PHPExcel |
| 22 |
* @package PHPExcel |
| 23 |
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel) |
| 24 |
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL |
| 25 |
* @version ##VERSION##, ##DATE## |
| 26 |
*/ |
| 27 |
|
| 28 |
/** Error reporting */ |
| 29 |
error_reporting(E_ALL); |
| 30 |
ini_set('display_errors', TRUE); |
| 31 |
ini_set('display_startup_errors', TRUE); |
| 32 |
date_default_timezone_set('Europe/London'); |
| 33 |
|
| 34 |
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />'); |
| 35 |
|
| 36 |
date_default_timezone_set('Europe/London'); |
| 37 |
mt_srand(1234567890); |
| 38 |
|
| 39 |
/** Include PHPExcel */ |
| 40 |
require_once dirname(__FILE__) . '/../Classes/PHPExcel.php'; |
| 41 |
|
| 42 |
|
| 43 |
// List functions |
| 44 |
echo date('H:i:s') , " List implemented functions" , EOL; |
| 45 |
$objCalc = PHPExcel_Calculation::getInstance(); |
| 46 |
print_r($objCalc->listFunctionNames()); |
| 47 |
|
| 48 |
// Create new PHPExcel object |
| 49 |
echo date('H:i:s') , " Create new PHPExcel object" , EOL; |
| 50 |
$objPHPExcel = new PHPExcel(); |
| 51 |
|
| 52 |
// Add some data, we will use some formulas here |
| 53 |
echo date('H:i:s') , " Add some data and formulas" , EOL; |
| 54 |
$objPHPExcel->getActiveSheet()->setCellValue('A14', 'Count:') |
| 55 |
->setCellValue('A15', 'Sum:') |
| 56 |
->setCellValue('A16', 'Max:') |
| 57 |
->setCellValue('A17', 'Min:') |
| 58 |
->setCellValue('A18', 'Average:') |
| 59 |
->setCellValue('A19', 'Median:') |
| 60 |
->setCellValue('A20', 'Mode:'); |
| 61 |
|
| 62 |
$objPHPExcel->getActiveSheet()->setCellValue('A22', 'CountA:') |
| 63 |
->setCellValue('A23', 'MaxA:') |
| 64 |
->setCellValue('A24', 'MinA:'); |
| 65 |
|
| 66 |
$objPHPExcel->getActiveSheet()->setCellValue('A26', 'StDev:') |
| 67 |
->setCellValue('A27', 'StDevA:') |
| 68 |
->setCellValue('A28', 'StDevP:') |
| 69 |
->setCellValue('A29', 'StDevPA:'); |
| 70 |
|
| 71 |
$objPHPExcel->getActiveSheet()->setCellValue('A31', 'DevSq:') |
| 72 |
->setCellValue('A32', 'Var:') |
| 73 |
->setCellValue('A33', 'VarA:') |
| 74 |
->setCellValue('A34', 'VarP:') |
| 75 |
->setCellValue('A35', 'VarPA:'); |
| 76 |
|
| 77 |
$objPHPExcel->getActiveSheet()->setCellValue('A37', 'Date:'); |
| 78 |
|
| 79 |
|
| 80 |
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Range 1') |
| 81 |
->setCellValue('B2', 2) |
| 82 |
->setCellValue('B3', 8) |
| 83 |
->setCellValue('B4', 10) |
| 84 |
->setCellValue('B5', True) |
| 85 |
->setCellValue('B6', False) |
| 86 |
->setCellValue('B7', 'Text String') |
| 87 |
->setCellValue('B9', '22') |
| 88 |
->setCellValue('B10', 4) |
| 89 |
->setCellValue('B11', 6) |
| 90 |
->setCellValue('B12', 12); |
| 91 |
|
| 92 |
$objPHPExcel->getActiveSheet()->setCellValue('B14', '=COUNT(B2:B12)') |
| 93 |
->setCellValue('B15', '=SUM(B2:B12)') |
| 94 |
->setCellValue('B16', '=MAX(B2:B12)') |
| 95 |
->setCellValue('B17', '=MIN(B2:B12)') |
| 96 |
->setCellValue('B18', '=AVERAGE(B2:B12)') |
| 97 |
->setCellValue('B19', '=MEDIAN(B2:B12)') |
| 98 |
->setCellValue('B20', '=MODE(B2:B12)'); |
| 99 |
|
| 100 |
$objPHPExcel->getActiveSheet()->setCellValue('B22', '=COUNTA(B2:B12)') |
| 101 |
->setCellValue('B23', '=MAXA(B2:B12)') |
| 102 |
->setCellValue('B24', '=MINA(B2:B12)'); |
| 103 |
|
| 104 |
$objPHPExcel->getActiveSheet()->setCellValue('B26', '=STDEV(B2:B12)') |
| 105 |
->setCellValue('B27', '=STDEVA(B2:B12)') |
| 106 |
->setCellValue('B28', '=STDEVP(B2:B12)') |
| 107 |
->setCellValue('B29', '=STDEVPA(B2:B12)'); |
| 108 |
|
| 109 |
$objPHPExcel->getActiveSheet()->setCellValue('B31', '=DEVSQ(B2:B12)') |
| 110 |
->setCellValue('B32', '=VAR(B2:B12)') |
| 111 |
->setCellValue('B33', '=VARA(B2:B12)') |
| 112 |
->setCellValue('B34', '=VARP(B2:B12)') |
| 113 |
->setCellValue('B35', '=VARPA(B2:B12)'); |
| 114 |
|
| 115 |
$objPHPExcel->getActiveSheet()->setCellValue('B37', '=DATE(2007, 12, 21)') |
| 116 |
->setCellValue('B38', '=DATEDIF( DATE(2007, 12, 21), DATE(2007, 12, 22), "D" )') |
| 117 |
->setCellValue('B39', '=DATEVALUE("01-Feb-2006 10:06 AM")') |
| 118 |
->setCellValue('B40', '=DAY( DATE(2006, 1, 2) )') |
| 119 |
->setCellValue('B41', '=DAYS360( DATE(2002, 2, 3), DATE(2005, 5, 31) )'); |
| 120 |
|
| 121 |
|
| 122 |
$objPHPExcel->getActiveSheet()->setCellValue('C1', 'Range 2') |
| 123 |
->setCellValue('C2', 1) |
| 124 |
->setCellValue('C3', 2) |
| 125 |
->setCellValue('C4', 2) |
| 126 |
->setCellValue('C5', 3) |
| 127 |
->setCellValue('C6', 3) |
| 128 |
->setCellValue('C7', 3) |
| 129 |
->setCellValue('C8', '0') |
| 130 |
->setCellValue('C9', 4) |
| 131 |
->setCellValue('C10', 4) |
| 132 |
->setCellValue('C11', 4) |
| 133 |
->setCellValue('C12', 4); |
| 134 |
|
| 135 |
$objPHPExcel->getActiveSheet()->setCellValue('C14', '=COUNT(C2:C12)') |
| 136 |
->setCellValue('C15', '=SUM(C2:C12)') |
| 137 |
->setCellValue('C16', '=MAX(C2:C12)') |
| 138 |
->setCellValue('C17', '=MIN(C2:C12)') |
| 139 |
->setCellValue('C18', '=AVERAGE(C2:C12)') |
| 140 |
->setCellValue('C19', '=MEDIAN(C2:C12)') |
| 141 |
->setCellValue('C20', '=MODE(C2:C12)'); |
| 142 |
|
| 143 |
$objPHPExcel->getActiveSheet()->setCellValue('C22', '=COUNTA(C2:C12)') |
| 144 |
->setCellValue('C23', '=MAXA(C2:C12)') |
| 145 |
->setCellValue('C24', '=MINA(C2:C12)'); |
| 146 |
|
| 147 |
$objPHPExcel->getActiveSheet()->setCellValue('C26', '=STDEV(C2:C12)') |
| 148 |
->setCellValue('C27', '=STDEVA(C2:C12)') |
| 149 |
->setCellValue('C28', '=STDEVP(C2:C12)') |
| 150 |
->setCellValue('C29', '=STDEVPA(C2:C12)'); |
| 151 |
|
| 152 |
$objPHPExcel->getActiveSheet()->setCellValue('C31', '=DEVSQ(C2:C12)') |
| 153 |
->setCellValue('C32', '=VAR(C2:C12)') |
| 154 |
->setCellValue('C33', '=VARA(C2:C12)') |
| 155 |
->setCellValue('C34', '=VARP(C2:C12)') |
| 156 |
->setCellValue('C35', '=VARPA(C2:C12)'); |
| 157 |
|
| 158 |
|
| 159 |
$objPHPExcel->getActiveSheet()->setCellValue('D1', 'Range 3') |
| 160 |
->setCellValue('D2', 2) |
| 161 |
->setCellValue('D3', 3) |
| 162 |
->setCellValue('D4', 4); |
| 163 |
|
| 164 |
$objPHPExcel->getActiveSheet()->setCellValue('D14', '=((D2 * D3) + D4) & " should be 10"'); |
| 165 |
|
| 166 |
$objPHPExcel->getActiveSheet()->setCellValue('E12', 'Other functions') |
| 167 |
->setCellValue('E14', '=PI()') |
| 168 |
->setCellValue('E15', '=RAND()') |
| 169 |
->setCellValue('E16', '=RANDBETWEEN(5, 10)'); |
| 170 |
|
| 171 |
$objPHPExcel->getActiveSheet()->setCellValue('E17', 'Count of both ranges:') |
| 172 |
->setCellValue('F17', '=COUNT(B2:C12)'); |
| 173 |
|
| 174 |
$objPHPExcel->getActiveSheet()->setCellValue('E18', 'Total of both ranges:') |
| 175 |
->setCellValue('F18', '=SUM(B2:C12)'); |
| 176 |
|
| 177 |
$objPHPExcel->getActiveSheet()->setCellValue('E19', 'Maximum of both ranges:') |
| 178 |
->setCellValue('F19', '=MAX(B2:C12)'); |
| 179 |
|
| 180 |
$objPHPExcel->getActiveSheet()->setCellValue('E20', 'Minimum of both ranges:') |
| 181 |
->setCellValue('F20', '=MIN(B2:C12)'); |
| 182 |
|
| 183 |
$objPHPExcel->getActiveSheet()->setCellValue('E21', 'Average of both ranges:') |
| 184 |
->setCellValue('F21', '=AVERAGE(B2:C12)'); |
| 185 |
|
| 186 |
$objPHPExcel->getActiveSheet()->setCellValue('E22', 'Median of both ranges:') |
| 187 |
->setCellValue('F22', '=MEDIAN(B2:C12)'); |
| 188 |
|
| 189 |
$objPHPExcel->getActiveSheet()->setCellValue('E23', 'Mode of both ranges:') |
| 190 |
->setCellValue('F23', '=MODE(B2:C12)'); |
| 191 |
|
| 192 |
|
| 193 |
// Calculated data |
| 194 |
echo date('H:i:s') , " Calculated data" , EOL; |
| 195 |
for ($col = 'B'; $col != 'G'; ++$col) { |
| 196 |
for($row = 14; $row <= 41; ++$row) { |
| 197 |
if ((!is_null($formula = $objPHPExcel->getActiveSheet()->getCell($col.$row)->getValue())) && |
| 198 |
($formula[0] == '=')) { |
| 199 |
echo 'Value of ' , $col , $row , ' [' , $formula , ']: ' , |
| 200 |
$objPHPExcel->getActiveSheet()->getCell($col.$row)->getCalculatedValue() . EOL; |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
// Save Excel 2007 file |
| 207 |
echo date('H:i:s') , " Write to Excel2007 format" , EOL; |
| 208 |
$callStartTime = microtime(true); |
| 209 |
|
| 210 |
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); |
| 211 |
|
| 212 |
// |
| 213 |
// If we set Pre Calculated Formulas to true then PHPExcel will calculate all formulae in the |
| 214 |
// workbook before saving. This adds time and memory overhead, and can cause some problems with formulae |
| 215 |
// using functions or features (such as array formulae) that aren't yet supported by the calculation engine |
| 216 |
// If the value is false (the default) for the Excel2007 Writer, then MS Excel (or the application used to |
| 217 |
// open the file) will need to recalculate values itself to guarantee that the correct results are available. |
| 218 |
// |
| 219 |
//$objWriter->setPreCalculateFormulas(true); |
| 220 |
$objWriter->save(str_replace('.php', '.xlsx', __FILE__)); |
| 221 |
$callEndTime = microtime(true); |
| 222 |
$callTime = $callEndTime - $callStartTime; |
| 223 |
|
| 224 |
echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL; |
| 225 |
echo 'Call time to write Workbook was ' , sprintf('%.4f',$callTime) , " seconds" , EOL; |
| 226 |
// Echo memory usage |
| 227 |
echo date('H:i:s') , ' Current memory usage: ' , (memory_get_usage(true) / 1024 / 1024) , " MB" , EOL; |
| 228 |
|
| 229 |
|
| 230 |
// Echo memory peak usage |
| 231 |
echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL; |
| 232 |
|
| 233 |
// Echo done |
| 234 |
echo date('H:i:s') , " Done writing file" , EOL; |
| 235 |
echo 'File has been created in ' , getcwd() , EOL; |
| 236 |
|