diff --git a/README.md b/README.md index 583e73e..42411c8 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,9 @@ $data = [ ['Italic', '12345.67'], ['Underline', '12345.67'], ['Strike', '12345.67'], + ['Green', ''], + ['Bold Red Text', ''], + ['Blue Text and Yellow Fill', ''], ['Bold + Italic', '12345.67'], ['Hyperlink', 'https://github.com/shuchkin/simplexlsxgen'], ['Italic + Hyperlink + Anchor', 'SimpleXLSXGen'], diff --git a/src/SimpleXLSXGen.php b/src/SimpleXLSXGen.php index 8809947..762b64f 100644 --- a/src/SimpleXLSXGen.php +++ b/src/SimpleXLSXGen.php @@ -16,6 +16,7 @@ class SimpleXLSXGen { protected $template; protected $F, $F_KEYS; // fonts protected $XF, $XF_KEYS; // cellXfs + protected $B, $B_KEYS; // background fills protected $SI, $SI_KEYS; // shared strings const N_NORMAL = 0; // General const N_INT = 1; // 0 @@ -31,6 +32,8 @@ class SimpleXLSXGen { const F_ITALIC = 4; const F_UNDERLINE = 8; const F_STRIKE = 16; + const C_NORMAL = 0; + const B_NORMAL = 0; const A_DEFAULT = 0; const A_LEFT = 1; const A_RIGHT = 2; @@ -44,8 +47,10 @@ class SimpleXLSXGen { $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->C = [ self::C_NORMAL ]; // + $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 = ['N0F0A0C0B0' => 0 ]; // & keys $this->template = [ '_rels/.rels' => ' @@ -80,7 +85,7 @@ class SimpleXLSXGen { 'xl/styles.xml' => ' {FONTS} - +{FILLS} {XF} @@ -295,14 +300,15 @@ class SimpleXLSXGen { $entries++; } elseif ( $cfilename === 'xl/styles.xml' ) { $FONTS = ['']; - foreach ( $this->F as $f ) { + foreach ( $this->F as $index => $f ) { $FONTS[] = '' . ( $this->defaultFontSize ? '' : '' ) .( $f & self::F_BOLD ? '' : '') .( $f & self::F_ITALIC ? '' : '') .( $f & self::F_UNDERLINE ? '' : '') .( $f & self::F_STRIKE ? '' : '') - .( $f & self::F_HYPERLINK ? '' : '') + .( $f & self::F_HYPERLINK ? '' : '') + .( isset($this->C[$index]) ? '' : '') .''; } $FONTS[] = ''; @@ -311,13 +317,22 @@ class SimpleXLSXGen { $align = ($xf[2] === self::A_LEFT ? ' applyAlignment="1">' : '') .($xf[2] === self::A_RIGHT ? ' applyAlignment="1">' : '') .($xf[2] === self::A_CENTER ? ' applyAlignment="1">' : ''); - $XF[] = ' 0 ? ' applyNumberFormat="1"' : '') .($align ? $align . '' : '/>'); } $XF[] = ''; - $template = str_replace(['{FONTS}','{XF}'], [implode("\r\n", $FONTS), implode("\r\n", $XF)], $template); + $FILLS = ['']; + foreach( $this->B as $fill){ + if($fill===0){ + $FILLS[] = ''; + } else { + $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 { @@ -448,7 +463,7 @@ class SimpleXLSXGen { } $ct = $cv = null; - $N = $F = $A = 0; + $N = $F = $A = $C = $B = 0; if ( is_string($v) ) { @@ -469,6 +484,16 @@ class SimpleXLSXGen { if ( strpos( $v, '' ) !== false ) { $F += self::F_STRIKE; } + if ( strpos( $v, '' ) !== false ) { $A += self::A_LEFT; } @@ -574,23 +599,31 @@ class SimpleXLSXGen { $COL[ $CUR_COL ] = max( $vl, $COL[ $CUR_COL ] ); $cs = 0; - if ( $N + $F + $A > 0 ) { + if ( $N + $F + $A > 0 OR $C != 0 OR $B !=0) { - if ( isset($this->F_KEYS[ $F ] ) ) { - $cf = $this->F_KEYS[ $F ]; + if ( isset($this->F_KEYS[ $F."-".$C ] ) ) { + $cf = $this->F_KEYS[ $F."-".$C ]; } else { $cf = count($this->F); - $this->F_KEYS[$F] = $cf; + $this->F_KEYS[$F."-".$C] = $cf; $this->F[] = $F; + $this->C[] = $C; } - $NFA = 'N' . $N . 'F' . $cf . 'A' . $A; + if ( isset($this->B_KEYS[ $B ] ) ) { + $bk = $this->B_KEYS[ $B ]; + } else { + $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 ] ) ) { $cs = $this->XF_KEYS[ $NFA ]; } if ( $cs === 0 ) { $cs = count( $this->XF ); $this->XF_KEYS[ $NFA ] = $cs; - $this->XF[] = [$N, $cf, $A]; + $this->XF[] = [$N, $cf, $A, $C, $bk]; } } @@ -689,4 +722,4 @@ class SimpleXLSXGen { // but we use fast version return str_replace( ['&', '<', '>', "\x00","\x03","\x0B"], ['&', '<', '>', '', '', ''], $str ); } -} \ No newline at end of file +} diff --git a/styles.png b/styles.png index 833cf97..37624bb 100644 Binary files a/styles.png and b/styles.png differ