40duplicateStyle.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /** Error reporting */
  3. error_reporting(E_ALL);
  4. ini_set('display_errors', TRUE);
  5. ini_set('display_startup_errors', TRUE);
  6. date_default_timezone_set('Europe/London');
  7. define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
  8. date_default_timezone_set('Europe/London');
  9. /** Include PHPExcel */
  10. require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';
  11. echo date('H:i:s') , " Create new PHPExcel object" , EOL;
  12. $objPHPExcel = new PHPExcel();
  13. $worksheet = $objPHPExcel->getActiveSheet();
  14. echo date('H:i:s') , " Create styles array" , EOL;
  15. $styles = array();
  16. for ($i = 0; $i < 10; $i++) {
  17. $style = new PHPExcel_Style();
  18. $style->getFont()->setSize($i + 4);
  19. $styles[] = $style;
  20. }
  21. echo date('H:i:s') , " Add data (begin)" , EOL;
  22. $t = microtime(true);
  23. for ($col = 0; $col < 50; $col++) {
  24. for ($row = 0; $row < 100; $row++) {
  25. $str = ($row + $col);
  26. $style = $styles[$row % 10];
  27. $coord = PHPExcel_Cell::stringFromColumnIndex($col) . ($row + 1);
  28. $worksheet->setCellValue($coord, $str);
  29. $worksheet->duplicateStyle($style, $coord);
  30. }
  31. }
  32. $d = microtime(true) - $t;
  33. echo date('H:i:s') , " Add data (end), time: " . round($d, 2) . " s", EOL;
  34. echo date('H:i:s') , " Write to Excel2007 format" , EOL;
  35. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  36. $objWriter->save(str_replace('.php', '.xlsx', __FILE__));
  37. echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
  38. echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL;
  39. echo date('H:i:s') , " Done writing file" , EOL;
  40. echo 'File has been created in ' , getcwd() , EOL;