1
0
mirror of https://github.com/shuchkin/simplexlsxgen.git synced 2023-08-10 21:12:59 +03:00

17 Commits

Author SHA1 Message Date
ff3f016358 1.2.11 2022-04-30 14:08:40 +06:00
bcd3586731 1.2.10 2022-04-30 14:04:13 +06:00
402c6b9417 1.2.10 2022-04-24 14:59:15 +06:00
5840fd5285 Merge pull request #58 from mrjemson/master
Added Font Color and Background Color Capabilities
2022-04-23 13:07:03 +06:00
4543922330 Updated examples 2022-04-23 11:34:13 +10:00
d49f8996df Switched to style tags 2022-04-23 11:33:17 +10:00
5309e6a002 Removed duplicate line 2022-04-22 23:21:40 +10:00
ab28bc319d Added missing line break 2022-04-22 23:19:38 +10:00
e145bd5777 Updated examples to include background fill 2022-04-22 23:16:28 +10:00
2ee7237016 Updated class to include background fill options 2022-04-22 23:15:35 +10:00
a0a932e2f5 Updated Styles Image for Background Fill 2022-04-22 23:14:42 +10:00
fc884044ac Color Code Clean Up 2022-04-22 18:03:17 +10:00
d84c842d38 Corrected const C_NORMAL value 2022-04-22 17:59:30 +10:00
0dafc8bf9e Fixes for color updates 2022-04-22 17:58:25 +10:00
82f19a9f35 Updated styles.png 2022-04-22 17:54:51 +10:00
139602c340 Update README.md 2022-04-22 17:51:35 +10:00
ac34d8f527 Added Font Color 2022-04-22 16:43:26 +10:00
4 changed files with 127 additions and 51 deletions

View File

@ -1,7 +1,13 @@
# Changelog
# 1.1.12 (2022-03-15)
* Added $xlsx->mergeCells('A1:C1')
## 1.2.11 (2022-)
* Row height `<style height="50">Custom row height 50</style>`
## 1.2.10 (2022-04-24)
* Added colors `<style color="#FFFF00" bgcolor="#00FF00">Yellow text on blue background</style>`, thx [mrjemson](https://github.com/mrjemson)
## 1.1.12 (2022-03-15)
* Added `$xlsx->mergeCells('A1:C1')`
## 1.1.11 (2022-02-05)
* sheet name maximum length is 31 chars, mb_substr used now

View File

@ -63,16 +63,19 @@ $data = [
['Bold + Italic', '<b><i>12345.67</i></b>'],
['Hyperlink', 'https://github.com/shuchkin/simplexlsxgen'],
['Italic + Hyperlink + Anchor', '<i><a href="https://github.com/shuchkin/simplexlsxgen">SimpleXLSXGen</a></i>'],
['Green', '<style color="#00FF00">12345.67</style>'],
['Bold Red Text', '<b><style color="#FF0000">12345.67</style></b>'],
['Blue Text and Yellow Fill', '<style bgcolor="#FFFF00" color="#0000FF">12345.67</style>'],
['Left', '<left>12345.67</left>'],
['Center', '<center>12345.67</center>'],
['Right', '<right>Right Text</right>'],
['Center + Bold', '<center><b>Name</b></center>'],
['<center>MERGE CELLS</center>']
['<center>MERGE CELLS</center>', null]
];
Shuchkin\SimpleXLSXGen::fromArray( $data )
SimpleXLSXGen::fromArray( $data )
->setDefaultFont( 'Courier New' )
->setDefaultFontSize( 14 )
->mergeCells('A13:B13')
->mergeCells('A16:B16')
->saveAs('styles_and_tags.xlsx');
```
![XLSX screenshot](styles.png)

View File

@ -1,4 +1,9 @@
<?php
<?php /** @noinspection ReturnTypeCanBeDeclaredInspection */
/** @noinspection PhpMissingReturnTypeInspection */
/** @noinspection NullCoalescingOperatorCanBeUsedInspection */
/** @noinspection PhpIssetCanBeReplacedWithCoalesceInspection */
namespace Shuchkin;
/**
@ -14,7 +19,6 @@ class SimpleXLSXGen {
protected $defaultFontSize;
protected $sheets;
protected $template;
protected $F, $F_KEYS; // fonts
protected $XF, $XF_KEYS; // cellXfs
protected $SI, $SI_KEYS; // shared strings
const N_NORMAL = 0; // General
@ -31,6 +35,14 @@ class SimpleXLSXGen {
const F_ITALIC = 4;
const F_UNDERLINE = 8;
const F_STRIKE = 16;
const F_COLOR = 32;
const FL_NONE = 0; // none
const FL_SOLID = 1; // solid
const FL_MEDIUM_GRAY = 2; // mediumGray
const FL_DARK_GRAY = 4; // darkGray
const FL_LIGHT_GRAY = 8; // lightGray
const FL_GRAY_125 = 16; // gray125
const FL_COLOR = 32;
const A_DEFAULT = 0;
const A_LEFT = 1;
const A_RIGHT = 2;
@ -42,10 +54,12 @@ class SimpleXLSXGen {
$this->sheets = [ ['name' => 'Sheet1', 'rows' => [], 'hyperlinks' => [], 'mergecells' => [] ] ];
$this->SI = []; // sharedStrings index
$this->SI_KEYS = []; // & keys
$this->F = [ self::F_NORMAL ]; // fonts
$this->F_KEYS = [0]; // & keys
$this->XF = [ [self::N_NORMAL, self::F_NORMAL, self::A_DEFAULT] ]; // styles
$this->XF_KEYS = ['N0F0A0' => 0 ]; // & keys
$this->XF = [ // styles
[self::N_NORMAL, self::A_DEFAULT, self::F_NORMAL, self::FL_NONE, 0, 0],
[self::N_NORMAL, self::A_DEFAULT, self::F_NORMAL, self::FL_GRAY_125, 0, 0], // hack
];
$this->XF_KEYS['0-0-0-0-0-0'] = 0; // & keys
$this->XF_KEYS['0-0-0-16-0-0'] = 1;
$this->template = [
'_rels/.rels' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
@ -80,7 +94,7 @@ class SimpleXLSXGen {
'xl/styles.xml' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
{FONTS}
<fills count="1"><fill><patternFill patternType="none"/></fill></fills>
{FILLS}
<borders count="1"><border><left/><right/><top/><bottom/><diagonal/></border></borders>
<cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0" /></cellStyleXfs>
{XF}
@ -294,30 +308,64 @@ class SimpleXLSXGen {
$this->_writeEntry($fh, $cdrec, $cfilename, $template);
$entries++;
} elseif ( $cfilename === 'xl/styles.xml' ) {
$FONTS = ['<fonts count="'.count($this->F).'">'];
foreach ( $this->F as $f ) {
$XF = $FONTS = $F_KEYS = $FILLS = $FL_KEYS = [];
// print_r( $this->XF );
foreach( $this->XF as $xf ) {
// 0 - num fmt, 1 - align, 2 - font, 3 - fill, 4 - font color, 5 - bgcolor
// fonts
$F_KEY = $xf[2].'-'.$xf[4];
if ( isset($F_KEYS[ $F_KEY ]) ) {
$F_ID = $F_KEYS[ $F_KEY ];
} else {
$F_ID = $F_KEYS[ $F_KEY ] = count( $FONTS );
$FONTS[] = '<font><name val="'.$this->defaultFont.'"/><family val="2"/>'
. ( $this->defaultFontSize ? '<sz val="'.$this->defaultFontSize.'"/>' : '' )
.( $f & self::F_BOLD ? '<b/>' : '')
.( $f & self::F_ITALIC ? '<i/>' : '')
.( $f & self::F_UNDERLINE ? '<u/>' : '')
.( $f & self::F_STRIKE ? '<strike/>' : '')
.( $f & self::F_HYPERLINK ? '<color rgb="FF0563C1"/><u/>' : '')
.( $xf[2] & self::F_BOLD ? '<b/>' : '')
.( $xf[2] & self::F_ITALIC ? '<i/>' : '')
.( $xf[2] & self::F_UNDERLINE ? '<u/>' : '')
.( $xf[2] & self::F_STRIKE ? '<strike/>' : '')
.( $xf[2] & self::F_HYPERLINK ? '<u/>' : '')
.( $xf[2] & self::F_COLOR ? '<color rgb="'.$xf[4].'"/>' : '')
.'</font>';
}
$FONTS[] = '</fonts>';
$XF = ['<cellXfs count="'.count($this->XF).'">'];
foreach( $this->XF as $xf ) {
$align = ($xf[2] === self::A_LEFT ? ' applyAlignment="1"><alignment horizontal="left"/>' : '')
.($xf[2] === self::A_RIGHT ? ' applyAlignment="1"><alignment horizontal="right"/>' : '')
.($xf[2] === self::A_CENTER ? ' applyAlignment="1"><alignment horizontal="center"/>' : '');
$XF[] = '<xf numFmtId="'.$xf[0].'" fontId="'.$xf[1].'" fillId="0" borderId="0" xfId="0"'
// fills
$FL_KEY = $xf[3].'-'.$xf[5];
if (isset($FL_KEYS[$FL_KEY])) {
$FL_ID = $FL_KEYS[ $FL_KEY ];
} else {
$FL_ID = $FL_KEYS[ $FL_KEY ] = count($FILLS);
$FILLS[] = '<fill><patternFill patternType="'
.( $xf[3] === 0 ? 'none' : '')
.( $xf[3] & self::FL_SOLID ? 'solid' : '')
.( $xf[3] & self::FL_MEDIUM_GRAY ? 'mediumGray' : '')
.( $xf[3] & self::FL_DARK_GRAY ? 'darkGray' : '')
.( $xf[3] & self::FL_LIGHT_GRAY ? 'lightGray' : '')
.( $xf[3] & self::FL_GRAY_125 ? 'gray125' : '')
.'"'
.( $xf[3] & self::FL_COLOR ? '><fgColor rgb="'.$xf[5].'"/><bgColor indexed="64"/></patternFill>' : ' />')
.'</fill>';
}
$align = ($xf[1] === self::A_LEFT ? ' applyAlignment="1"><alignment horizontal="left"/>' : '')
.($xf[1] === self::A_RIGHT ? ' applyAlignment="1"><alignment horizontal="right"/>' : '')
.($xf[1] === self::A_CENTER ? ' applyAlignment="1"><alignment horizontal="center"/>' : '');
$XF[] = '<xf numFmtId="'.$xf[0].'" fontId="'.$F_ID.'" fillId="'.$FL_ID.'" borderId="0" xfId="0"'
.($xf[0] > 0 ? ' applyNumberFormat="1"' : '')
.($F_ID > 0 ? ' applyFont="1"' : '')
.($FL_ID > 0 ? ' applyFill="1"' : '')
.($align ? $align . '</xf>' : '/>');
}
// wrap collections
array_unshift( $XF, '<cellXfs count="'.count($XF).'">');
$XF[] = '</cellXfs>';
$template = str_replace(['{FONTS}','{XF}'], [implode("\r\n", $FONTS), implode("\r\n", $XF)], $template);
array_unshift($FONTS, '<fonts count="'.count($FONTS).'">');
$FONTS[] = '</fonts>';
array_unshift($FILLS, '<fills count="'.count($FILLS).'">');
$FILLS[] = '</fills>';
$template = str_replace(['{FONTS}','{XF}','{FILLS}'], [implode("\r\n", $FONTS), implode("\r\n", $XF), implode("\r\n", $FILLS)], $template);
$this->_writeEntry($fh, $cdrec, $cfilename, $template);
$entries++;
} else {
@ -433,8 +481,9 @@ class SimpleXLSXGen {
$COL = [];
foreach( $this->sheets[$idx]['rows'] as $r ) {
$CUR_ROW++;
$row = '<row r="'.$CUR_ROW.'">';
$row = '';
$CUR_COL = 0;
$RH = 0; // row height
foreach( $r as $v ) {
$CUR_COL++;
if ( !isset($COL[ $CUR_COL ])) {
@ -448,7 +497,7 @@ class SimpleXLSXGen {
}
$ct = $cv = null;
$N = $F = $A = 0;
$N = $A = $F = $FL = $C = $B = 0;
if ( is_string($v) ) {
@ -469,6 +518,21 @@ class SimpleXLSXGen {
if ( strpos( $v, '<s>' ) !== false ) {
$F += self::F_STRIKE;
}
if ( preg_match('/<style([^>]+)>/', $v, $m ) ) {
if ( preg_match('/ color="([^"]+)"/', $m[1], $m2) ) {
$F += self::F_COLOR;
$C = strlen($m2[1]) === 8 ? $m2[1] : ('FF' . ltrim($m2[1],'#'));
}
if ( preg_match('/ bgcolor="([^"]+)"/', $m[1], $m2) ) {
$FL += self::FL_COLOR;
$B = strlen($m2[1]) === 8 ? $m2[1] : ('FF' . ltrim($m2[1],'#'));
}
if ( preg_match('/ height="([^"]+)"/', $m[1], $m2) ) {
$RH = $m2[1];
}
}
if ( strpos( $v, '<left>' ) !== false ) {
$A += self::A_LEFT;
}
@ -481,11 +545,11 @@ class SimpleXLSXGen {
if ( preg_match( '/<a href="(https?:\/\/[^"]+)">(.*?)<\/a>/i', $v, $m ) ) {
$h = explode( '#', $m[1] );
$this->sheets[ $idx ]['hyperlinks'][] = ['ID' => 'rId' . ( count( $this->sheets[ $idx ]['hyperlinks'] ) + 1 ), 'R' => $cname, 'H' => $h[0], 'L' => isset( $h[1] ) ? $h[1] : ''];
$F = self::F_HYPERLINK; // Hyperlink
$F += self::F_HYPERLINK; // Hyperlink
}
if ( preg_match( '/<a href="(mailto?:[^"]+)">(.*?)<\/a>/i', $v, $m ) ) {
$this->sheets[ $idx ]['hyperlinks'][] = ['ID' => 'rId' . ( count( $this->sheets[ $idx ]['hyperlinks'] ) + 1 ), 'R' => $cname, 'H' => $m[1], 'L' => ''];
$F = self::F_HYPERLINK; // mailto hyperlink
$F += self::F_HYPERLINK; // mailto hyperlink
}
$v = strip_tags( $v );
} // tags
@ -522,14 +586,14 @@ class SimpleXLSXGen {
$cv = $this->date2excel( $m[3], $m[2], $m[1], $m[4], $m[5], $m[6] );
$N = self::N_DATETIME; // [22] m/d/yy h:mm
} elseif ( preg_match( '/^[0-9+-.]+$/', $v ) ) { // Long ?
$A = self::A_RIGHT;
$A += self::A_RIGHT;
} elseif ( preg_match( '/^https?:\/\/\S+$/i', $v ) ) {
$h = explode( '#', $v );
$this->sheets[ $idx ]['hyperlinks'][] = ['ID' => 'rId' . ( count( $this->sheets[ $idx ]['hyperlinks'] ) + 1 ), 'R' => $cname, 'H' => $h[0], 'L' => isset( $h[1] ) ? $h[1] : ''];
$F = self::F_HYPERLINK; // Hyperlink
$F += self::F_HYPERLINK; // Hyperlink
} elseif ( preg_match( "/^[a-zA-Z0-9_\.\-]+@([a-zA-Z0-9][a-zA-Z0-9\-]*\.)+[a-zA-Z]{2,}$/", $v ) ) {
$this->sheets[ $idx ]['hyperlinks'][] = ['ID' => 'rId' . ( count( $this->sheets[ $idx ]['hyperlinks'] ) + 1 ), 'R' => $cname, 'H' => 'mailto:' . $v, 'L' => ''];
$F = self::F_HYPERLINK; // Hyperlink
$F += self::F_HYPERLINK; // Hyperlink
}
if ( ($N === self::N_DATE || $N === self::N_DATETIME) && $cv < 0 ) {
$cv = null;
@ -574,30 +638,33 @@ class SimpleXLSXGen {
$COL[ $CUR_COL ] = max( $vl, $COL[ $CUR_COL ] );
$cs = 0;
if ( $N + $F + $A > 0 ) {
if ( isset($this->F_KEYS[ $F ] ) ) {
$cf = $this->F_KEYS[ $F ];
} else {
$cf = count($this->F);
$this->F_KEYS[$F] = $cf;
$this->F[] = $F;
if ( $N + $A + $F + $FL > 0 ) {
if ( $FL === self::FL_COLOR ) {
$FL += self::FL_SOLID;
}
$NFA = 'N' . $N . 'F' . $cf . 'A' . $A;
if ( isset( $this->XF_KEYS[ $NFA ] ) ) {
$cs = $this->XF_KEYS[ $NFA ];
if ( ($F & self::F_HYPERLINK) && !($F & self::F_COLOR)) {
$F += self::F_COLOR;
$C = 'FF0563C1';
}
$XF_KEY = $N . '-' . $A . '-' . $F. '-'. $FL . '-' . $C . '-' . $B;
// echo $cname .'='.$XF_KEY.PHP_EOL;
if ( isset( $this->XF_KEYS[ $XF_KEY ] ) ) {
$cs = $this->XF_KEYS[ $XF_KEY ];
}
if ( $cs === 0 ) {
$cs = count( $this->XF );
$this->XF_KEYS[ $NFA ] = $cs;
$this->XF[] = [$N, $cf, $A];
$this->XF_KEYS[ $XF_KEY ] = $cs;
$this->XF[] = [$N, $A, $F, $FL, $C, $B];
}
}
$row .= '<c r="' . $cname . '"' . ($ct ? ' t="' . $ct . '"' : '') . ($cs ? ' s="' . $cs . '"' : '') . '>'
. ($ct === 'inlineStr' ? '<is><t>' . $cv . '</t></is>' : '<v>' . $cv . '</v>') . "</c>\r\n";
}
$ROWS[] = $row . "</row>\r\n";
$ROWS[] = '<row r="'.$CUR_ROW.'"'.($RH ? ' customHeight="1" ht="'.$RH.'"' : '').'>'.$row . "</row>";
}
foreach ( $COL as $k => $max ) {
$COLS[] = '<col min="'.$k.'" max="'.$k.'" width="'.min( $max+1, 60).'" />';

Binary file not shown.

Before

Width:  |  Height:  |  Size: 57 KiB

After

Width:  |  Height:  |  Size: 30 KiB