Commit ebb83dbd authored by hujun's avatar hujun

composer require mk-j/php_xlsxwriter

parent 43497fc1
...@@ -25,7 +25,8 @@ ...@@ -25,7 +25,8 @@
"topthink/think-queue": "1.*", "topthink/think-queue": "1.*",
"topthink/think-image": "1.*", "topthink/think-image": "1.*",
"firebase/php-jwt": "^5.0", "firebase/php-jwt": "^5.0",
"endroid/qrcode": "^2.5" "endroid/qrcode": "^2.5",
"mk-j/php_xlsxwriter": "^0.37.0"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
......
...@@ -379,9 +379,9 @@ class ClassLoader ...@@ -379,9 +379,9 @@ class ClassLoader
$subPath = substr($subPath, 0, $lastPos); $subPath = substr($subPath, 0, $lastPos);
$search = $subPath.'\\'; $search = $subPath.'\\';
if (isset($this->prefixDirsPsr4[$search])) { if (isset($this->prefixDirsPsr4[$search])) {
$pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1);
foreach ($this->prefixDirsPsr4[$search] as $dir) { foreach ($this->prefixDirsPsr4[$search] as $dir) {
$length = $this->prefixLengthsPsr4[$first][$search]; if (file_exists($file = $dir . $pathEnd)) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) {
return $file; return $file;
} }
} }
......
...@@ -459,6 +459,8 @@ return array( ...@@ -459,6 +459,8 @@ return array(
'SessionUpdateTimestampHandlerInterface' => $vendorDir . '/symfony/polyfill-php70/Resources/stubs/SessionUpdateTimestampHandlerInterface.php', 'SessionUpdateTimestampHandlerInterface' => $vendorDir . '/symfony/polyfill-php70/Resources/stubs/SessionUpdateTimestampHandlerInterface.php',
'Text_Template' => $vendorDir . '/phpunit/php-text-template/src/Template.php', 'Text_Template' => $vendorDir . '/phpunit/php-text-template/src/Template.php',
'TypeError' => $vendorDir . '/symfony/polyfill-php70/Resources/stubs/TypeError.php', 'TypeError' => $vendorDir . '/symfony/polyfill-php70/Resources/stubs/TypeError.php',
'XLSXWriter' => $vendorDir . '/mk-j/php_xlsxwriter/xlsxwriter.class.php',
'XLSXWriter_BuffererWriter' => $vendorDir . '/mk-j/php_xlsxwriter/xlsxwriter.class.php',
'Zxing\\Binarizer' => $vendorDir . '/khanamiryan/qrcode-detector-decoder/lib/Binarizer.php', 'Zxing\\Binarizer' => $vendorDir . '/khanamiryan/qrcode-detector-decoder/lib/Binarizer.php',
'Zxing\\BinaryBitmap' => $vendorDir . '/khanamiryan/qrcode-detector-decoder/lib/BinaryBitmap.php', 'Zxing\\BinaryBitmap' => $vendorDir . '/khanamiryan/qrcode-detector-decoder/lib/BinaryBitmap.php',
'Zxing\\ChecksumException' => $vendorDir . '/khanamiryan/qrcode-detector-decoder/lib/ChecksumException.php', 'Zxing\\ChecksumException' => $vendorDir . '/khanamiryan/qrcode-detector-decoder/lib/ChecksumException.php',
......
...@@ -7,12 +7,12 @@ $baseDir = dirname($vendorDir); ...@@ -7,12 +7,12 @@ $baseDir = dirname($vendorDir);
return array( return array(
'5255c38a0faeba867671b61dfda6d864' => $vendorDir . '/paragonie/random_compat/lib/random.php', '5255c38a0faeba867671b61dfda6d864' => $vendorDir . '/paragonie/random_compat/lib/random.php',
'023d27dca8066ef29e6739335ea73bad' => $vendorDir . '/symfony/polyfill-php70/bootstrap.php',
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => $vendorDir . '/symfony/polyfill-mbstring/bootstrap.php', '0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => $vendorDir . '/symfony/polyfill-mbstring/bootstrap.php',
'023d27dca8066ef29e6739335ea73bad' => $vendorDir . '/symfony/polyfill-php70/bootstrap.php',
'9b552a3cc426e3287cc811caefa3cf53' => $vendorDir . '/topthink/think-helper/src/helper.php', '9b552a3cc426e3287cc811caefa3cf53' => $vendorDir . '/topthink/think-helper/src/helper.php',
'626dcc41390ebdaa619faa02d99943b0' => $vendorDir . '/khanamiryan/qrcode-detector-decoder/lib/common/customFunctions.php', '626dcc41390ebdaa619faa02d99943b0' => $vendorDir . '/khanamiryan/qrcode-detector-decoder/lib/common/customFunctions.php',
'1cfd2761b63b0a29ed23657ea394cb2d' => $vendorDir . '/topthink/think-captcha/src/helper.php', '1cfd2761b63b0a29ed23657ea394cb2d' => $vendorDir . '/topthink/think-captcha/src/helper.php',
'ddc3cd2a04224f9638c5d0de6a69c7e3' => $vendorDir . '/topthink/think-migration/src/config.php', 'ddc3cd2a04224f9638c5d0de6a69c7e3' => $vendorDir . '/topthink/think-migration/src/config.php',
'72c97b53391125cae04082a81029f42d' => $vendorDir . '/topthink/think-testing/src/config.php',
'cc56288302d9df745d97c934d6a6e5f0' => $vendorDir . '/topthink/think-queue/src/common.php', 'cc56288302d9df745d97c934d6a6e5f0' => $vendorDir . '/topthink/think-queue/src/common.php',
'72c97b53391125cae04082a81029f42d' => $vendorDir . '/topthink/think-testing/src/config.php',
); );
...@@ -12,8 +12,8 @@ return array( ...@@ -12,8 +12,8 @@ return array(
'think\\helper\\' => array($vendorDir . '/topthink/think-helper/src'), 'think\\helper\\' => array($vendorDir . '/topthink/think-helper/src'),
'think\\composer\\' => array($vendorDir . '/topthink/think-installer/src'), 'think\\composer\\' => array($vendorDir . '/topthink/think-installer/src'),
'think\\captcha\\' => array($vendorDir . '/topthink/think-captcha/src'), 'think\\captcha\\' => array($vendorDir . '/topthink/think-captcha/src'),
'think\\' => array($baseDir . '/thinkphp/library/think', $vendorDir . '/topthink/think-queue/src', $vendorDir . '/topthink/think-image/src'), 'think\\' => array($baseDir . '/thinkphp/library/think', $vendorDir . '/topthink/think-image/src', $vendorDir . '/topthink/think-queue/src'),
'phpDocumentor\\Reflection\\' => array($vendorDir . '/phpdocumentor/reflection-common/src', $vendorDir . '/phpdocumentor/type-resolver/src', $vendorDir . '/phpdocumentor/reflection-docblock/src'), 'phpDocumentor\\Reflection\\' => array($vendorDir . '/phpdocumentor/reflection-common/src', $vendorDir . '/phpdocumentor/reflection-docblock/src', $vendorDir . '/phpdocumentor/type-resolver/src'),
'app\\' => array($baseDir . '/application'), 'app\\' => array($baseDir . '/application'),
'Webmozart\\Assert\\' => array($vendorDir . '/webmozart/assert/src'), 'Webmozart\\Assert\\' => array($vendorDir . '/webmozart/assert/src'),
'Symfony\\Polyfill\\Php70\\' => array($vendorDir . '/symfony/polyfill-php70'), 'Symfony\\Polyfill\\Php70\\' => array($vendorDir . '/symfony/polyfill-php70'),
......
...@@ -8,14 +8,14 @@ class ComposerStaticInit563e23492907a0ecb0dae7ce3a76ccdc ...@@ -8,14 +8,14 @@ class ComposerStaticInit563e23492907a0ecb0dae7ce3a76ccdc
{ {
public static $files = array ( public static $files = array (
'5255c38a0faeba867671b61dfda6d864' => __DIR__ . '/..' . '/paragonie/random_compat/lib/random.php', '5255c38a0faeba867671b61dfda6d864' => __DIR__ . '/..' . '/paragonie/random_compat/lib/random.php',
'023d27dca8066ef29e6739335ea73bad' => __DIR__ . '/..' . '/symfony/polyfill-php70/bootstrap.php',
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => __DIR__ . '/..' . '/symfony/polyfill-mbstring/bootstrap.php', '0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => __DIR__ . '/..' . '/symfony/polyfill-mbstring/bootstrap.php',
'023d27dca8066ef29e6739335ea73bad' => __DIR__ . '/..' . '/symfony/polyfill-php70/bootstrap.php',
'9b552a3cc426e3287cc811caefa3cf53' => __DIR__ . '/..' . '/topthink/think-helper/src/helper.php', '9b552a3cc426e3287cc811caefa3cf53' => __DIR__ . '/..' . '/topthink/think-helper/src/helper.php',
'626dcc41390ebdaa619faa02d99943b0' => __DIR__ . '/..' . '/khanamiryan/qrcode-detector-decoder/lib/common/customFunctions.php', '626dcc41390ebdaa619faa02d99943b0' => __DIR__ . '/..' . '/khanamiryan/qrcode-detector-decoder/lib/common/customFunctions.php',
'1cfd2761b63b0a29ed23657ea394cb2d' => __DIR__ . '/..' . '/topthink/think-captcha/src/helper.php', '1cfd2761b63b0a29ed23657ea394cb2d' => __DIR__ . '/..' . '/topthink/think-captcha/src/helper.php',
'ddc3cd2a04224f9638c5d0de6a69c7e3' => __DIR__ . '/..' . '/topthink/think-migration/src/config.php', 'ddc3cd2a04224f9638c5d0de6a69c7e3' => __DIR__ . '/..' . '/topthink/think-migration/src/config.php',
'72c97b53391125cae04082a81029f42d' => __DIR__ . '/..' . '/topthink/think-testing/src/config.php',
'cc56288302d9df745d97c934d6a6e5f0' => __DIR__ . '/..' . '/topthink/think-queue/src/common.php', 'cc56288302d9df745d97c934d6a6e5f0' => __DIR__ . '/..' . '/topthink/think-queue/src/common.php',
'72c97b53391125cae04082a81029f42d' => __DIR__ . '/..' . '/topthink/think-testing/src/config.php',
); );
public static $prefixLengthsPsr4 = array ( public static $prefixLengthsPsr4 = array (
...@@ -101,14 +101,14 @@ class ComposerStaticInit563e23492907a0ecb0dae7ce3a76ccdc ...@@ -101,14 +101,14 @@ class ComposerStaticInit563e23492907a0ecb0dae7ce3a76ccdc
'think\\' => 'think\\' =>
array ( array (
0 => __DIR__ . '/../..' . '/thinkphp/library/think', 0 => __DIR__ . '/../..' . '/thinkphp/library/think',
1 => __DIR__ . '/..' . '/topthink/think-queue/src', 1 => __DIR__ . '/..' . '/topthink/think-image/src',
2 => __DIR__ . '/..' . '/topthink/think-image/src', 2 => __DIR__ . '/..' . '/topthink/think-queue/src',
), ),
'phpDocumentor\\Reflection\\' => 'phpDocumentor\\Reflection\\' =>
array ( array (
0 => __DIR__ . '/..' . '/phpdocumentor/reflection-common/src', 0 => __DIR__ . '/..' . '/phpdocumentor/reflection-common/src',
1 => __DIR__ . '/..' . '/phpdocumentor/type-resolver/src', 1 => __DIR__ . '/..' . '/phpdocumentor/reflection-docblock/src',
2 => __DIR__ . '/..' . '/phpdocumentor/reflection-docblock/src', 2 => __DIR__ . '/..' . '/phpdocumentor/type-resolver/src',
), ),
'app\\' => 'app\\' =>
array ( array (
...@@ -639,6 +639,8 @@ class ComposerStaticInit563e23492907a0ecb0dae7ce3a76ccdc ...@@ -639,6 +639,8 @@ class ComposerStaticInit563e23492907a0ecb0dae7ce3a76ccdc
'SessionUpdateTimestampHandlerInterface' => __DIR__ . '/..' . '/symfony/polyfill-php70/Resources/stubs/SessionUpdateTimestampHandlerInterface.php', 'SessionUpdateTimestampHandlerInterface' => __DIR__ . '/..' . '/symfony/polyfill-php70/Resources/stubs/SessionUpdateTimestampHandlerInterface.php',
'Text_Template' => __DIR__ . '/..' . '/phpunit/php-text-template/src/Template.php', 'Text_Template' => __DIR__ . '/..' . '/phpunit/php-text-template/src/Template.php',
'TypeError' => __DIR__ . '/..' . '/symfony/polyfill-php70/Resources/stubs/TypeError.php', 'TypeError' => __DIR__ . '/..' . '/symfony/polyfill-php70/Resources/stubs/TypeError.php',
'XLSXWriter' => __DIR__ . '/..' . '/mk-j/php_xlsxwriter/xlsxwriter.class.php',
'XLSXWriter_BuffererWriter' => __DIR__ . '/..' . '/mk-j/php_xlsxwriter/xlsxwriter.class.php',
'Zxing\\Binarizer' => __DIR__ . '/..' . '/khanamiryan/qrcode-detector-decoder/lib/Binarizer.php', 'Zxing\\Binarizer' => __DIR__ . '/..' . '/khanamiryan/qrcode-detector-decoder/lib/Binarizer.php',
'Zxing\\BinaryBitmap' => __DIR__ . '/..' . '/khanamiryan/qrcode-detector-decoder/lib/BinaryBitmap.php', 'Zxing\\BinaryBitmap' => __DIR__ . '/..' . '/khanamiryan/qrcode-detector-decoder/lib/BinaryBitmap.php',
'Zxing\\ChecksumException' => __DIR__ . '/..' . '/khanamiryan/qrcode-detector-decoder/lib/ChecksumException.php', 'Zxing\\ChecksumException' => __DIR__ . '/..' . '/khanamiryan/qrcode-detector-decoder/lib/ChecksumException.php',
......
This source diff could not be displayed because it is too large. You can view the blob instead.
vendor
.idea
\ No newline at end of file
The MIT License (MIT)
Copyright (c) 2013 Mark Jones
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
PHP_XLSXWriter
==============
This library is designed to be lightweight, and have minimal memory usage.
It is designed to output an Excel compatible spreadsheet in (Office 2007+) xlsx format, with just basic features supported:
* supports PHP 5.2.1+
* takes UTF-8 encoded input
* multiple worksheets
* supports currency/date/numeric cell formatting, simple formulas
* supports basic cell styling
* supports writing huge 100K+ row spreadsheets
[Never run out of memory with PHPExcel again](https://github.com/mk-j/PHP_XLSXWriter).
Simple PHP CLI example:
```php
$data = array(
array('year','month','amount'),
array('2003','1','220'),
array('2003','2','153.5'),
);
$writer = new XLSXWriter();
$writer->writeSheet($data);
$writer->writeToFile('output.xlsx');
```
Simple/Advanced Cell Formats:
```php
$header = array(
'created'=>'date',
'product_id'=>'integer',
'quantity'=>'#,##0',
'amount'=>'price',
'description'=>'string',
'tax'=>'[$$-1009]#,##0.00;[RED]-[$$-1009]#,##0.00',
);
$data = array(
array('2015-01-01',873,1,'44.00','misc','=D2*0.05'),
array('2015-01-12',324,2,'88.00','none','=D3*0.05'),
);
$writer = new XLSXWriter();
$writer->writeSheetHeader('Sheet1', $header );
foreach($data as $row)
$writer->writeSheetRow('Sheet1', $row );
$writer->writeToFile('example.xlsx');
```
50000 rows: (1.4s, 0MB memory usage)
```php
include_once("xlsxwriter.class.php");
$writer = new XLSXWriter();
$writer->writeSheetHeader('Sheet1', array('c1'=>'integer','c2'=>'integer','c3'=>'integer','c4'=>'integer') );
for($i=0; $i<50000; $i++)
{
$writer->writeSheetRow('Sheet1', array($i, $i+1, $i+2, $i+3) );
}
$writer->writeToFile('huge.xlsx');
echo '#'.floor((memory_get_peak_usage())/1024/1024)."MB"."\n";
```
| rows | time | memory |
| ------ | ---- | ------ |
| 50000 | 1.4s | 0MB |
| 100000 | 2.7s | 0MB |
| 150000 | 4.1s | 0MB |
| 200000 | 5.7s | 0MB |
| 250000 | 7.0s | 0MB |
Simple cell formats map to more advanced cell formats
| simple formats | format code |
| ---------- | ---- |
| string | @ |
| integer | 0 |
| date | YYYY-MM-DD |
| datetime | YYYY-MM-DD HH:MM:SS |
| price | #,##0.00 |
| dollar | [$$-1009]#,##0.00;[RED]-[$$-1009]#,##0.00 |
| euro | #,##0.00 [$€-407];[RED]-#,##0.00 [$€-407] |
Basic cell styles have been available since version 0.30
| style | allowed values |
| ------------ | ---- |
| font | Arial, Times New Roman, Courier New, Comic Sans MS |
| font-size | 8,9,10,11,12 ... |
| font-style | bold, italic, underline, strikethrough or multiple ie: 'bold,italic' |
| border | left, right, top, bottom, or multiple ie: 'top,left' |
| border-style | thin, medium, thick, dashDot, dashDotDot, dashed, dotted, double, hair, mediumDashDot, mediumDashDotDot, mediumDashed, slantDashDot |
| border-color | #RRGGBB, ie: #ff99cc or #f9c |
| color | #RRGGBB, ie: #ff99cc or #f9c |
| fill | #RRGGBB, ie: #eeffee or #efe |
| halign | general, left, right, justify, center |
| valign | bottom, center, distributed |
{
"name": "mk-j/php_xlsxwriter",
"description": "PHP Library to write XLSX files",
"keywords": ["php", "library","xls", "xlsx", "excel"],
"type": "project",
"homepage": "https://github.com/mk-j/PHP_XLSXWriter",
"license": "MIT",
"autoload": {
"classmap": ["xlsxwriter.class.php"]
},
"require-dev": {
"phpunit/phpunit": "4.3.*"
},
"require": {
"php": ">=5.2.1",
"ext-zip": "*"
}
}
<?php
include_once("xlsxwriter.class.php");
ini_set('display_errors', 0);
ini_set('log_errors', 1);
error_reporting(E_ALL & ~E_NOTICE);
$filename = "example.xlsx";
$rows = array(
array('2003','1','-50.5','2010-01-01 23:00:00','2012-12-31 23:00:00'),
array('2003','=B1', '23.5','2010-01-01 00:00:00','2012-12-31 00:00:00'),
);
$writer = new XLSXWriter();
$writer->setAuthor('Some Author');
foreach($rows as $row)
$writer->writeSheetRow('Sheet1', $row);
$writer->writeToFile('example.xlsx');
<?php
include_once("xlsxwriter.class.php");
ini_set('display_errors', 0);
ini_set('log_errors', 1);
error_reporting(E_ALL & ~E_NOTICE);
$filename = "example.xlsx";
header('Content-disposition: attachment; filename="'.XLSXWriter::sanitize_filename($filename).'"');
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
$rows = array(
array('2003','1','-50.5','2010-01-01 23:00:00','2012-12-31 23:00:00'),
array('2003','=B1', '23.5','2010-01-01 00:00:00','2012-12-31 00:00:00'),
);
$writer = new XLSXWriter();
$writer->setAuthor('Some Author');
foreach($rows as $row)
$writer->writeSheetRow('Sheet1', $row);
$writer->writeToStdOut();
//$writer->writeToFile('example.xlsx');
//echo $writer->writeToString();
exit(0);
<?php
set_include_path( get_include_path().PATH_SEPARATOR."..");
include_once("xlsxwriter.class.php");
$header = array(
'c1-text'=>'string',//text
'c2-text'=>'@',//text
'c3-integer'=>'integer',
'c4-integer'=>'0',
'c5-price'=>'price',
'c6-price'=>'#,##0.00',//custom
'c7-date'=>'date',
'c8-date'=>'YYYY-MM-DD',
);
$rows = array(
array('x101',102,103,104,105,106,'2018-01-07','2018-01-08'),
array('x201',202,203,204,205,206,'2018-02-07','2018-02-08'),
array('x301',302,303,304,305,306,'2018-03-07','2018-03-08'),
array('x401',402,403,404,405,406,'2018-04-07','2018-04-08'),
array('x501',502,503,504,505,506,'2018-05-07','2018-05-08'),
array('x601',602,603,604,605,606,'2018-06-07','2018-06-08'),
array('x701',702,703,704,705,706,'2018-07-07','2018-07-08'),
);
$writer = new XLSXWriter();
$writer->writeSheetHeader('Sheet1', $header);
foreach($rows as $row)
$writer->writeSheetRow('Sheet1', $row);
//$writer->writeSheet($rows,'Sheet1', $header);//or write the whole sheet in 1 call
$writer->writeToFile('xlsx-simple.xlsx');
//$writer->writeToStdOut();
//echo $writer->writeToString();
<?php
set_include_path( get_include_path().PATH_SEPARATOR."..");
include_once("xlsxwriter.class.php");
$header = array(
'year'=>'string',
'month'=>'string',
'amount'=>'price',
'first_event'=>'datetime',
'second_event'=>'date',
);
$data1 = array(
array('2003','1','-50.5','2010-01-01 23:00:00','2012-12-31 23:00:00'),
array('2003','=B2', '23.5','2010-01-01 00:00:00','2012-12-31 00:00:00'),
array('2003',"'=B2", '23.5','2010-01-01 00:00:00','2012-12-31 00:00:00'),
);
$data2 = array(
array('2003','01','343.12','4000000000'),
array('2003','02','345.12','2000000000'),
);
$writer = new XLSXWriter();
$writer->writeSheetHeader('Sheet1', $header);
foreach($data1 as $row)
$writer->writeSheetRow('Sheet1', $row);
foreach($data2 as $row)
$writer->writeSheetRow('Sheet2', $row);
$writer->writeToFile('xlsx-sheets.xlsx');
//$writer->writeToStdOut();
//echo $writer->writeToString();
exit(0);
<?php
set_include_path( get_include_path().PATH_SEPARATOR."..");
include_once("xlsxwriter.class.php");
$sheet1header = array(
'c1-string'=>'string',
'c2-integer'=>'integer',
'c3-custom-integer'=>'0',
'c4-custom-1decimal'=>'0.0',
'c5-custom-2decimal'=>'0.00',
'c6-custom-percent'=>'0%',
'c7-custom-percent1'=>'0.0%',
'c8-custom-percent2'=>'0.00%',
'c9-custom-text'=>'@',//text
);
$sheet2header = array(
'col1-date'=>'date',
'col2-datetime'=>'datetime',
'custom-date1'=>'YYYY-MM-DD',
'custom-date2'=>'MM/DD/YYYY',
'custom-date3'=>'DD-MMM-YYYY HH:MM AM/PM',
'custom-date4'=>'MM/DD/YYYY HH:MM:SS',
'custom-date5'=>'YYYY-MM-DD HH:MM:SS',
'custom-date6'=>'YY MMMM',
'custom-date7'=>'QQ YYYY',
);
$sheet3header = array(
'col1-dollar'=>'dollar',
'col2-euro'=>'euro',
'custom-amount1'=>'0',
'custom-amount2'=>'0.0',//1 decimal place
'custom-amount3'=>'0.00',//2 decimal places
'custom-currency1'=>'#,##0.00',//currency 2 decimal places, no currency/dollar sign
'custom-currency2'=>'[$$-1009]#,##0.00;[RED]-[$$-1009]#,##0.00',//w/dollar sign
'custom-currency3'=>'#,##0.00 [$€-407];[RED]-#,##0.00 [$€-407]',//w/euro sign
'custom-currency4'=>'[$¥-411]#,##0;[RED]-[$¥-411]#,##0', //japanese yen
'custom-scientific'=>'0.00E+000',//-1.23E+003 scientific notation
);
$pi = 3.14159;
$date = '2018-12-31 23:59:59';
$amount = '5120.5';
$writer = new XLSXWriter();
$writer->setAuthor('Some Author');
$writer->writeSheetHeader('BasicFormats',$sheet1header);
$writer->writeSheetRow('BasicFormats',array($pi,$pi,$pi,$pi,$pi,$pi,$pi,$pi,$pi) );
$writer->writeSheetHeader('Dates',$sheet2header);
$writer->writeSheetRow('Dates',array($date,$date,$date,$date,$date,$date,$date,$date,$date) );
$writer->writeSheetHeader('Currencies',$sheet3header);
$writer->writeSheetRow('Currencies',array($amount,$amount,$amount,$amount,$amount,$amount,$amount,$amount,$amount) );
$writer->writeToFile('xlsx-formats.xlsx');
//$writer->writeToStdOut();
//echo $writer->writeToString();
echo '#'.floor((memory_get_peak_usage())/1024/1024)."MB"."\n";
exit(0);
<?php
set_include_path( get_include_path().PATH_SEPARATOR."..");
include_once("xlsxwriter.class.php");
$writer = new XLSXWriter();
$styles1 = array( 'font'=>'Arial','font-size'=>10,'font-style'=>'bold', 'fill'=>'#eee', 'halign'=>'center', 'border'=>'left,right,top,bottom');
$styles2 = array( ['font-size'=>6],['font-size'=>8],['font-size'=>10],['font-size'=>16] );
$styles3 = array( ['font'=>'Arial'],['font'=>'Courier New'],['font'=>'Times New Roman'],['font'=>'Comic Sans MS']);
$styles4 = array( ['font-style'=>'bold'],['font-style'=>'italic'],['font-style'=>'underline'],['font-style'=>'strikethrough']);
$styles5 = array( ['color'=>'#f00'],['color'=>'#0f0'],['color'=>'#00f'],['color'=>'#666']);
$styles6 = array( ['fill'=>'#ffc'],['fill'=>'#fcf'],['fill'=>'#ccf'],['fill'=>'#cff']);
$styles7 = array( 'border'=>'left,right,top,bottom');
$styles8 = array( ['halign'=>'left'],['halign'=>'right'],['halign'=>'center'],['halign'=>'none']);
$styles9 = array( array(),['border'=>'left,top,bottom'],['border'=>'top,bottom'],['border'=>'top,bottom,right']);
$writer->writeSheetRow('Sheet1', $rowdata = array(300,234,456,789), $styles1 );
$writer->writeSheetRow('Sheet1', $rowdata = array(300,234,456,789), $styles2 );
$writer->writeSheetRow('Sheet1', $rowdata = array(300,234,456,789), $styles3 );
$writer->writeSheetRow('Sheet1', $rowdata = array(300,234,456,789), $styles4 );
$writer->writeSheetRow('Sheet1', $rowdata = array(300,234,456,789), $styles5 );
$writer->writeSheetRow('Sheet1', $rowdata = array(300,234,456,789), $styles6 );
$writer->writeSheetRow('Sheet1', $rowdata = array(300,234,456,789), $styles7 );
$writer->writeSheetRow('Sheet1', $rowdata = array(300,234,456,789), $styles8 );
$writer->writeSheetRow('Sheet1', $rowdata = array(300,234,456,789), $styles9 );
$writer->writeToFile('xlsx-styles.xlsx');
<?php
set_include_path( get_include_path().PATH_SEPARATOR."..");
include_once("xlsxwriter.class.php");
$writer = new XLSXWriter();
$colors = array('ff','cc','99','66','33','00');
foreach($colors as $b) {
foreach($colors as $g) {
$rowdata = array();
$rowstyle = array();
foreach($colors as $r) {
$rowdata[] = "#$r$g$b";
$rowstyle[] = array('fill'=>"#$r$g$b");
}
$writer->writeSheetRow('Sheet1', $rowdata, $rowstyle );
}
}
$writer->writeToFile('xlsx-colors.xlsx');
<?php
set_include_path( get_include_path().PATH_SEPARATOR."..");
include_once("xlsxwriter.class.php");
$writer = new XLSXWriter();
$writer->writeSheetHeader('Sheet1', array('c1'=>'integer','c2'=>'integer','c3'=>'integer','c4'=>'integer') );//optional
for($i=0; $i<250000; $i++)
{
$writer->writeSheetRow('Sheet1', array(rand()%10000,rand()%10000,rand()%10000,rand()%10000) );
}
$writer->writeToFile('xlsx-numbers-250k.xlsx');
echo '#'.floor((memory_get_peak_usage())/1024/1024)."MB"."\n";
<?php
set_include_path( get_include_path().PATH_SEPARATOR."..");
include_once("xlsxwriter.class.php");
$chars = "abcdefghijklmnopqrstuvwxyz0123456789 ";
$s = '';
for($j=0; $j<16192;$j++)
$s.= $chars[rand()%36];
$writer = new XLSXWriter();
$writer->writeSheetHeader('Sheet1', array('c1'=>'string','c2'=>'string','c3'=>'string','c4'=>'string') );//optional
for($i=0; $i<250000; $i++)
{
$s1 = substr($s, rand()%4000, rand()%5+5);
$s2 = substr($s, rand()%8000, rand()%5+5);
$s3 = substr($s, rand()%12000, rand()%5+5);
$s4 = substr($s, rand()%16000, rand()%5+5);
$writer->writeSheetRow('Sheet1', array($s1, $s2, $s3, $s4) );
}
$writer->writeToFile('xlsx-strings-250k.xlsx');
echo '#'.floor((memory_get_peak_usage())/1024/1024)."MB"."\n";
<?php
set_include_path( get_include_path().PATH_SEPARATOR."..");
include_once("xlsxwriter.class.php");
$writer = new XLSXWriter();
$writer->writeSheetHeader('Sheet1', $rowdata = array(300,234,456,789), $col_options = ['widths'=>[10,20,30,40]] );
$writer->writeSheetRow('Sheet1', $rowdata = array(300,234,456,789), $row_options = ['height'=>20] );
$writer->writeSheetRow('Sheet1', $rowdata = array(300,234,456,789), $row_options = ['height'=>30] );
$writer->writeSheetRow('Sheet1', $rowdata = array(300,234,456,789), $row_options = ['height'=>40] );
$writer->writeToFile('xlsx-widths.xlsx');
<?php
set_include_path( get_include_path().PATH_SEPARATOR."..");
include_once("xlsxwriter.class.php");
$writer = new XLSXWriter();
$keywords = array('some','interesting','keywords');
$writer->setTitle('Some Title');
$writer->setSubject('Some Subject');
$writer->setAuthor('Some Author');
$writer->setCompany('Some Company');
$writer->setKeywords($keywords);
$writer->setDescription('Some interesting description');
$writer->setTempDir(sys_get_temp_dir());//set custom tempdir
//----
$sheet1 = 'merged_cells';
$header = array("string","string","string","string","string");
$rows = array(
array("Merge Cells Example"),
array(100, 200, 300, 400, 500),
array(110, 210, 310, 410, 510),
);
$writer->writeSheetHeader($sheet1, $header, $col_options = ['suppress_row'=>true] );
foreach($rows as $row)
$writer->writeSheetRow($sheet1, $row);
$writer->markMergedCell($sheet1, $start_row=0, $start_col=0, $end_row=0, $end_col=4);
//----
$sheet2 = 'utf8';
$rows = array(
array('Spreadsheet','_'),
array("Hoja de cálculo", "Hoja de c\xc3\xa1lculo"),
array("Електронна таблица", "\xd0\x95\xd0\xbb\xd0\xb5\xd0\xba\xd1\x82\xd1\x80\xd0\xbe\xd0\xbd\xd0\xbd\xd0\xb0 \xd1\x82\xd0\xb0\xd0\xb1\xd0\xbb\xd0\xb8\xd1\x86\xd0\xb0"),//utf8 encoded
array("電子試算表", "\xe9\x9b\xbb\xe5\xad\x90\xe8\xa9\xa6\xe7\xae\x97\xe8\xa1\xa8"),//utf8 encoded
);
$writer->writeSheet($rows, $sheet2);
//----
$sheet3 = 'fonts';
$format = array('font'=>'Arial','font-size'=>10,'font-style'=>'bold,italic', 'fill'=>'#eee','color'=>'#f00','fill'=>'#ffc', 'border'=>'top,bottom', 'halign'=>'center');
$writer->writeSheetRow($sheet3, $row=array(101,102,103,104,105,106,107,108,109,110), $format);
$writer->writeSheetRow($sheet3, $row=array(201,202,203,204,205,206,207,208,209,210), $format);
//----
$sheet4 = 'row_options';
$writer->writeSheetHeader($sheet4, ["col1"=>"string", "col2"=>"string"], $col_options = array('widths'=>[10,10]) );
$writer->writeSheetRow($sheet4, array(101,'this text will wrap' ), $row_options = array('height'=>30,'wrap_text'=>true));
$writer->writeSheetRow($sheet4, array(201,'this text is hidden' ), $row_options = array('height'=>30,'hidden'=>true));
$writer->writeSheetRow($sheet4, array(301,'this text will not wrap'), $row_options = array('height'=>30,'collapsed'=>true));
$writer->writeToFile('xlsx-advanced.xlsx');
<?php
set_include_path( get_include_path().PATH_SEPARATOR."..");
include_once("xlsxwriter.class.php");
$chars = 'abcdefgh';
$writer = new XLSXWriter();
$writer->writeSheetHeader('Sheet1', array('col-string'=>'string','col-numbers'=>'integer','col-timestamps'=>'datetime'), ['auto_filter'=>true, 'widths'=>[15,15,30]] );
for($i=0; $i<1000; $i++)
{
$writer->writeSheetRow('Sheet1', array(
str_shuffle($chars),
rand()%10000,
date('Y-m-d H:i:s',time()-(rand()%31536000))
));
}
$writer->writeToFile('xlsx-autofilter.xlsx');
echo '#'.floor((memory_get_peak_usage())/1024/1024)."MB"."\n";
<?php
set_include_path( get_include_path().PATH_SEPARATOR."..");
include_once("xlsxwriter.class.php");
$chars = 'abcdefgh';
$writer = new XLSXWriter();
$writer->writeSheetHeader('Sheet1', array('c1'=>'string','c2'=>'integer','c3'=>'integer','c4'=>'integer','c5'=>'integer'), ['freeze_rows'=>1, 'freeze_columns'=>1] );
for($i=0; $i<250; $i++)
{
$writer->writeSheetRow('Sheet1', array(
str_shuffle($chars),
rand()%10000,
rand()%10000,
rand()%10000,
rand()%10000
));
}
$writer->writeToFile('xlsx-freeze-rows-columns.xlsx');
echo '#'.floor((memory_get_peak_usage())/1024/1024)."MB"."\n";
README
------
run_test_and_diff.sh
Just a quick tool for diffing spreadsheets, from a baseline openoffice/libreoffice spreadsheet.
Requires xmllint and meld as command line tools in linux. The idea is you can manipulate the
xlsx spreadsheet and then see what the resulting xml is, and diff it with your test.xlsx
```sudo apt-get install xmllint libxml2-utils```
xlsxwriter.class.Test.php
A simple PHPUnit test for a basic spreadsheet
#!/bin/bash
if [ ! -f "$1" ] || [ ! -f "$2" ]; then
echo "Example Usage: $0 f1.xlsx f2.xlsx";
exit
fi
./extract.sh $1
./extract.sh $2
echo "Now, run this command:"
echo " meld openoffice/ test/";
#export DISPLAY=:0 && meld openoffice/ test/;
#!/bin/bash
FILENAME=`basename --suffix=.xlsx $1`
DIRNAME=$FILENAME"_dir"
if [ "" == "$1" ]; then
echo "Example Usage: $0 spreadsheet.xlsx";
exit
fi
if [ ! -f "$1" ]; then
echo "Example Usage: $0 spreadsheet.xlsx";
exit
fi
mkdir -p $DIRNAME;
cp $1 $DIRNAME;
cd $DIRNAME;
unzip -o $1;
for FILE in *.xml;
do
xmllint --format "$FILE" > temp.xml;
mv temp.xml $FILE;
done;
for FILE in */*.xml
do
xmllint --format "$FILE" > temp.xml;
mv temp.xml $FILE;
done;
for FILE in */*/*.xml
do
xmllint --format "$FILE" > temp.xml;
mv temp.xml $FILE;
done;
xmllint --format "xl/_rels/workbook.xml.rels" > temp.xml;
mv temp.xml xl/_rels/workbook.xml.rels;
cd ..;
exit
<?php
include_once("../../xlsxwriter.class.php");
$writer = new XLSXWriter();
$keywords = array('some','interesting','keywords');
$writer->setTitle('Some Title');
$writer->setSubject('Some Subject');
$writer->setAuthor('Some Author');
$writer->setCompany('Some Company');
$writer->setKeywords($keywords);
$writer->setDescription('Some interesting description');
$header = array(
'General'=>'string',
'Simple Integer'=>'0',
'2 Decimal Places Integer'=>'0.00',
'Integer 1000s Group'=>'#,##0',
'1000s,Decimal,Leading Zero'=>'#,##0.00',
'1000s,Decimal,No Leading Zero'=>'#,###.00',
'Negative In Parentheses'=>'#,##0_);(#,##0)',
'Negative In Parentheses With Decimal'=>'#,##0.00_);(#,##0.00)',
);
$row = array('1000','2000','3000','4000','0.50','0.50','-50','-50');
$writer->writeSheet(array( $row ),'Number',$header);
$header = array(
'Whole Percent'=>'0%',
'Decimal Percent'=>'0.00%',
);
$row = array('1','1');
$writer->writeSheet(array( $row ),'Percent',$header);
$header = array(
'USD'=>'[$$-409]#,##0.00;[RED]-[$$-409]#,##0.00',
'CAD'=>'[$$-1009]#,##0.00;[RED]-[$$-1009]#,##0.00',
'Euro'=>'#,##0.00 [$€-407];[RED]-#,##0.00 [$€-407]',
'JPY'=>'[$¥-411]#,##0;[RED]-[$¥-411]#,##0',
'CNY'=>'[$¥-804]#,##0.00;[RED]-[$¥-804]#,##0.00',
);
$row = array('1000','2000','3000','4000','5000');
$writer->writeSheet(array( $row ) ,'Currency',$header);
$header = array(
'M/D/YY'=>'M/D/YY',
'MM/DD/YYYY'=>'MM/DD/YYYY',
'YYYY-MM-DD'=>'YYYY-MM-DD',
'YYYY-MM-DD HH:MM:SS'=>'YYYY-MM-DD HH:MM:SS',
'NN'=>'NN',
'NNN'=>'NNN',
'NNNN'=>'NNNN',
'D'=>'D',
'DD'=>'DD',
'M'=>'M',
'MM'=>'MM',
'MMM'=>'MMM',
'MMMM'=>'MMMM',
'YY'=>'YY',
'YYYY'=>'YYYY',
'Q YY'=>'Q YY',
'Q YYYY'=>'Q YYYY',
);
$row = array('1999-01-01','1999-01-01','1999-12-31','1999-12-31 00:00:00',
'1999-12-31','1999-12-31','1999-12-31',
'1999-12-31','1999-12-31','1999-12-31',
'1999-12-31','1999-12-31','1999-12-31',
'1999-12-31','1999-12-31','1999-12-31',
'1999-12-31');
$writer->writeSheet(array( $row ) ,'Date',$header);
$header = array(
'HH:MM'=>'HH:MM',
'HH:MM:SS'=>'HH:MM:SS',
'HH:MM AM/PM'=>'HH:MM AM/PM',
'HH:MM:SS AM/PM'=>'HH:MM:SS AM/PM',
);
$row = array('12-31-1999 01:23:00','12-31-1999 01:23:00','12-31-1999 01:23:00','12-31-1999 01:23:00');
$writer->writeSheet(array( $row ) ,'Time',$header);
$writer->writeToFile('formats.xlsx');
<?php
include_once("../../xlsxwriter.class.php");
$writer = new XLSXWriter();
$keywords = array('some','interesting','keywords');
$writer->setTitle('Some Title');
$writer->setSubject('Some Subject');
$writer->setAuthor('Some Author');
$writer->setCompany('Some Company');
$writer->setKeywords($keywords);
$writer->setDescription('Some interesting description');
$header = array(
'General'=>'string',
'Simple Integer'=>'0',
'2 Decimal Places Integer'=>'0.00',
'Integer 1000s Group'=>'#,##0',
'1000s,Decimal,Leading Zero'=>'#,##0.00',
'1000s,Decimal,No Leading Zero'=>'#,###.00',
'Negative In Parentheses'=>'#,##0_);(#,##0)',
'Negative In Parentheses With Decimal'=>'#,##0.00_);(#,##0.00)',
);
$row = array('1000','2000','3000','4000','0.50','0.50','-50','-50');
$writer->writeSheet(array( $row ),'Number',$header);
$header = array(
'Whole Percent'=>'0%',
'Decimal Percent'=>'0.00%',
);
$row = array('1','1');
$writer->writeSheet(array( $row ),'Percent',$header);
$header = array(
'USD'=>'[$$-409]#,##0.00;[RED]-[$$-409]#,##0.00',
'CAD'=>'[$$-1009]#,##0.00;[RED]-[$$-1009]#,##0.00',
'Euro'=>'#,##0.00 [$€-407];[RED]-#,##0.00 [$€-407]',
'JPY'=>'[$¥-411]#,##0;[RED]-[$¥-411]#,##0',
'CNY'=>'[$¥-804]#,##0.00;[RED]-[$¥-804]#,##0.00',
);
$row = array('1000','2000','3000','4000','5000');
$writer->writeSheet(array( $row ) ,'Currency',$header);
$header = array(
'M/D/YY'=>'M/D/YY',
'MM/DD/YYYY'=>'MM/DD/YYYY',
'YYYY-MM-DD'=>'YYYY-MM-DD',
'YYYY-MM-DD HH:MM:SS'=>'YYYY-MM-DD HH:MM:SS',
'NN'=>'NN',
'NNN'=>'NNN',
'NNNN'=>'NNNN',
'D'=>'D',
'DD'=>'DD',
'M'=>'M',
'MM'=>'MM',
'MMM'=>'MMM',
'MMMM'=>'MMMM',
'YY'=>'YY',
'YYYY'=>'YYYY',
'Q YY'=>'Q YY',
'Q YYYY'=>'Q YYYY',
);
$row = array('1999-01-01','1999-01-01','1999-12-31','1999-12-31 00:00:00',
'1999-12-31','1999-12-31','1999-12-31',
'1999-12-31','1999-12-31','1999-12-31',
'1999-12-31','1999-12-31','1999-12-31',
'1999-12-31','1999-12-31','1999-12-31',
'1999-12-31');
$writer->writeSheet(array( $row ) ,'Date',$header);
$header = array(
'HH:MM'=>'HH:MM',
'HH:MM:SS'=>'HH:MM:SS',
'HH:MM AM/PM'=>'HH:MM AM/PM',
'HH:MM:SS AM/PM'=>'HH:MM:SS AM/PM',
);
$row = array('12-31-1999 01:23:00','12-31-1999 01:23:00','12-31-1999 01:23:00','12-31-1999 01:23:00');
$writer->writeSheet(array( $row ) ,'Time',$header);
$writer->writeToFile('formats_.xlsx');
<?php
include_once("../xlsxwriter.class.php");
$header = array(
'year'=>'string',
'month'=>'string',
'amount'=>'money',
'first_event'=>'datetime',
'second_event'=>'date',
);
$data1 = array(
array('2003','1','-50.5','2010-01-01 23:00:00','2012-12-31 23:00:00'),
array('2003','=B2', '23.5','2010-01-01 00:00:00','2012-12-31 00:00:00'),
);
$data2 = array(
array('2003','01','343.12'),
array('2003','02','345.12'),
);
$writer = new XLSXWriter();
$keywords = array('some','interesting','keywords');
$writer->setTitle('Some Title');
$writer->setSubject('Some Subject');
$writer->setAuthor('Some Author');
$writer->setCompany('Some Company');
$writer->setKeywords($keywords);
$writer->setDescription('Some interesting description');
$writer->writeSheet($data1,'Sheet1',$header);
$writer->writeSheet($data2,'Sheet2');
$writer->writeToFile('test.xlsx');
<?php
include_once __DIR__.'/../vendor/autoload.php';
//TODO test double:writeSheetHeader
//TODO test invalid UTF8
//TODO test outoforder writeSheetRow('Sheet1',());
class _XLSXWriter_ extends XLSXWriter
{
public function writeCell(XLSXWriter_BuffererWriter &$file, $row_number, $column_number, $value, $cell_format) {
return call_user_func_array('parent::writeCell', func_get_args());
}
}
//Just a simple test, by no means comprehensive
class XLSXWriterTest extends PHPUnit_Framework_TestCase
{
/**
* @covers XLSXWriter::writeCell
*/
public function testWriteCell()
{
$filename = tempnam("/tmp", "xlsx_writer");
$file_writer = new XLSXWriter_BuffererWriter($filename);
$xlsx_writer = new _XLSXWriter_();
$xlsx_writer->writeCell($file_writer, 0, 0, '0123', 'string');
$file_writer->close();
$cell_xml = file_get_contents($filename);
$this->assertNotEquals('<c r="A1" s="0" t="n"><v>123</v></c>', $cell_xml);
$this->assertEquals('<c r="A1" s="0" t="s"><v>0</v></c>', $cell_xml);//0123 should be the 0th index of the shared string array
@unlink($filename);
}
/**
* @covers XLSXWriter::writeToFile
*/
public function testWriteToFile()
{
$filename = tempnam("/tmp", "xlsx_writer");
$header = array('0'=>'string','1'=>'string','2'=>'string','3'=>'string');
$sheet = array(
array('55','66','77','88'),
array('10','11','12','13'),
);
$xlsx_writer = new XLSXWriter();
$xlsx_writer->writeSheet($sheet,'mysheet',$header);
$xlsx_writer->writeToFile($filename);
$zip = new ZipArchive();
$r = $zip->open($filename);
$this->assertTrue($r);
$r = $zip->numFiles>0 ? true : false;
$this->assertTrue($r);
$out_sheet = array();
for($z=0; $z<$zip->numFiles; $z++)
{
$inside_zip_filename = $zip->getNameIndex($z);
if (preg_match("/sheet(\d+).xml/", basename($inside_zip_filename)))
{
$out_sheet = $this->stripCellsFromSheetXML($zip->getFromName($inside_zip_filename));
array_shift($out_sheet);
$out_sheet = array_values($out_sheet);
}
}
$zip->close();
@unlink($filename);
$r1 = self::array_diff_assoc_recursive($out_sheet, $sheet);
$r2 = self::array_diff_assoc_recursive($sheet, $out_sheet);
$this->assertEmpty($r1);
$this->assertEmpty($r2);
}
private function stripCellsFromSheetXML($sheet_xml)
{
$output=array();
$xml = new SimpleXMLElement($sheet_xml);
$i=0;
foreach($xml->sheetData->row as $row)
{
$j=0;
foreach($row->c as $c)
{
$output[$i][$j] = (string)$c->v;
$j++;
}
$i++;
}
return $output;
}
public static function array_diff_assoc_recursive($array1, $array2)
{
foreach($array1 as $key => $value)
{
if(is_array($value))
{
if(!isset($array2[$key]) || !is_array($array2[$key]))
{
$difference[$key] = $value;
}
else
{
$new_diff = self::array_diff_assoc_recursive($value, $array2[$key]);
if(!empty($new_diff))
{
$difference[$key] = $new_diff;
}
}
}
else if(!isset($array2[$key]) || $array2[$key] != $value)
{
$difference[$key] = $value;
}
}
return !isset($difference) ? array() : $difference;
}
}
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment