02types-xls.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2015 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 - 2015 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. date_default_timezone_set('Europe/London');
  32. define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
  33. /** Include PHPExcel */
  34. require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';
  35. // Create new PHPExcel object
  36. echo date('H:i:s') , " Create new PHPExcel object" , EOL;
  37. $objPHPExcel = new PHPExcel();
  38. // Set document properties
  39. echo date('H:i:s') , " Set document properties" , EOL;
  40. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  41. ->setLastModifiedBy("Maarten Balliauw")
  42. ->setTitle("Office 2007 XLSX Test Document")
  43. ->setSubject("Office 2007 XLSX Test Document")
  44. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  45. ->setKeywords("office 2007 openxml php")
  46. ->setCategory("Test result file");
  47. // Set default font
  48. echo date('H:i:s') , " Set default font" , EOL;
  49. $objPHPExcel->getDefaultStyle()->getFont()->setName('Arial')
  50. ->setSize(10);
  51. // Add some data, resembling some different data types
  52. echo date('H:i:s') , " Add some data" , EOL;
  53. $objPHPExcel->getActiveSheet()->setCellValue('A1', 'String')
  54. ->setCellValue('B1', 'Simple')
  55. ->setCellValue('C1', 'PHPExcel');
  56. $objPHPExcel->getActiveSheet()->setCellValue('A2', 'String')
  57. ->setCellValue('B2', 'Symbols')
  58. ->setCellValue('C2', '!+&=()~§±æþ');
  59. $objPHPExcel->getActiveSheet()->setCellValue('A3', 'String')
  60. ->setCellValue('B3', 'UTF-8')
  61. ->setCellValue('C3', 'Создать MS Excel Книги из PHP скриптов');
  62. $objPHPExcel->getActiveSheet()->setCellValue('A4', 'Number')
  63. ->setCellValue('B4', 'Integer')
  64. ->setCellValue('C4', 12);
  65. $objPHPExcel->getActiveSheet()->setCellValue('A5', 'Number')
  66. ->setCellValue('B5', 'Float')
  67. ->setCellValue('C5', 34.56);
  68. $objPHPExcel->getActiveSheet()->setCellValue('A6', 'Number')
  69. ->setCellValue('B6', 'Negative')
  70. ->setCellValue('C6', -7.89);
  71. $objPHPExcel->getActiveSheet()->setCellValue('A7', 'Boolean')
  72. ->setCellValue('B7', 'True')
  73. ->setCellValue('C7', true);
  74. $objPHPExcel->getActiveSheet()->setCellValue('A8', 'Boolean')
  75. ->setCellValue('B8', 'False')
  76. ->setCellValue('C8', false);
  77. $dateTimeNow = time();
  78. $objPHPExcel->getActiveSheet()->setCellValue('A9', 'Date/Time')
  79. ->setCellValue('B9', 'Date')
  80. ->setCellValue('C9', PHPExcel_Shared_Date::PHPToExcel( $dateTimeNow ));
  81. $objPHPExcel->getActiveSheet()->getStyle('C9')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2);
  82. $objPHPExcel->getActiveSheet()->setCellValue('A10', 'Date/Time')
  83. ->setCellValue('B10', 'Time')
  84. ->setCellValue('C10', PHPExcel_Shared_Date::PHPToExcel( $dateTimeNow ));
  85. $objPHPExcel->getActiveSheet()->getStyle('C10')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4);
  86. $objPHPExcel->getActiveSheet()->setCellValue('A11', 'Date/Time')
  87. ->setCellValue('B11', 'Date and Time')
  88. ->setCellValue('C11', PHPExcel_Shared_Date::PHPToExcel( $dateTimeNow ));
  89. $objPHPExcel->getActiveSheet()->getStyle('C11')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_DATETIME);
  90. $objPHPExcel->getActiveSheet()->setCellValue('A12', 'NULL')
  91. ->setCellValue('C12', NULL);
  92. $objRichText = new PHPExcel_RichText();
  93. $objRichText->createText('你好 ');
  94. $objPayable = $objRichText->createTextRun('你 好 吗?');
  95. $objPayable->getFont()->setBold(true);
  96. $objPayable->getFont()->setItalic(true);
  97. $objPayable->getFont()->setColor( new PHPExcel_Style_Color( PHPExcel_Style_Color::COLOR_DARKGREEN ) );
  98. $objRichText->createText(', unless specified otherwise on the invoice.');
  99. $objPHPExcel->getActiveSheet()->setCellValue('A13', 'Rich Text')
  100. ->setCellValue('C13', $objRichText);
  101. $objRichText2 = new PHPExcel_RichText();
  102. $objRichText2->createText("black text\n");
  103. $objRed = $objRichText2->createTextRun("red text");
  104. $objRed->getFont()->setColor( new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_RED ) );
  105. $objPHPExcel->getActiveSheet()->getCell("C14")->setValue($objRichText2);
  106. $objPHPExcel->getActiveSheet()->getStyle("C14")->getAlignment()->setWrapText(true);
  107. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
  108. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
  109. // Rename worksheet
  110. echo date('H:i:s') , " Rename worksheet" , EOL;
  111. $objPHPExcel->getActiveSheet()->setTitle('Datatypes');
  112. // Set active sheet index to the first sheet, so Excel opens this as the first sheet
  113. $objPHPExcel->setActiveSheetIndex(0);
  114. // Save Excel 95 file
  115. echo date('H:i:s') , " Write to Excel5 format" , EOL;
  116. $callStartTime = microtime(true);
  117. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  118. $objWriter->save(str_replace('.php', '.xls', __FILE__));
  119. echo date('H:i:s') , " File written to " , str_replace('.php', '.xls', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
  120. $callEndTime = microtime(true);
  121. $callTime = $callEndTime - $callStartTime;
  122. echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
  123. echo 'Call time to write Workbook was ' , sprintf('%.4f',$callTime) , " seconds" , EOL;
  124. // Echo memory usage
  125. echo date('H:i:s') , ' Current memory usage: ' , (memory_get_usage(true) / 1024 / 1024) , " MB" , EOL;
  126. echo date('H:i:s') , " Reload workbook from saved file" , EOL;
  127. $callStartTime = microtime(true);
  128. $objPHPExcel = PHPExcel_IOFactory::load(str_replace('.php', '.xls', __FILE__));
  129. $callEndTime = microtime(true);
  130. $callTime = $callEndTime - $callStartTime;
  131. echo 'Call time to reload Workbook was ' , sprintf('%.4f',$callTime) , " seconds" , EOL;
  132. // Echo memory usage
  133. echo date('H:i:s') , ' Current memory usage: ' , (memory_get_usage(true) / 1024 / 1024) , " MB" , EOL;
  134. var_dump($objPHPExcel->getActiveSheet()->toArray());
  135. // Echo memory peak usage
  136. echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL;
  137. // Echo done
  138. echo date('H:i:s') , " Done testing file" , EOL;
  139. echo 'File has been created in ' , getcwd() , EOL;