2012-02-12 23:03:57 +00:00
|
|
|
<?php
|
2010-08-26 19:14:53 +00:00
|
|
|
/**
|
2016-08-16 23:06:51 +00:00
|
|
|
* PhpSpreadsheet
|
2010-08-26 19:14:53 +00:00
|
|
|
*
|
2016-08-16 23:06:51 +00:00
|
|
|
* Copyright (c) 2006 - 2016 PhpSpreadsheet
|
2010-08-26 19:14:53 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
2016-08-16 23:06:51 +00:00
|
|
|
* @category PhpSpreadsheet
|
|
|
|
* @copyright Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
|
|
|
|
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
2010-08-26 19:14:53 +00:00
|
|
|
* @version ##VERSION##, ##DATE##
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** Error reporting */
|
|
|
|
error_reporting(E_ALL);
|
2012-08-05 17:54:20 +00:00
|
|
|
ini_set('display_errors', TRUE);
|
2016-08-16 23:06:51 +00:00
|
|
|
ini_set('display_startup_errors', TRUE);
|
2010-08-26 19:14:53 +00:00
|
|
|
date_default_timezone_set('Europe/London');
|
|
|
|
|
2012-08-05 17:54:20 +00:00
|
|
|
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
|
|
|
|
|
2016-08-16 23:06:51 +00:00
|
|
|
/** Include PhpSpreadsheet */
|
2015-07-15 21:33:50 +00:00
|
|
|
require_once dirname(__FILE__) . '/../src/Bootstrap.php';
|
2010-08-26 19:14:53 +00:00
|
|
|
|
|
|
|
|
2016-08-16 23:06:51 +00:00
|
|
|
// Create new PhpSpreadsheet object
|
|
|
|
echo date('H:i:s') , " Create new PhpSpreadsheet object" , EOL;
|
|
|
|
$objPhpSpreadsheet = new \PhpSpreadsheet\Spreadsheet();
|
2010-08-26 19:14:53 +00:00
|
|
|
|
2012-02-12 23:03:57 +00:00
|
|
|
// Set document properties
|
2012-08-05 17:54:20 +00:00
|
|
|
echo date('H:i:s') , " Set document properties" , EOL;
|
2016-08-16 23:06:51 +00:00
|
|
|
$objPhpSpreadsheet->getProperties()
|
2015-07-15 21:33:50 +00:00
|
|
|
->setCreator("Maarten Balliauw")
|
|
|
|
->setLastModifiedBy("Maarten Balliauw")
|
|
|
|
->setTitle("Office 2007 XLSX Test Document")
|
|
|
|
->setSubject("Office 2007 XLSX Test Document")
|
|
|
|
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
|
|
|
|
->setKeywords("office 2007 openxml php")
|
|
|
|
->setCategory("Test result file");
|
2010-08-26 19:14:53 +00:00
|
|
|
|
|
|
|
// Set default font
|
2012-08-05 17:54:20 +00:00
|
|
|
echo date('H:i:s') , " Set default font" , EOL;
|
2016-08-16 23:06:51 +00:00
|
|
|
$objPhpSpreadsheet->getDefaultStyle()
|
2015-07-15 21:33:50 +00:00
|
|
|
->getFont()
|
|
|
|
->setName('Arial')
|
|
|
|
->setSize(10);
|
2010-08-26 19:14:53 +00:00
|
|
|
|
|
|
|
// Add some data, resembling some different data types
|
2012-08-05 17:54:20 +00:00
|
|
|
echo date('H:i:s') , " Add some data" , EOL;
|
2016-08-16 23:06:51 +00:00
|
|
|
$objPhpSpreadsheet->getActiveSheet()
|
2015-07-15 21:33:50 +00:00
|
|
|
->setCellValue('A1', 'String')
|
|
|
|
->setCellValue('B1', 'Simple')
|
2016-08-16 23:06:51 +00:00
|
|
|
->setCellValue('C1', 'PhpSpreadsheet');
|
2015-07-15 21:33:50 +00:00
|
|
|
|
2016-08-16 23:06:51 +00:00
|
|
|
$objPhpSpreadsheet->getActiveSheet()
|
2015-07-15 21:33:50 +00:00
|
|
|
->setCellValue('A2', 'String')
|
|
|
|
->setCellValue('B2', 'Symbols')
|
|
|
|
->setCellValue('C2', '!+&=()~§±æþ');
|
|
|
|
|
2016-08-16 23:06:51 +00:00
|
|
|
$objPhpSpreadsheet->getActiveSheet()
|
2015-07-15 21:33:50 +00:00
|
|
|
->setCellValue('A3', 'String')
|
|
|
|
->setCellValue('B3', 'UTF-8')
|
|
|
|
->setCellValue('C3', 'Создать MS Excel Книги из PHP скриптов');
|
|
|
|
|
2016-08-16 23:06:51 +00:00
|
|
|
$objPhpSpreadsheet->getActiveSheet()
|
2015-07-15 21:33:50 +00:00
|
|
|
->setCellValue('A4', 'Number')
|
|
|
|
->setCellValue('B4', 'Integer')
|
|
|
|
->setCellValue('C4', 12);
|
|
|
|
|
2016-08-16 23:06:51 +00:00
|
|
|
$objPhpSpreadsheet->getActiveSheet()
|
2015-07-15 21:33:50 +00:00
|
|
|
->setCellValue('A5', 'Number')
|
|
|
|
->setCellValue('B5', 'Float')
|
|
|
|
->setCellValue('C5', 34.56);
|
|
|
|
|
2016-08-16 23:06:51 +00:00
|
|
|
$objPhpSpreadsheet->getActiveSheet()
|
2015-07-15 21:33:50 +00:00
|
|
|
->setCellValue('A6', 'Number')
|
|
|
|
->setCellValue('B6', 'Negative')
|
|
|
|
->setCellValue('C6', -7.89);
|
|
|
|
|
2016-08-16 23:06:51 +00:00
|
|
|
$objPhpSpreadsheet->getActiveSheet()
|
2015-07-15 21:33:50 +00:00
|
|
|
->setCellValue('A7', 'Boolean')
|
|
|
|
->setCellValue('B7', 'True')
|
|
|
|
->setCellValue('C7', true);
|
|
|
|
|
2016-08-16 23:06:51 +00:00
|
|
|
$objPhpSpreadsheet->getActiveSheet()
|
2015-07-15 21:33:50 +00:00
|
|
|
->setCellValue('A8', 'Boolean')
|
|
|
|
->setCellValue('B8', 'False')
|
|
|
|
->setCellValue('C8', false);
|
2010-08-26 19:14:53 +00:00
|
|
|
|
2012-02-14 23:26:05 +00:00
|
|
|
$dateTimeNow = time();
|
2016-08-16 23:06:51 +00:00
|
|
|
$objPhpSpreadsheet->getActiveSheet()
|
2015-07-15 21:33:50 +00:00
|
|
|
->setCellValue('A9', 'Date/Time')
|
|
|
|
->setCellValue('B9', 'Date')
|
2016-08-16 23:06:51 +00:00
|
|
|
->setCellValue('C9', \PhpSpreadsheet\Shared\Date::PHPToExcel( $dateTimeNow ));
|
|
|
|
$objPhpSpreadsheet->getActiveSheet()
|
2015-07-15 21:33:50 +00:00
|
|
|
->getStyle('C9')
|
|
|
|
->getNumberFormat()
|
2016-08-16 23:06:51 +00:00
|
|
|
->setFormatCode(\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_YYYYMMDD2);
|
2015-07-15 21:33:50 +00:00
|
|
|
|
2016-08-16 23:06:51 +00:00
|
|
|
$objPhpSpreadsheet->getActiveSheet()
|
2015-07-15 21:33:50 +00:00
|
|
|
->setCellValue('A10', 'Date/Time')
|
|
|
|
->setCellValue('B10', 'Time')
|
2016-08-16 23:06:51 +00:00
|
|
|
->setCellValue('C10', \PhpSpreadsheet\Shared\Date::PHPToExcel( $dateTimeNow ));
|
|
|
|
$objPhpSpreadsheet->getActiveSheet()
|
2015-07-15 21:33:50 +00:00
|
|
|
->getStyle('C10')
|
|
|
|
->getNumberFormat()
|
2016-08-16 23:06:51 +00:00
|
|
|
->setFormatCode(\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_TIME4);
|
2015-07-15 21:33:50 +00:00
|
|
|
|
2016-08-16 23:06:51 +00:00
|
|
|
$objPhpSpreadsheet->getActiveSheet()
|
2015-07-15 21:33:50 +00:00
|
|
|
->setCellValue('A11', 'Date/Time')
|
|
|
|
->setCellValue('B11', 'Date and Time')
|
2016-08-16 23:06:51 +00:00
|
|
|
->setCellValue('C11', \PhpSpreadsheet\Shared\Date::PHPToExcel( $dateTimeNow ));
|
|
|
|
$objPhpSpreadsheet->getActiveSheet()
|
2015-07-15 21:33:50 +00:00
|
|
|
->getStyle('C11')
|
|
|
|
->getNumberFormat()
|
2016-08-16 23:06:51 +00:00
|
|
|
->setFormatCode(\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DATETIME);
|
2015-07-15 21:33:50 +00:00
|
|
|
|
2016-08-16 23:06:51 +00:00
|
|
|
$objPhpSpreadsheet->getActiveSheet()
|
2015-07-15 21:33:50 +00:00
|
|
|
->setCellValue('A12', 'NULL')
|
|
|
|
->setCellValue('C12', NULL);
|
|
|
|
|
2016-08-16 23:06:51 +00:00
|
|
|
$objRichText = new \PhpSpreadsheet\RichText();
|
2013-04-27 12:42:14 +00:00
|
|
|
$objRichText->createText('你好 ');
|
2012-10-31 22:00:40 +00:00
|
|
|
|
2013-04-27 12:42:14 +00:00
|
|
|
$objPayable = $objRichText->createTextRun('你 好 吗?');
|
|
|
|
$objPayable->getFont()->setBold(true);
|
|
|
|
$objPayable->getFont()->setItalic(true);
|
2016-08-16 23:06:51 +00:00
|
|
|
$objPayable->getFont()->setColor( new \PhpSpreadsheet\Style\Color( \PhpSpreadsheet\Style\Color::COLOR_DARKGREEN ) );
|
2013-04-27 12:42:14 +00:00
|
|
|
|
|
|
|
$objRichText->createText(', unless specified otherwise on the invoice.');
|
|
|
|
|
2016-08-16 23:06:51 +00:00
|
|
|
$objPhpSpreadsheet->getActiveSheet()
|
2015-07-15 21:33:50 +00:00
|
|
|
->setCellValue('A13', 'Rich Text')
|
|
|
|
->setCellValue('C13', $objRichText);
|
2013-04-27 12:42:14 +00:00
|
|
|
|
2013-08-15 17:10:29 +00:00
|
|
|
|
2016-08-16 23:06:51 +00:00
|
|
|
$objRichText2 = new \PhpSpreadsheet\RichText();
|
2013-08-15 17:10:29 +00:00
|
|
|
$objRichText2->createText("black text\n");
|
|
|
|
|
|
|
|
$objRed = $objRichText2->createTextRun("red text");
|
2016-08-16 23:06:51 +00:00
|
|
|
$objRed->getFont()->setColor( new \PhpSpreadsheet\Style\Color(\PhpSpreadsheet\Style\Color::COLOR_RED ) );
|
2015-07-15 21:33:50 +00:00
|
|
|
|
2016-08-16 23:06:51 +00:00
|
|
|
$objPhpSpreadsheet->getActiveSheet()
|
2015-07-15 21:33:50 +00:00
|
|
|
->getCell("C14")
|
|
|
|
->setValue($objRichText2);
|
2016-08-16 23:06:51 +00:00
|
|
|
$objPhpSpreadsheet->getActiveSheet()
|
2015-07-15 21:33:50 +00:00
|
|
|
->getStyle("C14")
|
|
|
|
->getAlignment()->setWrapText(true);
|
|
|
|
|
|
|
|
|
2016-08-16 23:06:51 +00:00
|
|
|
$objPhpSpreadsheet->getActiveSheet()->setCellValue('A17', 'Hyperlink');
|
2016-03-01 12:33:50 +00:00
|
|
|
|
2016-08-16 23:06:51 +00:00
|
|
|
$objPhpSpreadsheet->getActiveSheet()
|
|
|
|
->setCellValue('C17', 'PhpSpreadsheet Web Site');
|
|
|
|
$objPhpSpreadsheet->getActiveSheet()
|
|
|
|
->getCell('C17')
|
|
|
|
->getHyperlink()
|
|
|
|
->setUrl('https://github.com/PHPOffice/PhpSpreadsheet')
|
|
|
|
->setTooltip('Navigate to PhpSpreadsheet website');
|
2016-03-01 12:33:50 +00:00
|
|
|
|
2016-08-16 23:06:51 +00:00
|
|
|
$objPhpSpreadsheet->getActiveSheet()
|
|
|
|
->setCellValue('C18', '=HYPERLINK("mailto:abc@def.com","abc@def.com")');
|
2016-03-01 12:33:50 +00:00
|
|
|
|
|
|
|
|
2016-08-16 23:06:51 +00:00
|
|
|
$objPhpSpreadsheet->getActiveSheet()
|
2015-07-15 21:33:50 +00:00
|
|
|
->getColumnDimension('B')
|
|
|
|
->setAutoSize(true);
|
2016-08-16 23:06:51 +00:00
|
|
|
$objPhpSpreadsheet->getActiveSheet()
|
2015-07-15 21:33:50 +00:00
|
|
|
->getColumnDimension('C')
|
|
|
|
->setAutoSize(true);
|
2012-02-12 23:03:57 +00:00
|
|
|
|
|
|
|
// Rename worksheet
|
2012-08-05 17:54:20 +00:00
|
|
|
echo date('H:i:s') , " Rename worksheet" , EOL;
|
2016-08-16 23:06:51 +00:00
|
|
|
$objPhpSpreadsheet->getActiveSheet()->setTitle('Datatypes');
|
2010-08-26 19:14:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
|
2016-08-16 23:06:51 +00:00
|
|
|
$objPhpSpreadsheet->setActiveSheetIndex(0);
|
2010-08-26 19:14:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Save Excel 2007 file
|
2012-08-05 17:54:20 +00:00
|
|
|
echo date('H:i:s') , " Write to Excel2007 format" , EOL;
|
2012-11-28 21:30:58 +00:00
|
|
|
$callStartTime = microtime(true);
|
|
|
|
|
2016-08-16 23:06:51 +00:00
|
|
|
$objWriter = \PhpSpreadsheet\IOFactory::createWriter($objPhpSpreadsheet, 'Excel2007');
|
2010-08-26 19:14:53 +00:00
|
|
|
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
|
2012-11-28 21:30:58 +00:00
|
|
|
$callEndTime = microtime(true);
|
|
|
|
$callTime = $callEndTime - $callStartTime;
|
|
|
|
|
2012-08-05 17:54:20 +00:00
|
|
|
echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
|
2012-11-28 21:30:58 +00:00
|
|
|
echo 'Call time to write Workbook was ' , sprintf('%.4f',$callTime) , " seconds" , EOL;
|
|
|
|
// Echo memory usage
|
|
|
|
echo date('H:i:s') , ' Current memory usage: ' , (memory_get_usage(true) / 1024 / 1024) , " MB" , EOL;
|
2010-08-26 19:14:53 +00:00
|
|
|
|
|
|
|
|
2016-08-16 23:06:51 +00:00
|
|
|
// Save Excel 5 file
|
|
|
|
echo date('H:i:s') , " Write to Excel5 format" , EOL;
|
2012-11-28 21:30:58 +00:00
|
|
|
$callStartTime = microtime(true);
|
|
|
|
|
2016-08-16 23:06:51 +00:00
|
|
|
$objWriter = \PhpSpreadsheet\IOFactory::createWriter($objPhpSpreadsheet, 'Excel5');
|
|
|
|
$objWriter->save(str_replace('.php', '.xls', __FILE__));
|
2012-11-28 21:30:58 +00:00
|
|
|
$callEndTime = microtime(true);
|
|
|
|
$callTime = $callEndTime - $callStartTime;
|
2016-08-16 23:06:51 +00:00
|
|
|
|
|
|
|
echo date('H:i:s') , " File written to " , str_replace('.php', '.xls', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
|
|
|
|
echo 'Call time to write Workbook was ' , sprintf('%.4f',$callTime) , " seconds" , EOL;
|
2012-11-28 21:30:58 +00:00
|
|
|
// Echo memory usage
|
|
|
|
echo date('H:i:s') , ' Current memory usage: ' , (memory_get_usage(true) / 1024 / 1024) , " MB" , EOL;
|
|
|
|
|
|
|
|
|
2010-08-26 19:14:53 +00:00
|
|
|
// Echo memory peak usage
|
2012-08-05 17:54:20 +00:00
|
|
|
echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL;
|
2010-08-26 19:14:53 +00:00
|
|
|
|
|
|
|
// Echo done
|
2012-11-28 21:30:58 +00:00
|
|
|
echo date('H:i:s') , " Done testing file" , EOL;
|
2016-08-16 23:06:51 +00:00
|
|
|
echo 'Files have been created in ' , getcwd() , EOL;
|