No Description

30template.php 3.1KB

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