| 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 |
|
| 33 |
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />'); |
| 34 |
|
| 35 |
date_default_timezone_set('Europe/London'); |
| 36 |
|
| 37 |
/** PHPExcel_IOFactory */ |
| 38 |
require_once dirname(__FILE__) . '/../Classes/PHPExcel/IOFactory.php'; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
echo date('H:i:s') , " Load from Excel5 template" , EOL; |
| 43 |
$objReader = PHPExcel_IOFactory::createReader('Excel5'); |
| 44 |
$objPHPExcel = $objReader->load("templates/30template.xls"); |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
echo date('H:i:s') , " Add new data to the template" , EOL; |
| 50 |
$data = array(array('title' => 'Excel for dummies', |
| 51 |
'price' => 17.99, |
| 52 |
'quantity' => 2 |
| 53 |
), |
| 54 |
array('title' => 'PHP for dummies', |
| 55 |
'price' => 15.99, |
| 56 |
'quantity' => 1 |
| 57 |
), |
| 58 |
array('title' => 'Inside OOP', |
| 59 |
'price' => 12.95, |
| 60 |
'quantity' => 1 |
| 61 |
) |
| 62 |
); |
| 63 |
|
| 64 |
$objPHPExcel->getActiveSheet()->setCellValue('D1', PHPExcel_Shared_Date::PHPToExcel(time())); |
| 65 |
|
| 66 |
$baseRow = 5; |
| 67 |
foreach($data as $r => $dataRow) { |
| 68 |
$row = $baseRow + $r; |
| 69 |
$objPHPExcel->getActiveSheet()->insertNewRowBefore($row,1); |
| 70 |
|
| 71 |
$objPHPExcel->getActiveSheet()->setCellValue('A'.$row, $r+1) |
| 72 |
->setCellValue('B'.$row, $dataRow['title']) |
| 73 |
->setCellValue('C'.$row, $dataRow['price']) |
| 74 |
->setCellValue('D'.$row, $dataRow['quantity']) |
| 75 |
->setCellValue('E'.$row, '=C'.$row.'*D'.$row); |
| 76 |
} |
| 77 |
$objPHPExcel->getActiveSheet()->removeRow($baseRow-1,1); |
| 78 |
|
| 79 |
|
| 80 |
echo date('H:i:s') , " Write to Excel5 format" , EOL; |
| 81 |
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); |
| 82 |
$objWriter->save(str_replace('.php', '.xls', __FILE__)); |
| 83 |
echo date('H:i:s') , " File written to " , str_replace('.php', '.xls', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL; |
| 84 |
|
| 85 |
|
| 86 |
// Echo memory peak usage |
| 87 |
echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL; |
| 88 |
|
| 89 |
// Echo done |
| 90 |
echo date('H:i:s') , " Done writing file" , EOL; |
| 91 |
echo 'File has been created in ' , getcwd() , EOL; |
| 92 |
|