mirror of
https://github.com/shuchkin/simplexlsxgen.git
synced 2023-08-10 21:12:59 +03:00
Merge pull request #58 from mrjemson/master
Added Font Color and Background Color Capabilities
This commit is contained in:
commit
5840fd5285
@ -60,6 +60,9 @@ $data = [
|
||||
['Italic', '<i>12345.67</i>'],
|
||||
['Underline', '<u>12345.67</u>'],
|
||||
['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>'],
|
||||
['Hyperlink', 'https://github.com/shuchkin/simplexlsxgen'],
|
||||
['Italic + Hyperlink + Anchor', '<i><a href="https://github.com/shuchkin/simplexlsxgen">SimpleXLSXGen</a></i>'],
|
||||
|
@ -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' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
@ -80,7 +85,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}
|
||||
@ -295,14 +300,15 @@ class SimpleXLSXGen {
|
||||
$entries++;
|
||||
} elseif ( $cfilename === 'xl/styles.xml' ) {
|
||||
$FONTS = ['<fonts count="'.count($this->F).'">'];
|
||||
foreach ( $this->F as $f ) {
|
||||
foreach ( $this->F as $index => $f ) {
|
||||
$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/>' : '')
|
||||
.( $f & self::F_HYPERLINK ? '<u/>' : '')
|
||||
.( isset($this->C[$index]) ? '<color rgb="'.$this->C[$index].'"/>' : '')
|
||||
.'</font>';
|
||||
}
|
||||
$FONTS[] = '</fonts>';
|
||||
@ -311,13 +317,22 @@ class SimpleXLSXGen {
|
||||
$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"'
|
||||
$XF[] = '<xf numFmtId="'.$xf[0].'" fontId="'.$xf[1].'" fillId="'.$xf[4].'" borderId="0" xfId="0"'
|
||||
.($xf[0] > 0 ? ' applyNumberFormat="1"' : '')
|
||||
.($align ? $align . '</xf>' : '/>');
|
||||
|
||||
}
|
||||
$XF[] = '</cellXfs>';
|
||||
$template = str_replace(['{FONTS}','{XF}'], [implode("\r\n", $FONTS), implode("\r\n", $XF)], $template);
|
||||
$FILLS = ['<fills count="'.count($this->B).'">'];
|
||||
foreach( $this->B as $fill){
|
||||
if($fill===0){
|
||||
$FILLS[] = '<fill><patternFill patternType="none"></patternFill></fill>';
|
||||
} else {
|
||||
$FILLS[] = '<fill><patternFill patternType="solid"><fgColor rgb="'.$fill.'"/><bgColor indexed="64"/></patternFill></fill>';
|
||||
}
|
||||
}
|
||||
$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, '<s>' ) !== false ) {
|
||||
$F += self::F_STRIKE;
|
||||
}
|
||||
if ( strpos( $v, '<style' ) !== false ) {
|
||||
preg_match('/(?<= color=").*?(?=")/', $v, $cValue);
|
||||
if(!empty($cValue)){
|
||||
$C = $cValue[0];
|
||||
}
|
||||
preg_match('/(?<= bgcolor=").*?(?=")/', $v, $bValue);
|
||||
if(!empty($bValue)){
|
||||
$B = $bValue[0];
|
||||
}
|
||||
}
|
||||
if ( strpos( $v, '<left>' ) !== 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 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
BIN
styles.png
BIN
styles.png
Binary file not shown.
Before Width: | Height: | Size: 57 KiB After Width: | Height: | Size: 58 KiB |
Loading…
Reference in New Issue
Block a user