Skip to content

Commit

Permalink
Merge pull request #84 from mk-j/2017-styles
Browse files Browse the repository at this point in the history
add 2017 styles, remove shared string index
  • Loading branch information
mk-j authored Jan 30, 2017
2 parents 8f47921 + 4081038 commit 53842b9
Show file tree
Hide file tree
Showing 21 changed files with 648 additions and 497 deletions.
77 changes: 48 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
PHP_XLSXWriter
==============

This library is designed to be lightweight, and have relatively low memory usage.
This library is designed to be lightweight, and have minimal memory usage.

It is designed to output an Excel spreadsheet in with (Office 2007+) xlsx format, with just basic features supported:
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

Give this library a try, if you find yourself [running out of memory writing spreadsheets with PHPExcel](http://www.zedwood.com/article/php-excel-writer-performance-comparison).
[Never run out of memory with PHPExcel again](https://github.com/mk-j/PHP_XLSXWriter).

Simple PHP CLI example:
```php
Expand All @@ -24,32 +26,13 @@ $writer->writeSheet($data);
$writer->writeToFile('output.xlsx');
```

Multiple Sheets:
```php
$data1 = array(
array('5','3'),
array('1','6'),
);
$data2 = array(
array('2','7','9'),
array('4','8','0'),
);

$writer = new XLSXWriter();
$writer->setAuthor('Doc Author');
$writer->writeSheet($data1,'Sheet1');
$writer->writeSheet($data2,'Sheet2');
echo $writer->writeToString();
```

Simple/Advanced Cell Formats:
```php
//simple formats: date, datetime, integer, dollar, euro, string
$header = array(
'created'=>'date',
'product_id'=>'integer',
'quantity'=>'#,##0',
'amount'=>'dollar',
'amount'=>'price',
'description'=>'string',
'tax'=>'[$$-1009]#,##0.00;[RED]-[$$-1009]#,##0.00',
);
Expand All @@ -59,20 +42,56 @@ $data = array(
);

$writer = new XLSXWriter();
$writer->writeSheet($data,'Sheet1', $header);
$writer->writeSheetHeader('Sheet1', $header );
foreach($data as $row)
$writer->writeSheetRow('Sheet1', $row );
$writer->writeToFile('example.xlsx');
```

Load test with 50000 rows: (runs fast, with low memory usage)
50000 rows: (1.4s, 0MB memory usage)
```php
include_once("xlsxwriter.class.php");
$header = array('c1'=>'string','c2'=>'string','c3'=>'string','c4'=>'string');
$writer = new XLSXWriter();
$writer->writeSheetHeader('Sheet1', $header );//optional
$writer->writeSheetHeader('Sheet1', array('c1'=>'integer','c2'=>'integer','c3'=>'integer','c4'=>'integer') );
for($i=0; $i<50000; $i++)
{
$writer->writeSheetRow('Sheet1', array(rand()%10000,rand()%10000,rand()%10000,rand()%10000) );
$writer->writeSheetRow('Sheet1', array($i, $i+1, $i+2, $i+3) );
}
$writer->writeToFile('output.xlsx');
$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' |
| color | #RRGGBB, ie: #ff99cc or #f9c |
| fill | #RRGGBB, ie: #eeffee or #efe |
| halign | general, left, right, justify, center |
| valign | bottom, center, distributed |


123 changes: 0 additions & 123 deletions benchmark/test_phpexcel.php

This file was deleted.

40 changes: 0 additions & 40 deletions benchmark/test_xlsxwriter.php

This file was deleted.

17 changes: 0 additions & 17 deletions example-cli-formats.php

This file was deleted.

11 changes: 0 additions & 11 deletions example-cli-load.php

This file was deleted.

18 changes: 0 additions & 18 deletions example-cli-merge-cells.php

This file was deleted.

33 changes: 18 additions & 15 deletions example-cli.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
<?php
include_once("xlsxwriter.class.php");
ini_set('display_errors', 0);
ini_set('log_errors', 1);
error_reporting(E_ALL & ~E_NOTICE);

$header = array(
'year'=>'string',
'month'=>'string',
'amount'=>'money',
'first_event'=>'datetime',
'second_event'=>'date',
);
$data1 = array(
$filename = "example.xlsx";
$as_command_line = php_sapi_name()!='cli' ? true : false;
if ()
{
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','=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->setAuthor('Some Author');
$writer->writeSheet($data1,'Sheet1',$header);
$writer->writeSheet($data2,'Sheet2');
foreach($rows as $row)
$writer->writeSheetRow('Sheet1', $row);

$writer->writeToFile('example.xlsx');
//$writer->writeToStdOut();
//echo $writer->writeToString();
exit(0);


Loading

0 comments on commit 53842b9

Please sign in to comment.