1
0
mirror of https://github.com/shuchkin/simplexlsxgen.git synced 2023-08-10 21:12:59 +03:00
This commit is contained in:
Sergey Shuchkin 2022-04-24 14:59:15 +06:00
parent 5840fd5285
commit 402c6b9417
3 changed files with 109 additions and 76 deletions

View File

@ -1,7 +1,10 @@
# Changelog # Changelog
# 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) # 1.1.12 (2022-03-15)
* Added $xlsx->mergeCells('A1:C1') * Added `$xlsx->mergeCells('A1:C1')`
## 1.1.11 (2022-02-05) ## 1.1.11 (2022-02-05)
* sheet name maximum length is 31 chars, mb_substr used now * sheet name maximum length is 31 chars, mb_substr used now

View File

@ -60,22 +60,22 @@ $data = [
['Italic', '<i>12345.67</i>'], ['Italic', '<i>12345.67</i>'],
['Underline', '<u>12345.67</u>'], ['Underline', '<u>12345.67</u>'],
['Strike', '<s>12345.67</s>'], ['Strike', '<s>12345.67</s>'],
['Green', '<style color="0000FF00">12345.67</style>'],
['Bold Red Text', '<b><style color="00FF0000">12345.67</style></b>'],
['Blue Text and Yellow Fill', '<style bgcolor="00FFFF00" color="000000FF">12345.67</style>'],
['Bold + Italic', '<b><i>12345.67</i></b>'], ['Bold + Italic', '<b><i>12345.67</i></b>'],
['Hyperlink', 'https://github.com/shuchkin/simplexlsxgen'], ['Hyperlink', 'https://github.com/shuchkin/simplexlsxgen'],
['Italic + Hyperlink + Anchor', '<i><a href="https://github.com/shuchkin/simplexlsxgen">SimpleXLSXGen</a></i>'], ['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>'], ['Left', '<left>12345.67</left>'],
['Center', '<center>12345.67</center>'], ['Center', '<center>12345.67</center>'],
['Right', '<right>Right Text</right>'], ['Right', '<right>Right Text</right>'],
['Center + Bold', '<center><b>Name</b></center>'], ['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' ) ->setDefaultFont( 'Courier New' )
->setDefaultFontSize( 14 ) ->setDefaultFontSize( 14 )
->mergeCells('A13:B13') ->mergeCells('A16:B16')
->saveAs('styles_and_tags.xlsx'); ->saveAs('styles_and_tags.xlsx');
``` ```
![XLSX screenshot](styles.png) ![XLSX screenshot](styles.png)

View File

@ -1,4 +1,9 @@
<?php <?php /** @noinspection ReturnTypeCanBeDeclaredInspection */
/** @noinspection PhpMissingReturnTypeInspection */
/** @noinspection NullCoalescingOperatorCanBeUsedInspection */
/** @noinspection PhpIssetCanBeReplacedWithCoalesceInspection */
namespace Shuchkin; namespace Shuchkin;
/** /**
@ -14,9 +19,7 @@ class SimpleXLSXGen {
protected $defaultFontSize; protected $defaultFontSize;
protected $sheets; protected $sheets;
protected $template; protected $template;
protected $F, $F_KEYS; // fonts
protected $XF, $XF_KEYS; // cellXfs protected $XF, $XF_KEYS; // cellXfs
protected $B, $B_KEYS; // background fills
protected $SI, $SI_KEYS; // shared strings protected $SI, $SI_KEYS; // shared strings
const N_NORMAL = 0; // General const N_NORMAL = 0; // General
const N_INT = 1; // 0 const N_INT = 1; // 0
@ -32,8 +35,14 @@ class SimpleXLSXGen {
const F_ITALIC = 4; const F_ITALIC = 4;
const F_UNDERLINE = 8; const F_UNDERLINE = 8;
const F_STRIKE = 16; const F_STRIKE = 16;
const C_NORMAL = 0; const F_COLOR = 32;
const B_NORMAL = 0; 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_DEFAULT = 0;
const A_LEFT = 1; const A_LEFT = 1;
const A_RIGHT = 2; const A_RIGHT = 2;
@ -45,12 +54,12 @@ class SimpleXLSXGen {
$this->sheets = [ ['name' => 'Sheet1', 'rows' => [], 'hyperlinks' => [], 'mergecells' => [] ] ]; $this->sheets = [ ['name' => 'Sheet1', 'rows' => [], 'hyperlinks' => [], 'mergecells' => [] ] ];
$this->SI = []; // sharedStrings index $this->SI = []; // sharedStrings index
$this->SI_KEYS = []; // & keys $this->SI_KEYS = []; // & keys
$this->F = [ self::F_NORMAL ]; // fonts $this->XF = [ // styles
$this->F_KEYS = [0]; // & keys [self::N_NORMAL, self::A_DEFAULT, self::F_NORMAL, self::FL_NONE, 0, 0],
$this->C = [ self::C_NORMAL ]; // [self::N_NORMAL, self::A_DEFAULT, self::F_NORMAL, self::FL_GRAY_125, 0, 0], // hack
$this->B = [ self::B_NORMAL ]; // ];
$this->XF = [ [self::N_NORMAL, self::F_NORMAL, self::A_DEFAULT, self::C_NORMAL, self::B_NORMAL] ]; // styles $this->XF_KEYS['0-0-0-0-0-0'] = 0; // & keys
$this->XF_KEYS = ['N0F0A0C0B0' => 0 ]; // & keys $this->XF_KEYS['0-0-0-16-0-0'] = 1;
$this->template = [ $this->template = [
'_rels/.rels' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?> '_rels/.rels' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
@ -299,39 +308,63 @@ class SimpleXLSXGen {
$this->_writeEntry($fh, $cdrec, $cfilename, $template); $this->_writeEntry($fh, $cdrec, $cfilename, $template);
$entries++; $entries++;
} elseif ( $cfilename === 'xl/styles.xml' ) { } elseif ( $cfilename === 'xl/styles.xml' ) {
$FONTS = ['<fonts count="'.count($this->F).'">']; $XF = $FONTS = $F_KEYS = $FILLS = $FL_KEYS = [];
foreach ( $this->F as $index => $f ) { // 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"/>' $FONTS[] = '<font><name val="'.$this->defaultFont.'"/><family val="2"/>'
. ( $this->defaultFontSize ? '<sz val="'.$this->defaultFontSize.'"/>' : '' ) . ( $this->defaultFontSize ? '<sz val="'.$this->defaultFontSize.'"/>' : '' )
.( $f & self::F_BOLD ? '<b/>' : '') .( $xf[2] & self::F_BOLD ? '<b/>' : '')
.( $f & self::F_ITALIC ? '<i/>' : '') .( $xf[2] & self::F_ITALIC ? '<i/>' : '')
.( $f & self::F_UNDERLINE ? '<u/>' : '') .( $xf[2] & self::F_UNDERLINE ? '<u/>' : '')
.( $f & self::F_STRIKE ? '<strike/>' : '') .( $xf[2] & self::F_STRIKE ? '<strike/>' : '')
.( $f & self::F_HYPERLINK ? '<u/>' : '') .( $xf[2] & self::F_HYPERLINK ? '<u/>' : '')
.( isset($this->C[$index]) ? '<color rgb="'.$this->C[$index].'"/>' : '') .( $xf[2] & self::F_COLOR ? '<color rgb="'.$xf[4].'"/>' : '')
.'</font>'; .'</font>';
} }
$FONTS[] = '</fonts>'; // fills
$XF = ['<cellXfs count="'.count($this->XF).'">']; $FL_KEY = $xf[3].'-'.$xf[5];
foreach( $this->XF as $xf ) { if (isset($FL_KEYS[$FL_KEY])) {
$align = ($xf[2] === self::A_LEFT ? ' applyAlignment="1"><alignment horizontal="left"/>' : '') $FL_ID = $FL_KEYS[ $FL_KEY ];
.($xf[2] === self::A_RIGHT ? ' applyAlignment="1"><alignment horizontal="right"/>' : '') } else {
.($xf[2] === self::A_CENTER ? ' applyAlignment="1"><alignment horizontal="center"/>' : ''); $FL_ID = $FL_KEYS[ $FL_KEY ] = count($FILLS);
$XF[] = '<xf numFmtId="'.$xf[0].'" fontId="'.$xf[1].'" fillId="'.$xf[4].'" borderId="0" xfId="0"' $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"' : '') .($xf[0] > 0 ? ' applyNumberFormat="1"' : '')
.($F_ID > 0 ? ' applyFont="1"' : '')
.($FL_ID > 0 ? ' applyFill="1"' : '')
.($align ? $align . '</xf>' : '/>'); .($align ? $align . '</xf>' : '/>');
} }
// wrap collections
array_unshift( $XF, '<cellXfs count="'.count($XF).'">');
$XF[] = '</cellXfs>'; $XF[] = '</cellXfs>';
$FILLS = ['<fills count="'.count($this->B).'">']; array_unshift($FONTS, '<fonts count="'.count($FONTS).'">');
foreach( $this->B as $fill){ $FONTS[] = '</fonts>';
if($fill===0){ array_unshift($FILLS, '<fills count="'.count($FILLS).'">');
$FILLS[] = '<fill><patternFill patternType="none"></patternFill></fill>';
} else {
$FILLS[] = '<fill><patternFill patternType="solid"><fgColor rgb="'.$fill.'"/><bgColor indexed="64"/></patternFill></fill>';
}
}
$FILLS[] = '</fills>'; $FILLS[] = '</fills>';
$template = str_replace(['{FONTS}','{XF}','{FILLS}'], [implode("\r\n", $FONTS), implode("\r\n", $XF), implode("\r\n", $FILLS)], $template); $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); $this->_writeEntry($fh, $cdrec, $cfilename, $template);
$entries++; $entries++;
@ -463,7 +496,7 @@ class SimpleXLSXGen {
} }
$ct = $cv = null; $ct = $cv = null;
$N = $F = $A = $C = $B = 0; $N = $A = $F = $FL = $C = $B = 0;
if ( is_string($v) ) { if ( is_string($v) ) {
@ -484,14 +517,16 @@ class SimpleXLSXGen {
if ( strpos( $v, '<s>' ) !== false ) { if ( strpos( $v, '<s>' ) !== false ) {
$F += self::F_STRIKE; $F += self::F_STRIKE;
} }
if ( strpos( $v, '<style' ) !== false ) { if ( preg_match('/<style([^>]+)>/', $v, $m ) ) {
preg_match('/(?<= color=").*?(?=")/', $v, $cValue);
if(!empty($cValue)){ if ( preg_match('/ color="([^"]+)"/', $m[1], $m2) ) {
$C = $cValue[0];
$F += self::F_COLOR;
$C = strlen($m2[1]) === 8 ? $m2[1] : ('FF' . ltrim($m2[1],'#'));
} }
preg_match('/(?<= bgcolor=").*?(?=")/', $v, $bValue); if ( preg_match('/ bgcolor="([^"]+)"/', $m[1], $m2) ) {
if(!empty($bValue)){ $FL += self::FL_COLOR;
$B = $bValue[0]; $B = strlen($m2[1]) === 8 ? $m2[1] : ('FF' . ltrim($m2[1],'#'));
} }
} }
if ( strpos( $v, '<left>' ) !== false ) { if ( strpos( $v, '<left>' ) !== false ) {
@ -506,11 +541,11 @@ class SimpleXLSXGen {
if ( preg_match( '/<a href="(https?:\/\/[^"]+)">(.*?)<\/a>/i', $v, $m ) ) { if ( preg_match( '/<a href="(https?:\/\/[^"]+)">(.*?)<\/a>/i', $v, $m ) ) {
$h = explode( '#', $m[1] ); $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] : '']; $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 ) ) { 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' => '']; $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 ); $v = strip_tags( $v );
} // tags } // tags
@ -547,14 +582,14 @@ class SimpleXLSXGen {
$cv = $this->date2excel( $m[3], $m[2], $m[1], $m[4], $m[5], $m[6] ); $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 $N = self::N_DATETIME; // [22] m/d/yy h:mm
} elseif ( preg_match( '/^[0-9+-.]+$/', $v ) ) { // Long ? } elseif ( preg_match( '/^[0-9+-.]+$/', $v ) ) { // Long ?
$A = self::A_RIGHT; $A += self::A_RIGHT;
} elseif ( preg_match( '/^https?:\/\/\S+$/i', $v ) ) { } elseif ( preg_match( '/^https?:\/\/\S+$/i', $v ) ) {
$h = explode( '#', $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] : '']; $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 ) ) { } 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' => '']; $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 ) { if ( ($N === self::N_DATE || $N === self::N_DATETIME) && $cv < 0 ) {
$cv = null; $cv = null;
@ -599,31 +634,26 @@ class SimpleXLSXGen {
$COL[ $CUR_COL ] = max( $vl, $COL[ $CUR_COL ] ); $COL[ $CUR_COL ] = max( $vl, $COL[ $CUR_COL ] );
$cs = 0; $cs = 0;
if ( $N + $F + $A > 0 OR $C != 0 OR $B !=0) {
if ( isset($this->F_KEYS[ $F."-".$C ] ) ) { if ( $N + $A + $F + $FL > 0 ) {
$cf = $this->F_KEYS[ $F."-".$C ];
} else { if ( $FL === self::FL_COLOR ) {
$cf = count($this->F); $FL += self::FL_SOLID;
$this->F_KEYS[$F."-".$C] = $cf;
$this->F[] = $F;
$this->C[] = $C;
} }
if ( isset($this->B_KEYS[ $B ] ) ) { if ( ($F & self::F_HYPERLINK) && !($F & self::F_COLOR)) {
$bk = $this->B_KEYS[ $B ]; $F += self::F_COLOR;
} else { $C = 'FF0563C1';
$bk = count($this->B);
$this->B_KEYS[$B] = $bk;
$this->B[] = $B;
} }
$NFA = 'N' . $N . 'F' . $cf . 'A' . $A . 'C'. $C . 'B' . $bk;
if ( isset( $this->XF_KEYS[ $NFA ] ) ) { $XF_KEY = $N . '-' . $A . '-' . $F. '-'. $FL . '-' . $C . '-' . $B;
$cs = $this->XF_KEYS[ $NFA ]; // echo $cname .'='.$XF_KEY.PHP_EOL;
if ( isset( $this->XF_KEYS[ $XF_KEY ] ) ) {
$cs = $this->XF_KEYS[ $XF_KEY ];
} }
if ( $cs === 0 ) { if ( $cs === 0 ) {
$cs = count( $this->XF ); $cs = count( $this->XF );
$this->XF_KEYS[ $NFA ] = $cs; $this->XF_KEYS[ $XF_KEY ] = $cs;
$this->XF[] = [$N, $cf, $A, $C, $bk]; $this->XF[] = [$N, $A, $F, $FL, $C, $B];
} }
} }